#!/bin/sh

APP=gimp-dev

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
PREVIOUSLTS=$(wget -q https://releases.ubuntu.com/ -O - | grep class | grep LTS | grep -m2 href | tail -n1 | sed -n 's/.*href="\([^"]*\).*/\1/p' | rev| cut -c 2- | rev)
echo "app: gimp
binpatch: true

ingredients:
  dist: $PREVIOUSLTS
  sources:
    - deb http://archive.ubuntu.com/ubuntu/ $PREVIOUSLTS main universe restricted multiverse
    - deb http://archive.ubuntu.com/ubuntu $PREVIOUSLTS-security main universe restricted multiverse
    - deb http://archive.ubuntu.com/ubuntu/ $PREVIOUSLTS-updates main universe restricted multiverse
  ppas:
    - mati75/gimp30
  packages:
    - gimp
    - gimp-data
    - gir1.0-gimp-3.0
    - libgimp3.0
    - libgimp3.0-doc
    - libgimp3.0-dev" >> recipe.yml

./pkg2appimage ./recipe.yml

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

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

cd ..
mv ./tmp/*.AppImage ./GNU_Image_Manipulation_Program-DEV-$DEVERSION-x86_64.AppImage

rm -R -f ./tmp