/ templates / AM-SAMPLE-pkg2appimage-custom
AM-SAMPLE-pkg2appimage-custom
  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: APPDIR FROM A RECIPE...
 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  # ...DOWNLOADING LIBUNIONPRELOAD...
 36  #wget https://github.com/project-portable/libunionpreload/releases/download/amd64/libunionpreload.so
 37  #chmod a+x libunionpreload.so
 38  #mv ./libunionpreload.so ./$APP/$APP.AppDir/
 39  
 40  # ...REPLACING THE EXISTING APPRUN WITH A CUSTOM ONE...
 41  rm -R -f ./"$APP"/"$APP".AppDir/AppRun
 42  cat > AppRun << 'EOF'
 43  #!/bin/sh
 44  APP=SAMPLE
 45  HERE="$(dirname "$(readlink -f "${0}")")"
 46  export UNION_PRELOAD="${HERE}"
 47  #export LD_PRELOAD="${HERE}/libunionpreload.so"
 48  export PATH="${HERE}"/usr/bin/:"${HERE}"/usr/sbin/:"${HERE}"/usr/games/:"${HERE}"/bin/:"${HERE}"/sbin/:"${PATH}"
 49  ##export LD_LIBRARY_PATH="${HERE}"/usr/lib/:"${HERE}"/usr/lib/i386-linux-gnu/:"${HERE}"/usr/lib/x86_64-linux-gnu/:"${HERE}"/lib/:"${HERE}"/lib/i386-linux-gnu/:"${HERE}"/lib/x86_64-linux-gnu/:"${LD_LIBRARY_PATH}"
 50  #export LD_LIBRARY_PATH=/lib/:/lib64/:/lib/x86_64-linux-gnu/:/usr/lib/:"${HERE}"/usr/lib/:"${HERE}"/usr/lib/i386-linux-gnu/:"${HERE}"/usr/lib/x86_64-linux-gnu/:"${HERE}"/lib/:"${HERE}"/lib/i386-linux-gnu/:"${HERE}"/lib/x86_64-linux-gnu/:"${LD_LIBRARY_PATH}"
 51  export PYTHONPATH="${HERE}"/usr/share/pyshared/:"${PYTHONPATH}"
 52  export PYTHONHOME="${HERE}"/usr/
 53  export XDG_DATA_DIRS="${HERE}"/usr/share/:"${XDG_DATA_DIRS}"
 54  export PERLLIB="${HERE}"/usr/share/perl5/:"${HERE}"/usr/lib/perl5/:"${PERLLIB}"
 55  export GSETTINGS_SCHEMA_DIR="${HERE}"/usr/share/glib-2.0/schemas/:"${GSETTINGS_SCHEMA_DIR}"
 56  export QT_PLUGIN_PATH="${HERE}"/usr/lib/qt4/plugins/:"${HERE}"/usr/lib/i386-linux-gnu/qt4/plugins/:"${HERE}"/usr/lib/x86_64-linux-gnu/qt4/plugins/:"${HERE}"/usr/lib32/qt4/plugins/:"${HERE}"/usr/lib64/qt4/plugins/:"${HERE}"/usr/lib/qt5/plugins/:"${HERE}"/usr/lib/i386-linux-gnu/qt5/plugins/:"${HERE}"/usr/lib/x86_64-linux-gnu/qt5/plugins/:"${HERE}"/usr/lib32/qt5/plugins/:"${HERE}"/usr/lib64/qt5/plugins/:"${QT_PLUGIN_PATH}"
 57  EXEC=$(grep -e '^Exec=.*' "${HERE}"/*.desktop | head -n 1 | cut -d "=" -f 2- | sed -e 's|%.||g')
 58  ##exec ${EXEC} "$@"
 59  #exec "${HERE}"CUSTOMPATH "$@"
 60  EOF
 61  chmod a+x AppRun
 62  cp ./AppRun /opt/"$APP"/
 63  mv ./AppRun ./"$APP"/"$APP".AppDir
 64  
 65  # IMPORT THE LAUNCHER AND THE ICON TO THE APPDIR IF THEY NOT EXIST
 66  if test -f ./"$APP"/"$APP".AppDir/*.desktop; then
 67  	echo "The desktop file exists"
 68  else
 69  	echo "Trying to get the .desktop file"
 70  	cp ./"$APP"/"$APP".AppDir/usr/share/applications/*"$(ls . | grep -i "$APP" | cut -c -4)"*desktop ./"$APP"/"$APP".AppDir/ 2>/dev/null
 71  fi
 72  
 73  ICONNAME=$(cat ./"$APP"/"$APP".AppDir/*desktop | grep "Icon=" | head -1 | cut -c 6-)
 74  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/22x22/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 75  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/24x24/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 76  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/32x32/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 77  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/48x48/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 78  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/64x64/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 79  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/128x128/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 80  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/256x256/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 81  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/512x512/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 82  cp ./"$APP"/"$APP".AppDir/usr/share/icons/hicolor/scalable/apps/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 83  cp ./"$APP"/"$APP".AppDir/usr/share/applications/*"$ICONNAME"* ./"$APP"/"$APP".AppDir/ 2>/dev/null
 84  
 85  # CLEAN METAINFO DIRECTORY
 86  metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
 87  if [ -z "$metainfodir" ]; then
 88  	return
 89  else
 90  	cd "$metainfodir" || return
 91  	rm -R -f ./*.xml
 92  	cd - > /dev/null || return
 93  fi
 94  
 95  # ...EXPORT THE APPDIR TO AN APPIMAGE!
 96  ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
 97  underscore=_
 98  mkdir version
 99  mv ./"$APP"/"$APP""$underscore"*.deb ./version/
100  version=$(ls /opt/"$APP"/tmp/version)
101  echo "$version" >> /opt/"$APP"/version
102  
103  cd ..
104  mv ./tmp/*.AppImage ./"$APP"
105  chmod a+x ./"$APP"
106  rm -R -f ./tmp
107  
108  # LINK
109  ln -s /opt/"$APP"/"$APP" /usr/local/bin/"$APP"
110  
111  # SCRIPT TO UPDATE THE PROGRAM
112  cat >> /opt/"$APP"/AM-updater << 'EOF'
113  #!/bin/sh
114  APP=SAMPLE
115  initial=$(echo $APP | head -c 1)
116  version0=$(cat /opt/"$APP"/version)
117  url="http://http.us.debian.org/debian/pool/main/$initial/"$APP"/$version0"
118  if curl --output /dev/null --silent --head --fail "$url"; then
119  	echo "Update not needed, exit!"
120  else
121  	notify-send "A new version of $APP is available, please wait!"
122  	mkdir /opt/"$APP"/tmp
123  	cd /opt/"$APP"/tmp || exit 1
124  	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
125  	wget https://raw.githubusercontent.com/ivan-hc/AM-application-manager/main/tools/pkg2appimage # 64 BIT ONLY (comment to disable)
126  	chmod a+x ./appimagetool ./pkg2appimage
127  	cp /opt/"$APP"/recipe.yml /opt/"$APP"/tmp/recipe.yml
128  	./pkg2appimage ./recipe.yml
129  	#wget https://github.com/project-portable/libunionpreload/releases/download/amd64/libunionpreload.so
130  	#chmod a+x libunionpreload.so
131  	#mv ./libunionpreload.so ./"$APP"/"$APP".AppDir/
132  	rm -R -f ./"$APP"/"$APP".AppDir/AppRun
133  	cp /opt/"$APP"/AppRun ./"$APP"/"$APP".AppDir/
134  	metainfodir=$(find ./"$APP"/"$APP".AppDir -type d -name metainfo | grep "share/metainfo" | head -1)
135  	if [ -z "$metainfodir" ]; then
136  		return
137  	else
138  		cd "$metainfodir" || return
139  		rm -R -f ./*.xml
140  		cd - > /dev/null || return
141  	fi
142  	ARCH=$(uname -m) VERSION=$(./appimagetool -v | grep -o '[[:digit:]]*') ./appimagetool -s ./"$APP"/"$APP".AppDir
143  	underscore=_
144  	mkdir version
145  	mv ./"$APP"/"$APP""$underscore"*.deb ./version/
146  	cd ..
147  	version=$(ls /opt/"$APP"/tmp/version)
148  	if test -f ./tmp/version/$version; then rm -R -f ./version
149  	fi
150  	echo "$version" >> /opt/"$APP"/version
151  	mv ./tmp/*.AppImage ./"$APP"
152  	chmod a+x ./"$APP"
153  	rm -R -f ./tmp
154  	notify-send "$APP is updated!"
155  fi
156  EOF
157  chmod a+x /opt/"$APP"/AM-updater
158  
159  # LAUNCHER & ICON
160  cd "/opt/$APP" || exit 1
161  ./"$APP" --appimage-extract *.desktop 1>/dev/null && mv ./squashfs-root/*.desktop ./"$APP".desktop
162  ./"$APP" --appimage-extract .DirIcon 1>/dev/null && mv ./squashfs-root/.DirIcon ./DirIcon
163  COUNT=0
164  while [ "$COUNT" -lt 10 ]; do # Tries to get the actual icon/desktop if it is a symlink to another symlink
165  	if [ -L ./"$APP".desktop ]; then
166  		LINKPATH=$(readlink ./"$APP".desktop)
167  		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./"$APP".desktop
168  	fi
169  	if [ -L ./DirIcon ]; then
170  		LINKPATH=$(readlink ./DirIcon)
171  		./"$APP" --appimage-extract "$LINKPATH" && mv ./squashfs-root/"$LINKPATH" ./DirIcon
172  	fi
173  	[ ! -L ./"$APP".desktop ] && [ ! -L ./DirIcon ] && break
174  	COUNT=$((COUNT + 1))
175  done
176  sed -i "s#Exec=[^ ]*#Exec=$APP#g; s#Icon=.*#Icon=/opt/$APP/icons/$APP#g" ./"$APP".desktop
177  mv ./"$APP".desktop /usr/local/share/applications/"$APP"-AM.desktop && mv ./DirIcon ./icons/"$APP" 1>/dev/null
178  rm -R -f ./squashfs-root