APP=GIMP
LOWERAPP=gimp

sudo add-apt-repository -y ppa:otto-kesselgulasch/gimp-edge
sudo apt-get update

sudo apt-get -y install $LOWERAPP

mkdir -p $APP/$APP.AppDir

cd $APP

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

cd $APP.AppDir
find /var/cache/apt/archives/$LOWERAPP* -exec dpkg -x {} . \;
find /var/cache/apt/archives/libbabl* -exec dpkg -x {} . \;
find /var/cache/apt/archives/libgegl* -exec dpkg -x {} . \;
find /var/cache/apt/archives/libgimp* -exec dpkg -x {} . \;

rm -rf ./usr/share/icons/48x48/apps || true
rm -rf ./usr/share/icons/hicolor/48x48/apps || true

cat > ./AppRun <<\EOF
#!/bin/sh
cd "$(dirname "${0}")/usr"
export BABL_PATH=./lib/x86_64-linux-gnu/babl-0.1/
export GEGL_PATH=./lib/x86_64-linux-gnu/gegl-0.3/
export LD_LIBRARY_PATH="./lib:./lib/x86_64-linux-gnu/:${LD_LIBRARY_PATH}"
export PATH="./bin/:./sbin/:../bin/:../sbin/:${PATH}"
exec gimp-2.9 "$@"
EOF
chmod a+x ./AppRun

# The original desktop file is a bit strange, hence we provide our own
cat > gimp.desktop <<\EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=GIMP
GenericName=Image Editor
Comment=Create images and edit photographs
Exec=gimp
Icon=gimp
Terminal=false
Categories=Graphics;2DGraphics;RasterGraphics;GTK;
StartupNotify=true
MimeType=image/bmp;image/g3fax;image/gif;image/x-fits;image/x-pcx;image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/x-psd;image/x-sgi;image/x-tga;image/x-xbitmap;image/x-xwindowdump;image/x-xcf;image/x-compressed-xcf;image/x-gimp-gbr;image/x-gimp-pat;image/x-gimp-gih;image/tiff;image/jpeg;image/x-psp;application/postscript;image/png;image/x-icon;image/x-xpixmap;image/x-exr;image/svg+xml;image/x-wmf;image/jp2;image/jpeg2000;image/jpx;image/x-xcursor;
EOF

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

mv ./lib/x86_64-linux-gnu/* ./usr/lib/x86_64-linux-gnu/ ; rm -rf ./lib

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

# Make sure complaints about the packaging don't go to the upstream packager
sed -i -e 's|Packaged by Otto Meier|Bundled as an AppImage|g' usr/bin/gimp-2.9

# Python interpreter from the host system (TODO: Bundle our own)
sed -i -e 's|././|/usr|g' ./usr/lib/gimp/2.0/interpreters/pygimp.interp

# Need to patch etc, otherwise it does not find its defaults; found with "grep -r /etc/gimp"
sed -i -e 's|/etc/gimp|./et/gimp|g' ./usr/bin/gimp-2.9
sed -i -e 's|/etc/gimp|./et/gimp|g' ./usr/lib/libgimpbase-*
# TODO: Most likely there are more instances, as I am still missing the proper theming and palettes
# and something is still trying to access /etc/gimp when doing
# before running AppRun: sudo rm -rf /home/me/.local/share/applications/
mv etc/ usr/et

delete_blacklisted

# Workaround for:
# ImportError: /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0: undefined symbol: XRRGetMonitors
cp $(ldconfig -p | grep libgdk-x11-2.0.so.0 | cut -d ">" -f 2 | xargs) ./usr/lib/
cp $(ldconfig -p | grep libgtk-x11-2.0.so.0 | cut -d ">" -f 2 | xargs) ./usr/lib/

VERSION=$(find /var/cache/apt/archives/ -name gimp_* | head -n 1 | cut -d "~" -f 1 | cut -d "_" -f 2 | cut -d "-" -f 1)
echo $VERSION

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
