# In this example, we use a trusty PPA - keep providing these!
# Based on the Stellarium and Wireshark recipes
# Here some desktop, icon, and binary files are uppercase, is this non-debian-standard?

APP=FBReader
LOWERAPP=${APP,,}

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: libglib2.0-0
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libgtk2.0-0
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libegl1-mesa
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libgbm1
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libdrm2
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libdbm2
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libgl1-mesa-glx
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libgcc1
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: lsb-base
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libc6
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: libstdc++6
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: debconf
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

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

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

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

Package: libfontconfig1
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

Package: fontconfig-config
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999

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

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

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

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

echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe
deb http://ppa.launchpad.net/wireshark-dev/stable/ubuntu trusty main
" > sources.list

apt-get $OPTIONS update

URLS=$(apt-get $OPTIONS -y install --print-uris libbz2-1.0 libexpat1 libfribidi0 libqt4-network libqtcore4 libqtgui4 libsqlite3-0 zlib1g | 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
wget -c https://fbreader.org/files/desktop/fbreader_0.99.4-1_amd64.deb
wget -c https://fbreader.org/files/desktop/libunibreak1_1.0-1_amd64.deb

cd ./$APP.AppDir/

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

# cp ./usr/share/applications/$LOWERAPP.desktop .
cp ./usr/share/applications/$APP.desktop . # Is this non-debian-standard?

rm -rf ./usr/share/icons/48x48/apps || true
find ./usr/share/icons -path *256* -name $LOWERAPP.png -exec cp {} . \; || true
find ./usr/share/icons -path *512* -name $LOWERAPP.png -exec cp {} . \; || true

cp ./usr/share/pixmaps/FBReader.png .

get_apprun

# Copy in the indirect dependencies
# copy_deps # not needed here due to the way we use apt to download everything

move_lib
mv ./usr/lib/x86_64-linux-gnu/* ./usr/lib/ # AppRun sets Qt env here

delete_blacklisted
rm -rf ./etc/ ./home/ || true

# patch_usr
# Patching only the executable files seems not to be enough for some apps
find usr/ -type f -exec sed -i -e "s|/usr|././|g" {} \;

VERSION=$(find ../*.deb -name $LOWERAPP"_*" | head -n 1 | cut -d "~" -f 1 | cut -d "_" -f 2 | cut -d "-" -f 1 | sed -e 's|1%3a||g' )
echo $VERSION

mv usr/bin/FBReader usr/bin/fbreader # Is this non-debian-standard?
sed -i -e 's|Exec=FBReader|Exec=fbreader|g' FBReader.desktop # Is this non-debian-standard?
mv FBReader.desktop fbreader.desktop # Is this non-debian-standard?

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
