# In this example, we use non-repo .deb
# Some projects provide raw .deb files without a repository
# hence we create our own local repository as part of
# the AppImage creation process in order to "install"
# the package using apt-get as normal
# using debian oldstable (wheezy)

APP=ser-player
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

generate_status

# Workaround for bug in the package (libfontconfig should be libfontconfig1)
cat >> status <<\EOF

Package: libfontconfig
Status: install ok installed
Architecture: all
Version: 9:9999.9999.9999
Comment: This package does not actually exist in debian wheezy
EOF

echo "deb http://ftp.de.debian.org/debian/ wheezy main
" > sources.list

wget -c "https://dl.dropboxusercontent.com/u/1139742/ser_player_debug/ser-player_1.5.2-1_amd64.deb"

# Add local repository so that we can install deb files
# that were downloaded outside of a repository
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
echo "deb file:$(readlink -e $PWD) ./" >> sources.list

apt-get $OPTIONS update

URLS=$(apt-get $OPTIONS -y install --print-uris $LOWERAPP | cut -d "'" -f 2 | grep -e "^http")

wget -c $URLS

cd ./$APP.AppDir/

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

cp ./usr/share/applications/$LOWERAPP.desktop .
cp ./usr/share/app-install/icons/ser-player.png .

get_apprun

# copy_deps

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

mv ./usr/lib/pulseaudio/*.so usr/lib/ || true

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" {} \;

# Workaround for:
# xkbcommon: ERROR: failed to add default include path ././/share/X11/xkb
# Qt: Failed to create XKB context!
# Use QT_XKB_CONFIG_ROOT environmental variable
( cd usr/share/X11 ; ln -s /usr/share/X11/xkb . )

# Workaround for:
# find: ‘/tmp/.mount_T1Wwtc/usr/share/icons/’: No such file or directory
mkdir -p ./usr/share/icons/hicolor/128x128/apps/
cp ./usr/share/app-install/icons/ser-player.png ./usr/share/icons/hicolor/128x128/apps/

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

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
