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

APP=Darktable
LOWERAPP=${APP,,}

sudo add-apt-repository -y ppa:pmjdebruijn/darktable-unstable
# sudo add-apt-repository -y ppa:pmjdebruijn/darktable-release
sudo apt-get update

sudo apt-get -y install $LOWERAPP

mkdir -p $APP/$APP.AppDir
cd $APP/$APP.AppDir
find /var/cache/apt/archives/*$LOWERAPP* -exec dpkg -x {} . \; || true
find /var/cache/apt/archives/*-data* -exec dpkg -x {} . \; || true

cp ./usr/share/applications/$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

get_apprun

# Copy in the indirect dependencies
copy_deps ; copy_deps ; copy_deps # Three runs to ensure we catch indirect ones

move_lib

delete_blacklisted

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

# Workaround for:
# GLib-GIO-ERROR **: Settings schema 'org.gtk.Settings.FileChooser' is not installed
# when trying to use the file open dialog
# AppRun exports usr/share/glib-2.0/schemas/ which might be hurting us here
( mkdir -p usr/share/glib-2.0/schemas/ ; cd usr/share/glib-2.0/schemas/ ; ln -s /usr/share/glib-2.0/schemas/gschemas.compiled . )

VERSION=$(find /var/cache/apt/archives/ -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
