# In this example, we use a trusty PPA - keep providing these!
# Dear upstream developers, if you want the AppImage to run on distributions
# older than trusty, then please provide binaries compiled e.g., on debian oldstable
# Based on the Wireshark recipe

APP=FreeCAD
LOWERAPP=freecad-daily # ${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

echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe
deb http://ppa.launchpad.net/freecad-maintainers/freecad-daily/ubuntu trusty main
" > sources.list

apt-get $OPTIONS update

URLS=$(apt-get $OPTIONS -y install --print-uris $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 {} . \; || true

cp ./usr/share/applications/$LOWERAPP.desktop .

# Dear upstream developers, please
# fix the desktop file so that the following becomes unneccessary
sed -i -e 's|/usr/bin/||g' $LOWERAPP.desktop
sed -i -e 's|Path=|# Path=|g' $LOWERAPP.desktop
sed -i -e 's|Icon=freecad|Icon=freecad-daily|g' $LOWERAPP.desktop

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

# Dear upstream developers, please put an icon in one of the locations above
# so that the following is not needed
cp ./usr/share/icons/hicolor/64x64/apps/freecad-daily.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/ || true # AppRun sets Qt env here

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

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

# Dear upstream developers, please use relative rather than absolute paths
# then binary patching like this will become unneccessary
find usr/ -type f -exec sed -i -e "s|/usr/lib/freecad-daily|././/lib/freecad-daily|g" {} \;
find usr/ -type f -exec sed -i -e "s|/usr/share/freecad-daily|././/share/freecad-daily|g" {} \;
( cd ./usr/lib/freecad-daily/ ; ln -s ../../share/ . ) # Why?

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
