#!/bin/bash -e

. ./VERSION

TARGET=target
PREFIX=$TARGET/debian
ARTIFACTS=$TARGET/artifacts
CONTROL="DEBIAN/control"
ROOT=`pwd`
COMPLETE_PATH=$ROOT/$ARTIFACTS

#Instalo componentes auxiliares ----------------------------------------------------------------------------
apt-get update
##apt-get install nasm
##apt-get install libx264-157
##apt-get install libvpx5
##apt-get install libx264-dev
##apt-get install libfdk-aac2
##apt-get install libvpx-dev
##apt-get install libfdk-aac-dev
##apt-get install libdav1d3
##apt-get install libunistring2
##apt-get install libunistring-dev
##apt-get install libopus-dev
##apt-get install libdav1d-dev
##apt-get install libunistring-dev
##apt-get install libass9 					
##apt-get install libexpat1-dev   	
##apt-get install libfontconfig1-dev
##apt-get install libfreetype6-dev  
##apt-get install libfribidi-dev    
##apt-get install libfontconfig1-dev
##apt-get install libfreetype6-dev
##apt-get install libass-dev			
##apt-get install libgnutls-dane0     
##apt-get install libgnutls-openssl27 
##apt-get install libgnutls28-dev 		
##apt-get install libgnutls30         
##apt-get install libgnutlsxx28     	
##apt-get install libmp3lame-dev	
##apt-get install libogg-dev
##apt-get install libvorbis-dev		
##apt-get install ninja-build
##apt-get install meson						
##apt-get install yasm		
##apt-get install libx265-dev
##apt-get install libnuma1
##apt-get install libnuma-dev

echo "FULL VERSION: $FULL_VERSION"
echo "FSNAME:       $FSNAME"

##rm -rf $TARGET

##mkdir -p $PREFIX
##mkdir -p $ARTIFACTS

cp -r debian/* $PREFIX/
sed -i -e  "s/FULL_VERSION/$FULL_VERSION/" $PREFIX/$CONTROL
sed -i -e  "s/ARCHITECTURE/$ARCHITECTURE/" $PREFIX/$CONTROL

REPO=http://packages.inconcert/repo/ffmpeg
DEST="artifacts"

##mkdir $COMPLETE_PATH/ffmpeg_build
##mkdir $COMPLETE_PATH/bin
#COMPILE aom -------------------------------------------------------------------------------------------------
##ARCHIVE=aom.gz
##DIR=aom
##wget  -O  $ARTIFACTS/$ARCHIVE   $REPO/$ARCHIVE
##cd $ARTIFACTS
##echo `pwd`
##tar -xvzf $ARCHIVE

##mkdir -p aom_build
##cd aom_build
##PATH="$COMPLETE_PATH/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$COMPLETE_PATH/ffmpeg_build" -DENABLE_TESTS=OFF -DENABLE_NASM=on ../aom
##PATH="$COMPLETE_PATH/bin:$PATH" make
##make install

#COMPILE SVT-AV1 -------------------------------------------------------------------------------------------------
##cd $ROOT
##echo `pwd`
##ARCHIVE=SVT-AV1.gz
##DIR=SVT-AV1
##wget  -O  $ARTIFACTS/$ARCHIVE   $REPO/$ARCHIVE
##cd $ARTIFACTS
##echo `pwd`
##tar -xvzf $ARCHIVE
##mkdir -p SVT-AV1/build
##cd SVT-AV1/build
##PATH="$COMPLETE_PATH/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$COMPLETE_PATH/ffmpeg_build" -DCMAKE_BUILD_TYPE=Release -DBUILD_DEC=OFF -DBUILD_SHARED_LIBS=OFF .. 
##PATH="$COMPLETE_PATH/bin:$PATH"
##make install

#COMPILE FFMPEG -------------------------------------------------------------------------------------------------
cd $ROOT
echo `pwd`
##ARCHIVE=ffmpeg-snapshot.tar.bz2
##ARCHIVE_TAR=ffmpeg-snapshot.tar
DIR=ffmpeg
##wget  -O  $ARTIFACTS/$ARCHIVE   $REPO/$ARCHIVE
##cd $ARTIFACTS
##echo `pwd`
##bzip2 -d $ARCHIVE
##tar -xvf $ARCHIVE_TAR
##cd $DIR

##PATH="$COMPLETE_PATH/bin:$PATH" PKG_CONFIG_PATH="$COMPLETE_PATH/ffmpeg_build/lib/pkgconfig" ./configure \
##    --prefix="$COMPLETE_PATH/ffmpeg_build" \
##    --pkg-config-flags="--static" \
##    --extra-cflags="-I$COMPLETE_PATH/ffmpeg_build/include" \
##    --extra-ldflags="-L$COMPLETE_PATH/ffmpeg_build/lib" \
##    --extra-libs="-lpthread -lm" \
##    --ld="g++" \
##    --bindir="$COMPLETE_PATH/bin" \
##    --enable-gpl \
##    --enable-gnutls \
##    --enable-libaom \
##    --enable-libass \
##    --enable-libfdk-aac \
##    --enable-libfreetype \
##   --enable-libmp3lame \
##    --enable-libopus \
##    --enable-libsvtav1 \
##    --enable-libdav1d \
##    --enable-libvorbis \
##    --enable-libvpx \
##    --enable-libx264 \
##    --enable-libx265 \
##    --enable-nonfree 
##PATH="$COMPLETE_PATH/bin:$PATH" make && \
##  hash -r

cd $ROOT

mkdir -p $PREFIX/usr/bin
mkdir -p $PREFIX/lib

dos2unix $PREFIX/DEBIAN/postinst
dos2unix $PREFIX/DEBIAN/prerm

chmod 775 $PREFIX/DEBIAN/postinst
chmod 775 $PREFIX/DEBIAN/prerm

cp $ARTIFACTS/$DIR/ffmpeg $PREFIX/usr/bin
cp /usr/lib/x86_64-linux-gnu/libfdk-aac.so.2.0.0 $PREFIX/lib/
cp /usr/lib/x86_64-linux-gnu/libdav1d.so.3.0.0 $PREFIX/lib/
cp /usr/lib/x86_64-linux-gnu/libx264.so.157 $PREFIX/lib/
cp /usr/lib/x86_64-linux-gnu/libass.so.9.0.2 $PREFIX/lib/
dpkg-deb --build $PREFIX $TARGET/$FSNAME
