#!/bin/sh

APP=zmninja
APPNAME=zmNinja

CATEGORIES=$(curl -L -s https://raw.githubusercontent.com/AppImage/appimage.github.io/master/apps/$APPNAME.md | grep -E -i categories | cut -c 17-)
ICONURL="https://appimage.github.io/database/$(curl -L -s https://raw.githubusercontent.com/AppImage/appimage.github.io/master/apps/$APPNAME.md | grep -E -i icons | sed -n 2p | cut -c 5-)"
REPO="ZoneMinder/zmNinja"
USER=$(echo $REPO | sed 's:/[^/]*$::')
REPO2=$(echo $REPO | sed 's:.*/::')
FILENAMEEXTENTION="arm64.AppImage"
URL=https://github.com/$REPO/releases/latest
COMMENT=$(curl -L -s https://raw.githubusercontent.com/AppImage/appimage.github.io/master/apps/$APPNAME.md | grep "Comment:" | cut -c 14-)

# CREATE THE FOLDER
mkdir /opt/$APP
cd /opt/$APP

# ADD THE REMOVER
echo '#!/bin/sh' >> /opt/$APP/remove
echo "rm -R -f /usr/local/share/applications/$APP-AM.desktop /opt/$APP /usr/local/bin/$APP" >> /opt/$APP/remove
chmod a+x /opt/$APP/remove

# DOWNLOAD THE APPIMAGE
mkdir tmp
cd ./tmp

wget https://github.com/$(wget https://github.com/$REPO/releases/latest | grep -E '/.*/.*'$FILENAMEEXTENTION'' -o);

version=$(curl -Ls https://api.github.com/repos/$REPO/releases/latest | grep -E tag_name | awk -F '[""]' '{print $4}')
echo "$version" >> /opt/$APP/version

cd ..
mv ./tmp/*mage ./$APP
chmod a+x /opt/$APP/$APP
rmdir ./tmp

# LINK
ln -s /opt/$APP/$APP /usr/local/bin/$APP

# SCRIPT TO UPDATE THE PROGRAM
cat >> /opt/$APP/AM-updater << 'EOF'
#!/usr/bin/env bash
APP=zmninja
version0=$(cat /opt/$APP/version)
url=https://github.com/FUNCTION2/FUNCTION3/releases/latest
if curl -L -s $url | grep -ioF "$version0"; then
  echo "Update not needed!"
else
  notify-send "A new version of $APP is available, please wait"
  mkdir /opt/$APP/tmp
  cd /opt/$APP/tmp
  URL=https://github.com/FUNCTION2/FUNCTION3/releases/latest
  wget https://github.com/$(wget https://github.com/FUNCTION2/FUNCTION3/releases/latest | grep -E '/.*/.*'FUNCTION4'' -o);
  version=$(curl -Ls https://api.github.com/repos/FUNCTION2/FUNCTION3/releases/latest | grep -E tag_name | awk -F '[""]' '{print $4}')
  cd ..
  if test -f ./tmp/*mage; then rm ./version
  fi
  echo $version >> ./version
  mv --backup=t ./tmp/*mage ./$APP
  chmod a+x /opt/$APP/$APP
  rm -R -f ./tmp ./*~
fi
EOF
sed -i s/FUNCTION2/$USER/g /opt/$APP/AM-updater
sed -i s/FUNCTION3/$REPO2/g /opt/$APP/AM-updater
sed -i s/FUNCTION4/$FILENAMEEXTENTION/g /opt/$APP/AM-updater
chmod a+x /opt/$APP/AM-updater

# LAUNCHER
rm -f /usr/local/share/applications/$APP-AM.desktop
echo "[Desktop Entry]
Name=$APPNAME
Exec=$APP
Icon=/opt/$APP/icons/$APP
Type=Application
Terminal=false
Categories=$CATEGORIES;
Comment=$COMMENT" >> /usr/local/share/applications/$APP-AM.desktop

# ICON
mkdir ./icons
wget $ICONURL -O /opt/$APP/icons/$APP
