management.am 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_integration() {
  139. "$arg" --appimage-extract *.desktop 1>/dev/null && mv ./squashfs-root/*.desktop ./"$appimage".desktop
  140. "$arg" --appimage-extract .DirIcon 1>/dev/null && mv ./squashfs-root/.DirIcon ./DirIcon
  141. COUNT=0
  142. while [ "$COUNT" -lt 10 ]; do # Tries to get the actual icon/desktop if it is a symlink to another symlink
  143. if [ -L ./"$appimage".desktop ]; then
  144. LINKPATH="$(readlink ./"$appimage".desktop | sed 's|^\./||' 2>/dev/null)"
  145. "$arg" --appimage-extract "$LINKPATH" 1>/dev/null && mv ./squashfs-root/"$LINKPATH" ./"$appimage".desktop
  146. fi
  147. if [ -L ./DirIcon ]; then
  148. LINKPATH="$(readlink ./DirIcon | sed 's|^\./||' 2>/dev/null)"
  149. "$arg" --appimage-extract "$LINKPATH" 1>/dev/null && mv ./squashfs-root/"$LINKPATH" "$DATADIR"/icons/ 1>/dev/null
  150. fi
  151. [ ! -L ./"$appimage".desktop ] && [ ! -L ./DirIcon ] && break
  152. COUNT=$((COUNT + 1))
  153. done
  154. sed -i "s#Exec=[^ ]*#Exec=\"$arg\"#g" ./"$appimage".desktop
  155. mv ./"$appimage".desktop "$DATADIR"/applications/AppImages/"$appimage"-AM.desktop
  156. rm -R -f ./squashfs-root ./DirIcon
  157. }
  158. function _launcher_appimage_bin() {
  159. mkdir -p "$BINDIR"
  160. if ! echo "$PATH" | grep "$BINDIR" >/dev/null 2>&1; then
  161. echo "$DIVIDING_LINE"
  162. echo "WARNING: \"$BINDIR\" is not in PATH, apps may not run from command line." | fold -sw 77 | sed 's/^/ /g'
  163. echo "$DIVIDING_LINE"
  164. fi
  165. read -r -p " Write a custom command to launch the app, or leave blank: " response
  166. if [ -z "$response" ]; then
  167. appimage_cmd=$(echo "$appimage" | tr '[:upper:]' '[:lower:]')
  168. if ! echo "$appimage" | grep -q -i ".appimage"; then
  169. [ -n "$BINDIR" ] && printf '#!/bin/sh\n%s' "$arg" >> "$BINDIR"/"$appimage_cmd".appimage \
  170. && chmod a+x "$BINDIR"/"$appimage_cmd".appimage && echo " New command: \"$appimage_cmd.appimage\" in $BINDIR"
  171. else
  172. [ -n "$BINDIR" ] && printf '#!/bin/sh\n%s' "$arg" >> "$BINDIR"/"$appimage_cmd" \
  173. && chmod a+x "$BINDIR"/"$appimage_cmd" && echo " New command: \"$appimage_cmd\" in $BINDIR"
  174. fi
  175. elif command -v "$response" 1>/dev/null; then
  176. echo " ERROR: the \"$response\" command alredy exists, retry!"
  177. else
  178. [ -n "$BINDIR" ] && ln -s "$arg" "$BINDIR"/"$response"
  179. fi
  180. }
  181. function _launcher(){
  182. if ! test -f "$arg"; then
  183. echo " ERROR: \"$arg\" not found"
  184. elif ! strings -d "$arg" 2>/dev/null | grep -F -q 'if you run it with the --appimage-extract option'; then
  185. echo " ERROR: \"$arg\" is NOT an AppImage"
  186. else
  187. printf " ◆ File: %s\n" "$arg"
  188. appimage=$(basename -- "$arg")
  189. mkdir -p "$DATADIR"/applications/AppImages
  190. mkdir -p "$DATADIR"/icons
  191. chmod a+x "$arg"
  192. cd "$(dirname "$arg")" || return
  193. _launcher_appimage_integration 2>/dev/null
  194. _launcher_appimage_bin
  195. fi
  196. }
  197. # LOCK/UNLOCK
  198. function _lock() {
  199. if [ ! -f "$argpath"/AM-updater ]; then
  200. echo " \"$AMCLIUPPER\" cannot manage updates for $2, \"AM-updater\" file not found!"
  201. return 1
  202. fi
  203. read -r -p " Do you wish to keep $2 at its current version? (y/N) " yn
  204. if echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  205. mv "$argpath"/AM-updater "$argpath"/AM-LOCK 1>/dev/null
  206. echo " $2 has been locked at current version!"
  207. else
  208. echo " Operation aborted!"
  209. return 1
  210. fi
  211. }
  212. function _unlock() {
  213. if ! test -f "$argpath"/AM-LOCK; then
  214. echo " \"$2\" cannot be unlocked, \"AM-LOCK\" file not found!"
  215. return 1
  216. fi
  217. read -r -p " Do you wish to unlock updates for $2? (Y/n) " yn
  218. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  219. echo " $2 is still locked at current version!"
  220. return 1
  221. else
  222. mv "$argpath"/AM-LOCK "$argpath"/AM-updater 1>/dev/null
  223. echo " \"$2\" can now receive updates!"
  224. fi
  225. }
  226. # NO LIBFUSE
  227. function _nolibfuse() {
  228. AMCLIPATH_ORIGIN="$AMCLIPATH"
  229. target="$(echo "${2}" | tr '[:lower:]' '[:upper:]')"
  230. # safety checks
  231. if ! cd "$argpath" 2>/dev/null; then
  232. echo " ⚠️ Error: \"$target\" is NOT installed."
  233. return 1
  234. fi
  235. string="$(strings -d "./$2" 2>/dev/null)"
  236. if ! echo "$string" | grep -q -- 'run it with the --appimage-extract'; then
  237. echo " ⚠️ Error: $target is NOT an AppImage."
  238. return 1
  239. elif ! echo "$string" | grep -q -- 'AppImages require FUSE to run'; then
  240. echo " ◆ $target is already a new generation AppImage."
  241. return 1
  242. elif test -f ./*.zsync; then
  243. echo "-----------------------------------------------------------------------"
  244. echo " Warning! Your AppImage uses \"zsync\" to update."
  245. echo " The .zsync file will be removed and will no longer work"
  246. echo " your \"AM-updater\" will likely still be able to update the AppImage"
  247. echo " by comparing the old vs new version url, but it is not guaranteed"
  248. read -r -p " Do you want to proceede anyway? (N/y) " yn
  249. if ! echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  250. return 1
  251. fi
  252. fi
  253. appimagetool="https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-$ARCH.AppImage"
  254. printf " ...downloading appimagetool\r"
  255. wget -q "$appimagetool" -O ./appimagetool || return 1
  256. printf '#!/bin/sh\nexit 0' > ./desktop-file-validate # hack due to https://github.com/AppImage/appimagetool/pull/47
  257. chmod a+x ./appimagetool ./desktop-file-validate
  258. printf " ...extracting the AppImage\r"
  259. ./"$2" --appimage-extract >/dev/null 2>&1 && chmod 0755 ./squashfs-root
  260. printf " ...trying to convert in new generation AppImage\r"
  261. PATH="$PATH:$PWD" ARCH="$(uname -m)" ./appimagetool -n ./squashfs-root >/dev/null 2>&1
  262. if ! test -f ./*.AppImage; then
  263. echo " 💀Error when trying to convert $target. Operation Aborted."
  264. rm -R -f ./appimagetool ./squashfs-root ./desktop-file-validate
  265. return 1
  266. fi
  267. mv ./"$2" ./"$2".old && mv ./*.AppImage ./"$2" || return 1
  268. echo " ◆ $target has been converted to a new generation AppImage."
  269. rm -rf ./appimagetool ./squashfs-root ./desktop-file-validate ./*.zsync
  270. if [ -f ./AM-updater ] && ! grep -q 'nolibfuse' ./AM-updater; then
  271. 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
  272. echo " The next update may replace this AppImage with a Type2 one"
  273. echo " so I added this command to the bottom of the \"AM-updater\" script!"
  274. fi
  275. echo " Contact the upstream developers to make them officially upgrade!"
  276. read -r -p " Do you wish to remove the old libfuse2 AppImage? (Y/n) " yn
  277. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  278. return 1
  279. else
  280. rm -f ./*.old
  281. fi
  282. }
  283. # REMOVE
  284. function _detect_path_to_remove() {
  285. if [ -d "$APPMAN_APPSPATH"/"$arg" ]; then
  286. RMPATH="1"
  287. elif [ -d "$APPSPATH"/"$arg" ]; then
  288. [ -w "$APPSPATH"/"$arg" ] && $SUDOCMD echo -e "\r" >/dev/null
  289. RMPATH="1"
  290. else
  291. RMPATH=""
  292. fi
  293. echo "$DIVIDING_LINE"
  294. }
  295. function _remove() {
  296. [ "$AMCLI" = am ] && _detect_appman_apps
  297. _detect_path_to_remove
  298. [ -z "$RMPATH" ] && echo -e " \"${RED}$arg\033[0m\" is not a valid \"APPNAME\", see \"$AMCLI -f\" for more." && return 1
  299. read -r -p " ◆ Do you wish to remove \"$arg\"? (Y/n) " yn
  300. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  301. echo -e " \"${LightBlue}$arg\033[0m\" has not been removed!"
  302. else
  303. [ "$AMCLI" = am ] && "$APPMAN_APPSPATH"/"$arg"/remove 2>/dev/null || $SUDOCMD "$APPSPATH"/"$arg"/remove 2>/dev/null || return 1
  304. _clean_amcachedir
  305. sleep 0.1
  306. echo -e " \"${Green}$arg\033[0m\" has been removed!"
  307. fi
  308. [ -d "$DATADIR"/icons/hicolor/scalable/apps ] && find "$DATADIR"/icons/hicolor/scalable/apps -xtype l -exec rm {} \;
  309. }
  310. function _hard_remove() {
  311. [ "$AMCLI" = am ] && _detect_appman_apps
  312. _detect_path_to_remove
  313. [ -z "$RMPATH" ] && echo -e " \"${RED}$arg\033[0m\" is not a valid \"APPNAME\", see \"$AMCLI -f\" for more." && return 1
  314. [ "$AMCLI" = am ] && "$APPMAN_APPSPATH"/"$arg"/remove 2>/dev/null || $SUDOCMD "$APPSPATH"/"$arg"/remove 2>/dev/null || return 1
  315. _clean_amcachedir
  316. sleep 0.1
  317. echo -e " \"${Green}$arg\033[0m\" has been removed!"
  318. [ -d "$DATADIR"/icons/hicolor/scalable/apps ] && find "$DATADIR"/icons/hicolor/scalable/apps -xtype l -exec rm {} \;
  319. }
  320. ###########################################################################
  321. # OPTIONS AVAILABLE IN THIS MODULE
  322. ###########################################################################
  323. # Main logic
  324. [ -z "$2" ] && echo " USAGE: $AMCLI $1 [ARGUMENT]" && exit 1
  325. case "$1" in
  326. 'backup'|'-b')
  327. # Do a snapshot of an installed app
  328. _determine_args
  329. entries="$(echo "$@" | cut -f2- -d ' ')"
  330. for arg in $entries; do
  331. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  332. _backup "${@}"
  333. shift
  334. done
  335. ;;
  336. 'downgrade'|'--rollback')
  337. # Downgrade the installed app to a previous version, from its online source
  338. _online_check
  339. _determine_args
  340. entries="$(echo "$@" | cut -f2- -d ' ')"
  341. for arg in $entries; do
  342. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  343. _downgrade "${@}"
  344. _clean_amcachedir
  345. _update_list_updatable_apps
  346. shift
  347. done
  348. ;;
  349. 'icons'|'--icons')
  350. # Place local AppImages into the menu by dragging and dropping them into the terminal
  351. _determine_args
  352. if [ "$2" = "--all" ]; then
  353. entries="$ARGS"
  354. read -r -p " ◆ Do you wish to allow custom icon theming for ALL the apps? (y,N) " yn
  355. else
  356. entries="$(echo "$@" | cut -f2- -d ' ')"
  357. read -r -p " ◆ Do you wish to allow custom icon theming for the selected apps? (y,N) " yn
  358. fi
  359. if echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  360. for arg in $entries; do
  361. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  362. _icon_theme "${@}"
  363. done
  364. echo " ✔ Success!"
  365. else
  366. echo " ✖ Aborted!"
  367. fi
  368. ;;
  369. 'launcher'|'--launcher')
  370. # Place local AppImages into the menu by dragging and dropping them into the terminal
  371. entries="$(echo "$@" | cut -f2- -d ' ')"
  372. for arg in $entries; do
  373. echo "---------------------------------------------------------------------"
  374. _launcher "${@}"
  375. done
  376. echo "---------------------------------------------------------------------"
  377. ;;
  378. 'lock')
  379. # Lock the version of an installed app
  380. _determine_args
  381. entries="$(echo "$@" | cut -f2- -d ' ')"
  382. for arg in $entries; do
  383. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  384. _lock "${@}"
  385. shift
  386. done
  387. ;;
  388. 'unlock')
  389. # Unlock the version of an installed app
  390. _determine_args
  391. entries="$(echo "$@" | cut -f2- -d ' ')"
  392. for arg in $entries; do
  393. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  394. _unlock "${@}"
  395. shift
  396. done
  397. ;;
  398. 'nolibfuse')
  399. # Convert old AppImages to a new standard and get rid of libfuse2 dependency
  400. _online_check
  401. _determine_args
  402. entries="$(echo "$@" | cut -f2- -d ' ')"
  403. for arg in $entries; do
  404. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  405. _nolibfuse "${@}"
  406. _remove_info_files
  407. shift
  408. done
  409. ;;
  410. 'overwrite'|'-o')
  411. # Restore an app to a previous version using a snapshot (see "backup" or "-b", above)
  412. _determine_args
  413. entries="$(echo "$@" | cut -f2- -d ' ')"
  414. for arg in $entries; do
  415. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  416. _overwrite "${@}"
  417. _remove_info_files
  418. shift
  419. done
  420. ;;
  421. 'remove'|'-r')
  422. # Remove apps
  423. entries="$(echo "$@" | cut -f2- -d ' ')"
  424. for arg in $entries; do
  425. _remove "${@}"
  426. _remove_info_files
  427. shift
  428. done
  429. echo "------------------------------------------------------------------------------"
  430. ;;
  431. '-R')
  432. # Remove apps without confirmation
  433. entries="$(echo "$@" | cut -f2- -d ' ')"
  434. for arg in $entries; do
  435. _hard_remove "${@}"
  436. _remove_info_files
  437. shift
  438. done
  439. echo "------------------------------------------------------------------------------"
  440. ;;
  441. esac