/ templates / AM-SAMPLE-pkg2appimage
AM-SAMPLE-pkg2appimage
  1  #!/bin/sh
  2  
  3  set -u
  4  APP=SAMPLE
  5  
  6  # CREATE DIRECTORIES AND ADD REMOVER
  7  [ -n "$APP" ] && mkdir -p "/opt/$APP/tmp" "/opt/$APP/icons" && cd "/opt/$APP/tmp" || exit 1
  8  printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > "/opt/$APP/remove"
  9  printf '\n%s' "rm -f /usr/local/share/applications/$APP-AM.desktop" >> "/opt/$APP/remove"
 10  chmod a+x "/opt/$APP/remove"
 11  
 12  # DOWNLOADING THE DEPENDENCIES
 13  wget -q "$(curl -Ls https://api.github.com/repos/probonopd/go-appimage/releases | grep -v zsync | grep -i continuous | grep -i appimagetool | grep -i "$(uname -m)" | grep browser_download_url | cut -d '"' -f 4 | head -1)" -O appimagetool
 14  wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage
 15  chmod a+x ./appimagetool ./pkg2appimage
 16  
 17  # CREATING THE APPIMAGE
 18  echo "app: SAMPLE
 19  binpatch: true
 20  
 21  ingredients:
 22    dist: oldstable
 23    sources:
 24      - deb http://deb.debian.org/debian/ oldstable main contrib non-free
 25      - deb http://deb.debian.org/debian-security/ oldstable-security main contrib non-free
 26      - deb http://deb.debian.org/debian oldstable-updates main contrib non-free
 27      - deb http://deb.debian.org/debian oldstable-backports main contrib non-free
 28    packages:
 29      - SAMPLE" >> recipe.yml
 30  
 31  cp /opt/"$APP"/tmp/recipe.yml /opt/"$APP"/recipe.yml
 32  
 33  ./pkg2appimage ./recipe.yml
 34  
 35  # CLEAN METAINFO DIRECTORY
 36  metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
 37  if [ -z "$metainfodir" ]; then
 38  	return
 39  else
 40  	cd "$metainfodir" || return
 41  	rm -R -f ./*.xml
 42  	cd - > /dev/null || return
 43  fi
 44  
 45  # ...EXPORT THE APPDIR TO AN APPIMAGE!
 46  ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
 47  underscore=_
 48  mkdir version
 49  mv ./"$APP"/"$APP""$underscore"*.deb ./version/
 50  version=$(ls /opt/"$APP"/tmp/version)
 51  echo "$version" >> /opt/"$APP"/version
 52  
 53  cd ..
 54  mv ./tmp/*.AppImage ./"$APP"
 55  chmod a+x ./"$APP"
 56  
 57  rm -R -f ./tmp
 58  
 59  # LINK
 60  ln -s /opt/"$APP"/"$APP" /usr/local/bin/"$APP"
 61  
 62  # SCRIPT TO UPDATE THE PROGRAM
 63  cat >> /opt/"$APP"/AM-updater << 'EOF'
 64  #!/bin/sh
 65  APP=SAMPLE
 66  initial=$(echo $APP | head -c 1)
 67  version0=$(cat /opt/"$APP"/version)
 68  url="http://http.us.debian.org/debian/pool/main/$initial/"$APP"/$version0"
 69  if curl --output /dev/null --silent --head --fail "$url"; then
 70  	echo "Update not needed, exit!"
 71  else
 72  	notify-send "A new version of $APP is available, please wait!"
 73  	mkdir /opt/"$APP"/tmp
 74  	cd /opt/"$APP"/tmp || exit 1
 75  	wget -q $(curl -Ls https://api.github.com/repos/probonopd/go-appimage/releases | grep -v zsync | grep -i continuous | grep -i appimagetool | grep -i "$(uname -m)" | grep browser_download_url | cut -d '"' -f 4 | head -1) -O appimagetool
 76  	wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage # 64 BIT ONLY (comment to disable)
 77  	chmod a+x ./appimagetool ./pkg2appimage
 78  	cp /opt/"$APP"/recipe.yml /opt/"$APP"/tmp/recipe.yml
 79  	./pkg2appimage ./recipe.yml
 80  	metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
 81  	if [ -z "$metainfodir" ]; then
 82  		return
 83  	else
 84  		cd "$metainfodir" || return
 85  		rm -R -f ./*.xml
 86  		cd - > /dev/null || return
 87  	fi
 88  	ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
 89  	underscore=_
 90  	mkdir version
 91  	mv ./"$APP"/"$APP""$underscore"*.deb ./version/
 92  	cd ..
 93  	version=$(ls /opt/"$APP"/tmp/version)
 94  	if test -f ./tmp/version/$version; then rm -R -f ./version
 95  	fi
 96  	echo "$version" >> /opt/"$APP"/version
 97  	mv ./tmp/*.AppImage ./"$APP";
 98  	chmod a+x ./"$APP"
 99  	rm -R -f ./tmp
100  	notify-send "$APP is updated!"
101  fi
102  EOF
103  chmod a+x /opt/"$APP"/AM-updater
104  
105  # LAUNCHER & ICON
106  cd "/opt/$APP" || exit 1
107  ./"$APP" --appimage-extract *.desktop 1>/dev/null && mv ./squashfs-root/*.desktop ./"$APP".desktop
108  ./"$APP" --appimage-extract .DirIcon 1>/dev/null && mv ./squashfs-root/.DirIcon ./DirIcon
109  COUNT=0
110  while [ "$COUNT" -lt 10 ]; do # Tries to get the actual icon/desktop if it is a symlink to another symlink
111  	if [ -L ./"$APP".desktop ]; then
112  		LINKPATH=$(readlink ./"$APP".desktop)
113  		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./"$APP".desktop
114  	fi
115  	if [ -L ./DirIcon ]; then
116  		LINKPATH=$(readlink ./DirIcon)
117  		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./DirIcon
118  	fi
119  	[ ! -L ./"$APP".desktop ] && [ ! -L ./DirIcon ] && break
120  	COUNT=$((COUNT + 1))
121  done
122  sed -i "s#Exec=[^ ]*#Exec=$APP#g; s#Icon=.*#Icon=/opt/$APP/icons/$APP#g" ./"$APP".desktop
123  mv ./"$APP".desktop /usr/local/share/applications/"$APP"-AM.desktop && mv ./DirIcon ./icons/"$APP" 1>/dev/null
124  rm -R -f ./squashfs-root