#!/bin/bash

FILE="ffmpeg-5.1.2.tar.bz2"
WORKDIR="ffmpeg-5.1.2"

# where to install ffmpeg
#INSTALLDIR="/usr"                  # install system-wide
INSTALLDIR="`realpath .`/ffmpeg"    # install in current directory

# where to install docs
#DOCDIR="/usr/doc/ffmpeg"           # install system-wide
DOCDIR="`realpath .`/ffmpeg-doc"    # install in current directory

# -----------------------------------------------------------------------------

# clean up previous work
[ -d $WORKDIR ] && rm -rf $WORKDIR

# extract source archive
tar -xf $FILE || exit

# enter build directory
cd $WORKDIR || exit

# -----------------------------------------------------------------------------

# apply patch
patch -b -Np1 -i ../ffmpeg-5.1.2-hls-seek-fix.patch || exit

# apply patch
sed -i 's/-lflite"/-lflite -lasound"/' configure || exit

# -----------------------------------------------------------------------------

# set build target
[ -v TARGET ] && target="$TARGET" || target="`uname -m`"

case $target in

    i686)    arch="x86_32" ;;
    x86_64)  arch="x86_64" ;;
    arm*)    arch="arm" ;;
    aarch64) arch="aarch64" ;;

    *)  echo "ERROR: Unknown target specified." 
        exit
        ;;
esac

# -----------------------------------------------------------------------------

# configure package
./configure --prefix=$INSTALLDIR \
            --docdir=$DOCDIR \
            --arch=$arch \
            --enable-gpl \
            --enable-nonfree \
            --enable-static \
            --enable-shared \
            --disable-debug \
            --enable-libdav1d \
            --enable-libfdk-aac \
            --enable-libmp3lame \
            --enable-libopus \
            --enable-libsrt \
            --enable-libtheora \
            --enable-libvorbis \
            --enable-libvpx \
            --enable-libx264 \
            --enable-libx265 \
            --enable-openssl || exit

# build package
make || exit

# build PDF documentation with texlive
[ -f /usr/bin/latex ] &&
{
    pushd doc

    for DOCNAME in `basename -s .html *.html`
    do
        texi2pdf -b $DOCNAME.texi || exit
        texi2dvi -b $DOCNAME.texi || exit
        dvips -o $DOCNAME.ps $DOCNAME.dvi || exit
    done

    popd
}

# install package
make install || exit

# -----------------------------------------------------------------------------

# install text documentation
install -vm755 -d $DOCDIR || exit
install -vm644 doc/*.txt $DOCDIR || exit

[ -f /usr/bin/latex ] &&
{
    # install PDF/postscript docs
    install -vm644 doc/*.pdf $DOCDIR || exit
    install -vm644 doc/*.ps $DOCDIR || exit
}

# prepare to build API docs
cd doc && mkdir -p doc/doxy || exit

# remove clang dependency
doxygen -u || exit

# generate API docs
doxygen 

# install API docs
install -vm755 -d $DOCDIR/api || exit
cp -vr doc/doxy/html/* $DOCDIR/api || exit

# set proper permissions for API documentation
find $DOCDIR/api -type f -exec chmod -c 0644 \{} \;
find $DOCDIR/api -type d -exec chmod -c 0755 \{} \;

# -----------------------------------------------------------------------------

# clean up
cd ../.. && rm -rf $WORKDIR

# -----------------------------------------------------------------------------

