#!/bin/sh

APP=gimp

mkdir tmp
cd tmp

# DOWNLOADING THE DEPENDENCIES
wget https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage -O appimagetool
wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage
chmod a+x ./appimagetool ./pkg2appimage

# CREATING THE APPIMAGE
echo "app: gimp
binpatch: true

ingredients:
  dist: bionic
  sources:
    - deb http://archive.ubuntu.com/ubuntu/ bionic main universe restricted multiverse
    - deb http://archive.ubuntu.com/ubuntu bionic-security main universe restricted multiverse
    - deb http://archive.ubuntu.com/ubuntu/ bionic-updates main universe restricted multiverse
  ppas:
    - savoury1/ffmpeg4
    - savoury1/gimp
    - savoury1/graphics
    - savoury1/multimedia
    - savoury1/build-tools
    - savoury1/backports
  packages:
    - gimp
    - gimp-data
    - gimp-data-extras
    - gimp-help-common" >> recipe.yml

./pkg2appimage ./recipe.yml

rm -R -f ./$APP/$APP.AppDir/AppRun
wget https://raw.githubusercontent.com/ivan-hc/GIMP-appimage/main/AppRun -P ./$APP/$APP.AppDir/
chmod a+x ./$APP/$APP.AppDir/AppRun

ARCH=x86_64 ./appimagetool -n ./$APP/$APP.AppDir
VERSION=$(curl -sL https://ppa.launchpadcontent.net/savoury1/gimp/ubuntu/pool/main/g/gimp/ | grep -Po '(?<=href=")[^"]*(?=")' | grep gimp_ | grep 18.04 | head -1 | cut -f1 -d "-" | grep -Eo "[0-9]+([.][0-9]+)?+[+-]?[0-9]?")

cd ..
mv ./tmp/*.AppImage ./GNU_Image_Manipulation_Program-$VERSION-x86_64.AppImage

rm -R -f ./tmp
