#!/bin/bash

# This should bascically be the same as a static build, but the
# "make install" is followed by the appimage creation. After the
# install does its work, the bin directory and subdirectories
# contain the whole application.

# Install linuxdeploy (CLI version). Download from
# https://github.com/linuxdeploy/linuxdeploy/releases/continuous . It is an
# AppImage, set it executable, and put it in a directory in your path.
# For instance, depending on distribution, directory ~/bin and/or ~/.local/bin
# might be automatically included in your path at log-in time it they exists.
# Or use a globally accessable directory if different logins need to us it.

# To include the context-sensitive help, specify the directory
# where the manual source is. This script will then call the "translate"
# script there to build the manual, and copy it to the proper place
# before the AppImage is built.

CINGG_DIRECTORY=$(pwd)
MANUAL_DIRECTORY=../../cin-manual-latex

# Build Cinelerra-GG itself
( ./autogen.sh
  ./configure --with-single-user --enable-static-build
  make && make install ) 2>&1 | tee cingg.log
mv Makefile Makefile.cfg
cp Makefile.devel Makefile

# Build the manual if the directory is specified and exists, and copy it
# to CinGG
if [ -d "$MANUAL_DIRECTORY" ]; then
  pushd $MANUAL_DIRECTORY
  # run the build in a subshell so if some error causes an exit
  # the popd will still work.
  (./translate_manual) 2>&1 | tee $CINGG_DIRECTORY/manual.log
  popd
  rm -rf bin/doc/CinelerraGG_Manual
  cp -r $MANUAL_DIRECTORY/CinelerraGG_Manual bin/doc/
fi

rm -rf AppDir           # remove old in case a file gets deleted from the distribution
mkdir -p AppDir/usr
cp -r bin AppDir/usr/   # copy whole of bin directory, inclusive html manual if present

# We need to specify all executables, so linuxdeploy can pick up dependencies.
# Any executable code in other places like plugins in not picked up (yet).
~/bin/linuxdeploy-x86_64.AppImage --appdir=AppDir -o appimage -d image/cin.desktop -i image/cin.svg -e bin/cin -e bin/mpeg2enc -e bin/mplex -e bin/hveg2enc -e bin/lv2ui -e bin/bdwrite -e bin/zmpeg3toc -e bin/zmpeg3show -e bin/zmpeg3cat -e bin/zmpeg3ifochk -e bin/zmpeg3cc2txt -e bin/mplexlo -l /usr/lib/x86_64-linux-gnu/libjbig.so.0 -l /usr/lib/x86_64-linux-gnu/libbz2.so 02>&1 | tee appimage.log

# There is now an appimage in the cinelerra-5.1 directory.


