management.am 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #!/usr/bin/env bash
  2. ##############################################################################################
  3. # THIS MODULE INCLUDES ALL THE ACTIONS INTENDED TO MANAGE THE APPS INSTALLED FROM THE DATABASE
  4. # AND ONE OPTION IS FOR LOCAL APPIMAGES INSTEAD
  5. # OPTIONS: BACKUP/RESTORE, DOWNGRADE, LAUNCHER, LOCK/UNLOCK, NOLIBFUSE, REMOVE
  6. ##############################################################################################
  7. # BACKUP
  8. function _backup_name() {
  9. printf "\n ◆ To set date and time as a name, press ENTER (default)\n ◆ To set the version as a name, press \"1\"\n ◆ To set a custom name, write anything else\n\n"
  10. read -r -p " Write your choice here, or leave blank to use \"date/time\": " response
  11. case "$response" in
  12. '')
  13. backupname=$(date +%F-%X | sed 's/://g' | sed 's/-//g')
  14. ;;
  15. '1')
  16. _check_version
  17. backupname=$(grep -w " ◆ $app_name |" "$AMCACHEDIR"/version-args 2>/dev/null | sed 's:.*| ::')
  18. ;;
  19. *)
  20. backupname="$(echo "$response" | sed 's/ /_/g')"
  21. ;;
  22. esac
  23. }
  24. function _backup() {
  25. if [ ! -f "$argpath"/remove ]; then
  26. echo " \"$2\" is not a valid argument or is not installed."
  27. else
  28. read -r -p " Do you wish to backup the current version of $2? (y/N) " yn
  29. if ! echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  30. printf "\n OPERATION ABORTED!\n\n"
  31. else
  32. mkdir -p "$HOME/.am-snapshots/$2"
  33. app_name="$2"
  34. _backup_name
  35. if test -d "$HOME/.am-snapshots/$2/$backupname"; then
  36. echo " 💀 ERROR: \"$2/$backupname\" already exists, ABORTED!"
  37. echo "$DIVIDING_LINE"
  38. return 1
  39. else
  40. cp -r "$argpath" "$HOME/.am-snapshots/$2/$backupname"
  41. fi
  42. echo " SAVED in $HOME/.am-snapshots/$2/$backupname"
  43. fi
  44. fi
  45. echo "$DIVIDING_LINE"
  46. }
  47. # RESTORE
  48. function _overwrite() {
  49. if [ ! -d "$HOME/.am-snapshots/$2" ]; then
  50. echo " ERROR: No snapshot for \"$2\" found."
  51. elif [ ! -f "$argpath"/remove ]; then
  52. echo " \"$2\" is not a valid argument or is not installed."
  53. else
  54. read -r -p " Do you wish to overwrite $2 with an older version? (y,N) " yn
  55. if ! echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  56. printf "\n OPERATION ABORTED! \n\n"
  57. else
  58. printf "\n Please, select a snapshot or press CTRL+C to abort:\n\n"
  59. sleep 1
  60. select d in "$HOME/.am-snapshots/$2"/*; do
  61. test -n "$d" && break
  62. echo ">>> Invalid Selection"
  63. done
  64. cp -r --backup=t "$d"/* "$argpath"/
  65. rm -R -f ./tmp "$argpath"/*~
  66. echo " RESTORE COMPLETED SUCCESSFULLY!"
  67. fi
  68. fi
  69. echo "$DIVIDING_LINE"
  70. }
  71. # DOWNGRADE
  72. function _downgrade_entries_filter() {
  73. grep -vi "^#\|version0=" ./AM-updater | grep "api.github.com" \
  74. | tr '=' '\n' | sed 's/^\$(//g' \
  75. | tail -1 | sed '1,${
  76. s/version=$(//g
  77. s/head -1)/head -100/g
  78. s#$REPO#'"$REPO"'#g
  79. s#releases/latest#releases#g
  80. s#releases #releases?per_page=100 #g
  81. s#/$tag/$app##g
  82. }'
  83. }
  84. function _downgrade() {
  85. # Safety checks
  86. if [ ! -f "$argpath"/AM-updater ]; then
  87. echo " No AM-updater available for \"$2\", cannot detect any URL!"
  88. return 1
  89. elif [ -f "$argpath"/"$2"-rollback ]; then
  90. cd "$argpath" || return 1
  91. ./"$2"-rollback || return 1
  92. return 0
  93. elif ! grep -q "api.github.com" "$argpath"/AM-updater; then
  94. echo " The option \"$1\" only works for https://github.com"
  95. return 1
  96. fi
  97. cd "$argpath" || return 1
  98. entries_list=$(_downgrade_entries_filter)
  99. urls="$(eval "$entries_list" 2>/dev/null | grep -vi "zsync$")"
  100. if ! echo "$urls" | grep -q "[0-9]"; then
  101. echo " ERROR: No valid links found, sorry!"
  102. exit 1
  103. fi
  104. echo "-----------------------------------------------------------------------"
  105. echo "You have chosen to roll back \"$2\"."
  106. echo "-----------------------------------------------------------------------"
  107. echo "Select a URL from this menu (read carefully) or press CTRL+C to abort:"
  108. echo "-----------------------------------------------------------------------"
  109. sleep 1
  110. select d in $urls; do
  111. test -n "$d" && break
  112. echo ">>> Invalid Selection"
  113. done
  114. cp ./AM-updater ./AM-rollback
  115. sed -i -e 's/version0/#version0/g' \
  116. -e 's/appimageupdatetool -Or/#appimageupdatetool -Or/g' ./AM-rollback
  117. [ -f ./"$2".zsync ] && mv ./"$2".zsync ./"$2".zsync.old
  118. if wget --version | head -1 | grep -q ' 1.'; then
  119. sed -i '/wget/c\wget -q --show-progress '"$d"'' ./AM-rollback
  120. else
  121. sed -i '/wget/c\wget '"$d"'' ./AM-rollback
  122. fi
  123. ./AM-rollback && rm -f ./AM-rollback || return 1
  124. mv ./"$2".zsync.old ./"$2".zsync 2>/dev/null
  125. echo "$d" > ./version
  126. echo "ROLLBACK SUCCESSFUL!"
  127. }
  128. # ICON THEME CHANGER
  129. function _icon_theme() {
  130. if [ "$AMCLI" = am ] && test -f /usr/local/share/applications/"$arg"*AM.desktop; then
  131. $SUDOCMD sed -i "s#Icon=$argpath/icons/#Icon=#g" /usr/local/share/applications/"$arg"*AM.desktop 2>/dev/null
  132. else
  133. sed -i "s#Icon=$argpath/icons/#Icon=#g" "$DATADIR"/applications/"$arg"*AM.desktop 2>/dev/null
  134. fi
  135. _icon_theme_export_to_datadir 2>/dev/null
  136. }
  137. # LAUNCHER
  138. function _launcher_appimage_extract() {
  139. "$arg" --appimage-extract share/icons/*/*/* 1>/dev/null
  140. "$arg" --appimage-extract usr/share/icons/*/*/* 1>/dev/null
  141. "$arg" --appimage-extract share/icons/*/*/*/* 1>/dev/null
  142. "$arg" --appimage-extract usr/share/icons/*/*/*/* 1>/dev/null
  143. "$arg" --appimage-extract *.svg 1>/dev/null
  144. "$arg" --appimage-extract *.png 1>/dev/null
  145. "$arg" --appimage-extract *.desktop 1>/dev/null
  146. "$arg" --appimage-extract share/applications/*.desktop 1>/dev/null
  147. "$arg" --appimage-extract usr/share/applications/*.desktop 1>/dev/null
  148. }
  149. function _launcher_appimage_integration() {
  150. printf "[Desktop Entry]\nVersion=1.0\nType=Application\nTerminal=false\nStartupNotify=true\nExec=%s\n" "$arg %U" > ./"$appimage".desktop
  151. cat ./squashfs-root/*.desktop | grep "^Name=" | head -1 >> ./"$appimage".desktop
  152. cat ./squashfs-root/*.desktop | grep -F '^Name[' >> ./"$appimage".desktop
  153. cat ./squashfs-root/*.desktop | grep "Categories=" >> ./"$appimage".desktop
  154. cat ./squashfs-root/*.desktop | grep "Comment=" | head -1 >> ./"$appimage".desktop
  155. cat ./squashfs-root/*.desktop | grep -F 'Comment[' >> ./"$appimage".desktop
  156. cat ./squashfs-root/*.desktop | grep "Icon=" | head -1 >> ./"$appimage".desktop
  157. cat ./squashfs-root/*.desktop | grep "MimeType=" | head -1 >> ./"$appimage".desktop
  158. uniq ./"$appimage".desktop > ./"$appimage"-1.desktop && mv ./"$appimage"-1.desktop ./"$appimage".desktop
  159. mv ./"$appimage".desktop "$DATADIR"/applications/AppImages/"$appimage".desktop 2>/dev/null
  160. mv ./squashfs-root/*.png "$DATADIR"/icons/ 2>/dev/null
  161. mv ./squashfs-root/*.svg "$DATADIR"/icons/ 2>/dev/null
  162. share_dirs="usr/share share"
  163. size_dirs="0x0 22x22 24x24 32x32 48x48 64x64 128x128 256x256 512x512"
  164. for share in $share_dirs; do
  165. for sizes in $size_dirs; do
  166. mv ./squashfs-root/"${share}"/icons/hicolor/"${sizes}"/apps/*.png "$DATADIR"/icons/ 2>/dev/null
  167. done
  168. mv ./squashfs-root/"${share}"/icons/hicolor/scalable/apps/*.svg "$DATADIR"/icons/ 2>/dev/null
  169. mv ./squashfs-root/"${share}"/pixmaps/*.png "$DATADIR"/icons/ 2>/dev/null
  170. done
  171. rm -Rf ./squashfs-root
  172. }
  173. function _launcher_appimage_bin() {
  174. mkdir -p "$HOME"/.local/bin
  175. _check_if_home_local_bin_is_not_in_path
  176. read -r -p " Write a custom command to launch the app, or leave blank: " response
  177. if [ -z "$response" ]; then
  178. appimage_cmd=$(echo "$appimage" | tr '[:upper:]' '[:lower:]')
  179. if ! echo "$appimage" | grep -q -i ".appimage"; then
  180. printf '#!/bin/sh\n%s' "$arg" >> "$HOME"/.local/bin/"$appimage_cmd".appimage
  181. chmod a+x "$HOME"/.local/bin/"$appimage_cmd".appimage
  182. echo " New command: \"$appimage_cmd.appimage\" in ~/.local/bin"
  183. else
  184. printf '#!/bin/sh\n%s' "$arg" >> "$HOME"/.local/bin/"$appimage_cmd"
  185. chmod a+x "$HOME"/.local/bin/"$appimage_cmd"
  186. echo " New command: \"$appimage_cmd\" in ~/.local/bin"
  187. fi
  188. elif command -v "$response" 1>/dev/null; then
  189. echo " ERROR: the \"$response\" command alredy exists, retry!"
  190. else
  191. ln -s "$arg" "$HOME"/.local/bin/"$response"
  192. fi
  193. }
  194. function _launcher(){
  195. if ! test -f "$arg"; then
  196. echo " ERROR: \"$arg\" not found"
  197. elif ! strings -d "$arg" 2>/dev/null | grep -F -q 'if you run it with the --appimage-extract option'; then
  198. echo " ERROR: \"$arg\" is NOT an AppImage"
  199. else
  200. printf " ◆ File: %s\n" "$arg"
  201. appimage=$(basename -- "$arg")
  202. mkdir -p "$DATADIR"/applications/AppImages
  203. mkdir -p "$DATADIR"/icons
  204. chmod a+x "$arg"
  205. cd "$(dirname "$arg")" || return
  206. _launcher_appimage_extract 2>/dev/null
  207. _launcher_appimage_integration 2>/dev/null
  208. _launcher_appimage_bin
  209. fi
  210. }
  211. # LOCK/UNLOCK
  212. function _lock() {
  213. if [ ! -f "$argpath"/AM-updater ]; then
  214. echo " \"$AMCLIUPPER\" cannot manage updates for $2, \"AM-updater\" file not found!"
  215. return 1
  216. fi
  217. read -r -p " Do you wish to keep $2 at its current version? (y/N) " yn
  218. if echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  219. mv "$argpath"/AM-updater "$argpath"/AM-LOCK 1>/dev/null
  220. echo " $2 has been locked at current version!"
  221. else
  222. echo " Operation aborted!"
  223. return 1
  224. fi
  225. }
  226. function _unlock() {
  227. if ! test -f "$argpath"/AM-LOCK; then
  228. echo " \"$2\" cannot be unlocked, \"AM-LOCK\" file not found!"
  229. return 1
  230. fi
  231. read -r -p " Do you wish to unlock updates for $2? (Y/n) " yn
  232. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  233. echo " $2 is still locked at current version!"
  234. return 1
  235. else
  236. mv "$argpath"/AM-LOCK "$argpath"/AM-updater 1>/dev/null
  237. echo " \"$2\" can now receive updates!"
  238. fi
  239. }
  240. # NO LIBFUSE
  241. function _nolibfuse() {
  242. AMCLIPATH_ORIGIN="$AMCLIPATH"
  243. target="$(echo "${2}" | tr '[:lower:]' '[:upper:]')"
  244. # safety checks
  245. if ! cd "$argpath" 2>/dev/null; then
  246. echo " ⚠️ Error: \"$target\" is NOT installed."
  247. return 1
  248. fi
  249. string="$(strings -d "./$2" 2>/dev/null)"
  250. if ! echo "$string" | grep -q -- 'run it with the --appimage-extract'; then
  251. echo " ⚠️ Error: $target is NOT an AppImage."
  252. return 1
  253. elif ! echo "$string" | grep -q -- 'AppImages require FUSE to run'; then
  254. echo " ◆ $target is already a new generation AppImage."
  255. return 1
  256. elif test -f ./*.zsync; then
  257. echo "-----------------------------------------------------------------------"
  258. echo " Warning! Your AppImage uses \"zsync\" to update."
  259. echo " The .zsync file will be removed and will no longer work"
  260. echo " your \"AM-updater\" will likely still be able to update the AppImage"
  261. echo " by comparing the old vs new version url, but it is not guaranteed"
  262. read -r -p " Do you want to proceede anyway? (N/y) " yn
  263. if ! echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  264. return 1
  265. fi
  266. fi
  267. appimagetool="https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-$ARCH.AppImage"
  268. printf " ...downloading appimagetool\r"
  269. wget -q "$appimagetool" -O ./appimagetool || return 1
  270. printf '#!/bin/sh\nexit 0' > ./desktop-file-validate # hack due to https://github.com/AppImage/appimagetool/pull/47
  271. chmod a+x ./appimagetool ./desktop-file-validate
  272. printf " ...extracting the AppImage\r"
  273. ./"$2" --appimage-extract >/dev/null 2>&1 && chmod 0755 ./squashfs-root
  274. printf " ...trying to convert in new generation AppImage\r"
  275. PATH="$PATH:$PWD" ARCH="$(uname -m)" ./appimagetool -n ./squashfs-root >/dev/null 2>&1
  276. if ! test -f ./*.AppImage; then
  277. echo " 💀Error when trying to convert $target. Operation Aborted."
  278. rm -R -f ./appimagetool ./squashfs-root ./desktop-file-validate
  279. return 1
  280. fi
  281. mv ./"$2" ./"$2".old && mv ./*.AppImage ./"$2" || return 1
  282. echo " ◆ $target has been converted to a new generation AppImage."
  283. rm -rf ./appimagetool ./squashfs-root ./desktop-file-validate ./*.zsync
  284. if [ -f ./AM-updater ] && ! grep -q 'nolibfuse' ./AM-updater; then
  285. sed -i "s/^else/ echo y | $AMCLIPATH_ORIGIN nolibfuse \"\$APP\"\n notify-send \"\$APP has been converted too\!\"\nelse/g" ./AM-updater 2>/dev/null
  286. echo " The next update may replace this AppImage with a Type2 one"
  287. echo " so I added this command to the bottom of the \"AM-updater\" script!"
  288. fi
  289. echo " Contact the upstream developers to make them officially upgrade!"
  290. read -r -p " Do you wish to remove the old libfuse2 AppImage? (Y/n) " yn
  291. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  292. return 1
  293. else
  294. rm -f ./*.old
  295. fi
  296. }
  297. # REMOVE
  298. function _detect_path_to_remove() {
  299. if [ -d "$APPMAN_APPSPATH"/"$arg" ]; then
  300. RMPATH="1"
  301. elif [ -d "$APPSPATH"/"$arg" ]; then
  302. [ -w "$APPSPATH"/"$arg" ] && $SUDOCMD echo -e "\r" >/dev/null
  303. RMPATH="1"
  304. else
  305. RMPATH=""
  306. fi
  307. echo "$DIVIDING_LINE"
  308. }
  309. function _remove() {
  310. [ "$AMCLI" = am ] && _detect_appman_apps
  311. _detect_path_to_remove
  312. [ -z "$RMPATH" ] && echo -e " \"${RED}$arg\033[0m\" is not a valid \"APPNAME\", see \"$AMCLI -f\" for more." && return 1
  313. read -r -p " ◆ Do you wish to remove \"$arg\"? (Y/n) " yn
  314. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  315. echo -e " \"${LightBlue}$arg\033[0m\" has not been removed!"
  316. else
  317. [ "$AMCLI" = am ] && "$APPMAN_APPSPATH"/"$arg"/remove 2>/dev/null || $SUDOCMD "$APPSPATH"/"$arg"/remove 2>/dev/null || return 1
  318. _clean_amcachedir
  319. sleep 0.1
  320. echo -e " \"${Green}$arg\033[0m\" has been removed!"
  321. fi
  322. [ -d "$DATADIR"/icons/hicolor/scalable/apps ] && find "$DATADIR"/icons/hicolor/scalable/apps -xtype l -exec rm {} \;
  323. }
  324. function _hard_remove() {
  325. [ "$AMCLI" = am ] && _detect_appman_apps
  326. _detect_path_to_remove
  327. [ -z "$RMPATH" ] && echo -e " \"${RED}$arg\033[0m\" is not a valid \"APPNAME\", see \"$AMCLI -f\" for more." && return 1
  328. [ "$AMCLI" = am ] && "$APPMAN_APPSPATH"/"$arg"/remove 2>/dev/null || $SUDOCMD "$APPSPATH"/"$arg"/remove 2>/dev/null || return 1
  329. _clean_amcachedir
  330. sleep 0.1
  331. echo -e " \"${Green}$arg\033[0m\" has been removed!"
  332. [ -d "$DATADIR"/icons/hicolor/scalable/apps ] && find "$DATADIR"/icons/hicolor/scalable/apps -xtype l -exec rm {} \;
  333. }
  334. ###########################################################################
  335. # OPTIONS AVAILABLE IN THIS MODULE
  336. ###########################################################################
  337. # Main logic
  338. [ -z "$2" ] && echo " USAGE: $AMCLI $1 [ARGUMENT]" && exit 1
  339. case "$1" in
  340. 'backup'|'-b')
  341. # Do a snapshot of an installed app
  342. _determine_args
  343. entries="$(echo "$@" | cut -f2- -d ' ')"
  344. for arg in $entries; do
  345. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  346. _backup "${@}"
  347. shift
  348. done
  349. ;;
  350. 'downgrade'|'--rollback')
  351. # Downgrade the installed app to a previous version, from its online source
  352. _online_check
  353. _determine_args
  354. entries="$(echo "$@" | cut -f2- -d ' ')"
  355. for arg in $entries; do
  356. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  357. _downgrade "${@}"
  358. _clean_amcachedir
  359. _update_list_updatable_apps
  360. shift
  361. done
  362. ;;
  363. 'icons'|'--icons')
  364. # Place local AppImages into the menu by dragging and dropping them into the terminal
  365. _determine_args
  366. if [ "$2" = "--all" ]; then
  367. entries="$ARGS"
  368. read -r -p " ◆ Do you wish to allow custom icon theming for ALL the apps? (y,N) " yn
  369. else
  370. entries="$(echo "$@" | cut -f2- -d ' ')"
  371. read -r -p " ◆ Do you wish to allow custom icon theming for the selected apps? (y,N) " yn
  372. fi
  373. if echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  374. for arg in $entries; do
  375. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  376. _icon_theme "${@}"
  377. done
  378. echo " ✔ Success!"
  379. else
  380. echo " ✖ Aborted!"
  381. fi
  382. ;;
  383. 'launcher'|'--launcher')
  384. # Place local AppImages into the menu by dragging and dropping them into the terminal
  385. entries="$(echo "$@" | cut -f2- -d ' ')"
  386. for arg in $entries; do
  387. echo "---------------------------------------------------------------------"
  388. _launcher "${@}"
  389. done
  390. echo "---------------------------------------------------------------------"
  391. ;;
  392. 'lock')
  393. # Lock the version of an installed app
  394. _determine_args
  395. entries="$(echo "$@" | cut -f2- -d ' ')"
  396. for arg in $entries; do
  397. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  398. _lock "${@}"
  399. shift
  400. done
  401. ;;
  402. 'unlock')
  403. # Unlock the version of an installed app
  404. _determine_args
  405. entries="$(echo "$@" | cut -f2- -d ' ')"
  406. for arg in $entries; do
  407. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  408. _unlock "${@}"
  409. shift
  410. done
  411. ;;
  412. 'nolibfuse')
  413. # Convert old AppImages to a new standard and get rid of libfuse2 dependency
  414. _online_check
  415. _determine_args
  416. entries="$(echo "$@" | cut -f2- -d ' ')"
  417. for arg in $entries; do
  418. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  419. _nolibfuse "${@}"
  420. _remove_info_files
  421. shift
  422. done
  423. ;;
  424. 'overwrite'|'-o')
  425. # Restore an app to a previous version using a snapshot (see "backup" or "-b", above)
  426. _determine_args
  427. entries="$(echo "$@" | cut -f2- -d ' ')"
  428. for arg in $entries; do
  429. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  430. _overwrite "${@}"
  431. _remove_info_files
  432. shift
  433. done
  434. ;;
  435. 'remove'|'-r')
  436. # Remove apps
  437. entries="$(echo "$@" | cut -f2- -d ' ')"
  438. for arg in $entries; do
  439. _remove "${@}"
  440. _remove_info_files
  441. shift
  442. done
  443. echo "------------------------------------------------------------------------------"
  444. ;;
  445. '-R')
  446. # Remove apps without confirmation
  447. entries="$(echo "$@" | cut -f2- -d ' ')"
  448. for arg in $entries; do
  449. _hard_remove "${@}"
  450. _remove_info_files
  451. shift
  452. done
  453. echo "------------------------------------------------------------------------------"
  454. ;;
  455. esac