# In this example, we bundle so many libraries (down to glibc)
# that the resulting AppImage runs even on
# ubuntu-10.04.1-desktop-amd64.iso
# although it is using 16.04 components from 6 years later!
# Note the special usr/bin/sigil that is used to pull this magic

APP=Sigil
LOWERAPP=sigil

mkdir -p ./$APP/$APP.AppDir/usr/lib

cd ./$APP/

wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh
. ./functions.sh

# We get this app and almost all its dependencies via apt-get
# but not using the host system's information about what is
# installed on the system but our own assumptions instead

cat > status <<EOF
Package: debconf
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: dpkg
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: libcups2
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: dbus
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: fontconfig
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: passwd
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: uuid-runtime
Status: install ok installed
Architecture: all
Version: 9999.9999.9999

Package: libgl1-mesa
Status: install ok installed
Architecture: all
Version: 9999.9999.9999
Comment: Draws in 100 MB of dependencies

Package: mime-support
Status: install ok installed
Architecture: all
Version: 9999.9999.9999
EOF

echo "deb http://archive.ubuntu.com/ubuntu/ xenial main universe
deb http://ppa.launchpad.net/sunab/sigil-release/ubuntu xenial main " > sources.list

apt-get $OPTIONS update

URLS=$(apt-get $OPTIONS -y install --print-uris hunspell-en-us python3-chardet python3-pil python3-cssselect python3-cssutils python3-html5lib python3-regex python3-chardet python3-six $LOWERAPP | cut -d "'" -f 2 | grep -e "^http")

wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh
. ./functions.sh

wget -c $URLS

cd ./$APP.AppDir/

find ../*.deb -exec dpkg -x {} . \;

find . -name $LOWERAPP.desktop -exec cp {} . \;

get_apprun
patch_usr

rm -rf ./usr/lib/apt/ ./usr/lib/dpkg/ ./bin ./sbin ./var ./usr/share/man ./usr/sbin ./usr/include || true

# Save > 100 MB
rm -rf ./usr/lib/x86_64-linux-gnu/dri

# The following does the magic of running the bundled ld-linux-x86-64.so.2
# so that it can load a newer glibc than what is present on the host system.
# Crashes if GCONV_PATH is not set to match, and if its location 
# is not on the --library-path.

cat > ./usr/bin/sigil <<\EOF
#!/bin/bash
export LD_PRELOAD=./lib/sigil/libsigilgumbo.so # Why is this needed? Bug? FIXME
export SIGIL_DICTIONARIES=./share/hunspell/
export GCONV_PATH=./lib/x86_64-linux-gnu/gconv
export QT_PLUGIN_PATH=./lib/x86_64-linux-gnu/qt5/plugins/
export LD_LIBRARY_PATH=./lib/x86_64-linux-gnu/qt5/plugins/platforms:$LD_LIBRARY_PATH
export PYTHONPATH=./lib/python3.5:./lib/python3.5/plat-x86_64-linux-gnu/:./lib/python3/dist-packages/
exec ../lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 --library-path ../lib:../lib/x86_64-linux-gnu:./lib:./lib/x86_64-linux-gnu  ./lib/sigil/sigil "$@"
EOF
chmod a+x ./usr/bin/sigil

cp ./usr/share/pixmaps/sigil.png .

get_desktopintegration $LOWERAPP

cd ..

VERSION=$(ls "${LOWERAPP}"_*deb | head -n 1 | cut -d "_" -f 2 | cut -d "+" -f 1)

generate_appimage
