APP-MANAGER 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. #!/usr/bin/env bash
  2. AMVERSION="9.1"
  3. # Determine main repository and branch
  4. AMREPO="https://raw.githubusercontent.com/ivan-hc/AM/main"
  5. AMBRANCH=$(basename "$AMREPO")
  6. MODULES_SOURCE="$AMREPO/modules"
  7. # Determine catalogue in use
  8. export AMCATALOGUEMARKDOWNS="https://portable-linux-apps.github.io/apps"
  9. export AMCATALOGUEICONS="https://portable-linux-apps.github.io/icons"
  10. # Determine the name of this script and its working directory
  11. export REALDIR="$PWD"
  12. DIR="$( cd "$( dirname "$0" )" && pwd )"
  13. CLI=$(basename "$0")
  14. # Determine system architecture and current user
  15. arch="$HOSTTYPE"
  16. export ARCH="$arch"
  17. # XDG Variables
  18. export BINDIR="${XDG_BIN_HOME:-$HOME/.local/bin}"
  19. export DATADIR="${XDG_DATA_HOME:-$HOME/.local/share}"
  20. export CONFIGDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
  21. export CACHEDIR="${XDG_CACHE_HOME:-$HOME/.cache}"
  22. export APPMANCONFIG="$CONFIGDIR/appman"
  23. APPMANCONFIG="$CONFIGDIR/appman"
  24. SCRIPTDIR="$(xdg-user-dir DESKTOP 2>/dev/null || echo "$HOME")"
  25. export SCRIPTDIR
  26. # Colors
  27. RED='\033[0;31m'
  28. Gold='\033[0;33m'
  29. Green='\033[0;32m'
  30. LightBlue='\033[1;34m'
  31. DIVIDING_LINE="-----------------------------------------------------------------------------"
  32. # Prevent the use of "sudo" ("AM")
  33. [ -n "$SUDO_COMMAND" ] && echo -e "\n Please do not use \"sudo\" to execute \"$CLI\", try again.\n" && exit 1
  34. function _create_cache_dir() {
  35. AMCACHEDIR="$CACHEDIR/$AMCLI"
  36. mkdir -p "$AMCACHEDIR"
  37. }
  38. function _clean_amcachedir() {
  39. [ "$AMCLI" = am ] && [ -d "$CACHEDIR"/am ] && rm -f "$CACHEDIR"/am/*
  40. [ -d "$CACHEDIR"/appman ] && rm -f "$CACHEDIR"/appman/*
  41. }
  42. ################################################################################
  43. # AM/APPMAN
  44. ################################################################################
  45. # "APPMAN" CORE VARIABLES AND FUNCTIONS
  46. APPMAN_SETUP_MSG="Before proceeding with any task, where do you want to install apps?
  47. SYNTAX: /FULLPATH/TO/DIRNAME
  48. EXAMPLE: $HOME/My-apps
  49. NOTE: Any spaces in the path will be replaced for dashes
  50. NOTE: If no input is given then \"~/Applications\" will be used as default
  51. if you wish to later change the location, first remove all the programs and then edit the \"$APPMANCONFIG/appman-config\" file."
  52. function _appman_check() {
  53. if [ ! -f "$APPMANCONFIG"/appman-config ]; then
  54. echo "$DIVIDING_LINE"
  55. [ "$AMCLI" = am ] && echo ">>> Configure AppMan" || echo ">>> Thank you for choosing AppMan!"
  56. echo "$DIVIDING_LINE"
  57. echo "$APPMAN_SETUP_MSG" | fold -sw 77 | sed 's/^/ /g'
  58. echo "$DIVIDING_LINE"
  59. read -r -ep " Write the path or just press enter to use default:$(printf "\n\n ")" location
  60. location="$(echo "$location" | sed 's/[ \t]/-/g; s|^\./||' 2>/dev/null)"
  61. [ -z "$location" ] && location="$HOME/Applications"
  62. if ! echo "$location" | grep "^/" >/dev/null 2>&1; then
  63. location="$HOME/$location"
  64. fi
  65. if echo "$location" | grep "$BINDIR" >/dev/null 2>&1; then
  66. echo "$DIVIDING_LINE"
  67. echo " 💀 ERROR, you can't install applications in \"$BINDIR\""
  68. echo " $BINDIR is normally used for executables, Please choose a different path and retry!"
  69. echo "$DIVIDING_LINE"
  70. exit 1
  71. elif ! mkdir -p "$location" 2>/dev/null || [ ! -w "$location" ]; then
  72. echo " 💀 ERROR: You don't have write access to $location or it is invalid"
  73. exit 1
  74. fi
  75. mkdir -p "$APPMANCONFIG" || exit 1
  76. echo "${location%/}" > "$APPMANCONFIG"/appman-config || exit 1
  77. echo "$DIVIDING_LINE"
  78. echo " You are ready! Start installing your favorite apps locally!"
  79. echo " All apps will be installed in $location"
  80. echo " In case of problems, use the option \"-h\"."
  81. echo "$DIVIDING_LINE"
  82. fi
  83. }
  84. function _appman() {
  85. _appman_check
  86. if ! grep -q "^/" "$APPMANCONFIG"/appman-config; then
  87. APPSDIR="$HOME/$(head -1 "$APPMANCONFIG"/appman-config 2>/dev/null)"
  88. else
  89. APPSDIR="$(head -1 "$APPMANCONFIG"/appman-config 2>/dev/null)"
  90. fi
  91. [ -n "$APPSDIR" ] && mkdir -p "$APPSDIR"/appman || exit 1
  92. mkdir -p "$BINDIR" "$DATADIR"/applications "$DATADIR"/icons || exit 1
  93. AMCLI="appman"
  94. AMCLIPATH="$DIR/$AMCLI"
  95. SUDOCMD=""
  96. APPSPATH="$APPSDIR"
  97. AMPATH="$APPSDIR/$AMCLI"
  98. _create_cache_dir
  99. if [ ! -w "$APPSPATH" ]; then
  100. echo " ERROR: You don't have write access to $APPSPATH"
  101. exit 1
  102. elif ! echo "$PATH" | grep "$BINDIR" >/dev/null 2>&1; then
  103. echo "$DIVIDING_LINE"
  104. echo " ⚠️ WARNING: \"$BINDIR\" is not in PATH, apps may not run."
  105. echo "$DIVIDING_LINE"
  106. fi
  107. MODULES_PATH="$AMPATH/modules"
  108. mkdir -p "$MODULES_PATH" || exit 1
  109. }
  110. # "AM" CORE VARIABLES
  111. function _am() {
  112. AMCLI="am"
  113. AMCLIPATH="$AMCLI"
  114. if command -v sudo >/dev/null 2>&1; then
  115. export SUDOCMD="sudo"
  116. elif command -v doas >/dev/null 2>&1; then
  117. export SUDOCMD="doas"
  118. else
  119. echo 'ERROR: No sudo or doas found'
  120. exit 1
  121. fi
  122. APPSPATH="/opt"
  123. AMPATH="$APPSPATH/$AMCLI"
  124. _create_cache_dir
  125. MODULES_PATH="$AMPATH/modules"
  126. }
  127. # DETERMINE WHEN TO USE "AM" OR "APPMAN"
  128. if [ "$(realpath "$0")" = "/opt/am/APP-MANAGER" ]; then
  129. _am
  130. mkdir -p "$MODULES_PATH" || exit 1
  131. elif [ "$(realpath "$0")" = "/usr/bin/am" ]; then
  132. _am
  133. AMPATH="$AMCACHEDIR"
  134. MODULES_PATH="/usr/lib/am/modules"
  135. else
  136. _appman
  137. fi
  138. function _detect_appman_apps() {
  139. [ -f "$APPMANCONFIG/appman-config" ] && APPMAN_APPSPATH=$(<"$APPMANCONFIG/appman-config")
  140. if ! echo "$APPMAN_APPSPATH" | grep -q "^/"; then
  141. [ -f "$APPMANCONFIG/appman-config" ] && APPMAN_APPSPATH="$HOME/$(<"$APPMANCONFIG/appman-config")"
  142. fi
  143. }
  144. function _determine_args() {
  145. ARGPATHS=$(find "$APPSPATH" -name 'remove' -printf " %h\n" 2>/dev/null | sort -u | sed 's/ //g')
  146. ARGS=$(echo "$ARGPATHS" | xargs -n 1 basename 2>/dev/null)
  147. if [ "$AMCLI" = am ]; then
  148. _detect_appman_apps
  149. if [ -d "$APPMAN_APPSPATH" ]; then
  150. APPMAN_PATHS=$(find "$APPMAN_APPSPATH" -name 'remove' -printf " %h\n" 2>/dev/null | sort -u | sed 's/ //g')
  151. ARGPATHS=$(echo -e "$ARGPATHS\n$APPMAN_PATHS")
  152. ARGS=$(echo "$ARGPATHS" | xargs -n 1 basename 2>/dev/null)
  153. fi
  154. fi
  155. # use "argpath=$(echo "$ARGPATHS" | grep "/$arg$")" to determine the full path of "arg"
  156. }
  157. function _icon_theme_export_to_datadir() {
  158. PNG="$(file "$APPSPATH"/*/icons/* | grep -i '.png' | awk -F":" '{print $1}' | grep -vi .png)"
  159. SVG="$(file "$APPSPATH"/*/icons/* | grep -i '.svg' | awk -F":" '{print $1}' | grep -vi .svg)"
  160. for file in $PNG; do ln -s "$file" "${file}".png; done
  161. for file in $SVG; do ln -s "$file" "${file}".svg; done
  162. if [ -n "$APPMAN_APPSPATH" ]; then
  163. PNG="$(file "$APPMAN_APPSPATH"/*/icons/* | grep -i '.png' | awk -F":" '{print $1}' | grep -vi .png)"
  164. SVG="$(file "$APPMAN_APPSPATH"/*/icons/* | grep -i '.svg' | awk -F":" '{print $1}' | grep -vi .svg)"
  165. for file in $PNG; do ln -s "$file" "${file}".png; done
  166. for file in $SVG; do ln -s "$file" "${file}".svg; done
  167. fi
  168. mkdir -p "$DATADIR"/icons/hicolor/scalable/apps
  169. find "$DATADIR"/icons/hicolor/scalable/apps -xtype l -exec rm {} \;
  170. ln -s "$APPSPATH"/*/icons/*.* "$DATADIR"/icons/hicolor/scalable/apps
  171. [ -n "$APPMAN_APPSPATH" ] && ln -s "$APPMAN_APPSPATH"/*/icons/*.* "$DATADIR"/icons/hicolor/scalable/apps
  172. }
  173. ################################################################################
  174. # FINALIZE
  175. ################################################################################
  176. AMCLIUPPER=$(echo "$AMCLI" | tr '[:lower:]' '[:upper:]')
  177. # Create new data directory and move important files there
  178. AMDATADIR="$DATADIR/AM"
  179. mkdir -p "$AMDATADIR"
  180. # DEVELOPER MODE
  181. if test -f "$AMDATADIR"/betatester; then
  182. AMREPO="https://raw.githubusercontent.com/ivan-hc/AM/dev"
  183. AMBRANCH=$(basename "$AMREPO")
  184. MODULES_SOURCE="$AMREPO/modules"
  185. fi
  186. function _betatester_message_on() {
  187. [ -f "$AMDATADIR"/betatester ] \
  188. && echo -e "$DIVIDING_LINE\n\"$AMCLIUPPER\" $AMVERSION: DEVELOPER MODE\n$DIVIDING_LINE"
  189. }
  190. # Apps database in use
  191. APPSDB="$AMREPO/programs/$arch"
  192. APPSLISTDB="$AMREPO/programs/$arch-apps"
  193. ################################################################################
  194. # SECURITY
  195. ################################################################################
  196. # SAFETY CHECKS
  197. function _am_dependences_check() {
  198. # Check for essential commands required by the application
  199. missing_deps=()
  200. AMDEPENDENCES="cat chmod chown curl grep less sed wget"
  201. for name in $AMDEPENDENCES; do
  202. if ! command -v "$name" &>/dev/null; then
  203. missing_deps+=("$name")
  204. fi
  205. done
  206. # Exit if any essential command is missing
  207. if [ -n "$missing_deps" ]; then
  208. echo "$DIVIDING_LINE"
  209. printf " ${RED}💀 ERROR! MISSING ESSENTIAL COMMANDS\033[0m: %s\n\n Install the above and try again! \n" "${missing_deps[*]}"
  210. echo -e "$DIVIDING_LINE\n ${Green}List of the $AMCLIUPPER $AMVERSION core dependences\033[0m:\n"
  211. echo "$AMDEPENDENCES" | tr ' ' ',' | sed 's/,/, /g' | sed 's/^/ /g'
  212. echo -e "\n$DIVIDING_LINE"
  213. echo " If this message appears it is because you are missing some dependency"
  214. echo " and if its the first time its because something new has been introduced."
  215. echo -e "\n See ${LightBlue}https://github.com/ivan-hc/AM#core-dependences\033[0m for more information\n$DIVIDING_LINE"
  216. exit 1
  217. fi
  218. }
  219. function _check_ubuntu_mess() {
  220. if ! unshare --user -p /bin/true >/dev/null 2>&1; then
  221. echo "$DIVIDING_LINE"
  222. echo ""
  223. echo -e " ${RED}⚠️ WARNING: ACCESS TO USER NAMESPACES IS RESTRICTED! \033[0m"
  224. echo ""
  225. echo " Some apps may not run, you need to enable access to user namespaces,"
  226. echo -e " please visit ${LightBlue}https://github.com/ivan-hc/AM#ubuntu-mess\033[0m to know more."
  227. echo ""
  228. echo "$DIVIDING_LINE"
  229. fi
  230. }
  231. _am_dependences_check
  232. _check_ubuntu_mess
  233. # Function to check online connections (uses github.com by default, as the database and CLI itself are stored/hosted there)
  234. function _online_check() {
  235. if ! wget -q --tries=10 --timeout=20 --spider https://github.com; then
  236. echo -e "\n $AMCLI is offline, please check your internet connection and try again\n"
  237. exit 0
  238. fi
  239. }
  240. # BLACKLIST FILES
  241. appimagelauncher_msg="Your installation of AppImageLauncher may have been done via DEB, RPM, or AUR, which interrupts the \
  242. natural operation of \"systemd-binfmt\" in addition to launching the aforementioned daemon. To avoid problems with \"$AMCLIUPPER\" \
  243. and any other AppImages helper, it's preferable to use \"only\" the standalone AppImage of AppImageLauncher, whose official \
  244. updated release can also be installed via \"$AMCLIUPPER\". But as long as you have the currently installed version, you can't use this CLI."
  245. function _blacklisted_file() {
  246. echo ""
  247. echo -e " ${RED}💀WARNING! Detected \"$blacklisted_file\"\033[0m"
  248. echo ""
  249. echo "It will prevent \"$AMCLIUPPER\" from working correctly with AppImages, especially when extracting, integrating and updating them." | fold -sw 77 | sed 's/^/ /g'
  250. echo ""
  251. if echo "$blacklisted_file" | grep -q appimagelauncherd; then
  252. echo "$appimagelauncher_msg" | fold -sw 77 | sed 's/^/ /g'
  253. echo "" && echo " Please remove \"AppImageLauncher\", reboot and retry!"
  254. else
  255. echo " Please remove \"$blacklisted_file\", reboot and retry!"
  256. fi
  257. echo ""
  258. exit 0
  259. }
  260. if command -v appimaged &>/dev/null; then
  261. blacklisted_file=$(command -v appimaged)
  262. _blacklisted_file
  263. elif command -v appimagelauncherd &>/dev/null; then
  264. blacklisted_file=$(command -v appimagelauncherd)
  265. _blacklisted_file
  266. fi
  267. ################################################################################
  268. # 3RD PARTY
  269. ################################################################################
  270. function _use_newrepo() {
  271. [ -z "$2" ] && echo " USAGE: $AMCLI $1 [ARGUMENT]" && exit 1
  272. case $2 in
  273. 'add')
  274. [ -z "$3" ] && echo -e " USAGE: $AMCLI $1 $2 /path/to/dir\n $AMCLI $1 $2 {URL}" && exit 1
  275. echo "$3" >> "$AMDATADIR/newrepo-lists"
  276. ;;
  277. 'enable'|'on')
  278. [ ! -f "$AMDATADIR/newrepo-lists" ] && echo " ERROR, \"$AMDATADIR/newrepo-lists\" file not found" && exit 1
  279. [ -f "$AMDATADIR/newrepo-off" ] && mv "$AMDATADIR/newrepo-off" "$AMDATADIR/newrepo-on" && echo " New repo ON!"
  280. ;;
  281. 'disable'|'off')
  282. [ ! -f "$AMDATADIR/newrepo-lists" ] && echo " ERROR, \"$AMDATADIR/newrepo-lists\" file not found" && exit 1
  283. [ -f "$AMDATADIR/newrepo-on" ] && mv "$AMDATADIR/newrepo-on" "$AMDATADIR/newrepo-off" && echo " New repo OFF!"
  284. ;;
  285. 'info')
  286. echo -e " Source: $AMREPO\n Apps: $APPSDB\n List: $APPSLISTDB"
  287. ;;
  288. 'purge')
  289. [ -f "$AMDATADIR/newrepo-lists" ] && rm -f "$AMDATADIR"/newrepo* && echo " Removed all 3rd party repositories"
  290. ;;
  291. 'select')
  292. [ ! -f "$AMDATADIR/newrepo-lists" ] && echo " ERROR, \"$AMDATADIR/newrepo-lists\" file not found" && exit 1
  293. echo -e "Select a repo from the list or press CTRL+C to abort:\n$DIVIDING_LINE\n"; sleep 1
  294. select repo in $(sort -u "$AMDATADIR/newrepo-lists" | uniq); do
  295. test -n "$repo" && break
  296. echo ">>> Invalid Selection"
  297. done
  298. echo "$repo" > "$AMDATADIR/newrepo-on"
  299. ;;
  300. esac
  301. }
  302. # 3RD PARTY DATABASES
  303. function _am_newrepo_check() {
  304. # Determine if the CLI uses the "main" branch of https://github.com/ivan-hc/AM or an alternative one
  305. if [ -f "$AMDATADIR/newrepo-on" ]; then
  306. if grep -q "^http" "$AMDATADIR/newrepo-on"; then
  307. AMREPO=$(<"$AMDATADIR/newrepo-on")
  308. elif grep -q "^/" "$AMDATADIR/newrepo-on"; then
  309. AMREPO="file://$(<"$AMDATADIR/newrepo-on")"
  310. fi
  311. AMBRANCH=$(basename "$AMREPO")
  312. export APPSDB="$AMREPO/programs/$arch"
  313. export APPSLISTDB="$AMREPO/programs/$arch-apps"
  314. export MODULES_PATH="$AMPATH/modules"
  315. export AMCATALOGUEMARKDOWNS=""
  316. export AMCATALOGUEICONS=""
  317. [ "$1" != "newrepo" ] && [ "$1" != "neodb" ] && echo -e "$DIVIDING_LINE\n Source: $AMREPO\n$DIVIDING_LINE"
  318. fi
  319. }
  320. _am_newrepo_check "$@"
  321. # 3RD PARTY SOURCES -- TOOLPACK
  322. toolpack_repo="https://github.com/Azathothas/Toolpacks"
  323. export toolpack_readme="https://bin.pkgforge.dev/${ARCH}/AM.txt"
  324. export toolpack_name="1" toolpack_description="2" toolpack_site="3" toolpack_dl="4" toolpack_ver="5"
  325. ################################################################################
  326. # UTILITIES
  327. ################################################################################
  328. # COMPLETION LIST
  329. available_options="about add apikey backup clean config disable downgrade download enable extra files home icons info \
  330. install install-appimage launcher list lock neodb newrepo nolibfuse off on overwrite purge query remove sandbox \
  331. select sync template test unlock update --all --appimages --apps --byname --config --convert --debug \
  332. --devmode-disable --devmode-enable --disable-notifications --enable-notifications --force-latest --home --icons \
  333. --launcher --less --pkg --rollback --disable-sandbox --sandbox --system --toolpack --user"
  334. function _completion_lists() {
  335. # Remove existing lists and download new ones
  336. curl -Ls "$APPSLISTDB" > "$AMDATADIR/$arch-apps"
  337. awk -v FS="(◆ | : )" '{print $2}' <"$AMDATADIR"/"$arch"-apps > "$AMDATADIR"/list
  338. [ -f "$AMCACHEDIR/$ARCH-toolpack" ] && awk -v FS="(◆ | : )" '{print $2}' <"$AMCACHEDIR/$ARCH-toolpack" >> "$AMDATADIR"/list
  339. # Append options to the list
  340. for o in $available_options; do
  341. echo "$o" >> "$AMDATADIR"/list
  342. done
  343. }
  344. # BASH AND ZSH COMPLETION
  345. completion_file="$DATADIR/bash-completion/completions/$AMCLI"
  346. mkdir -p "$DATADIR/bash-completion/completions" || exit 1
  347. [ -f "$HOME"/.bash_completion ] && sed -i "/ $AMCLI$/d" "$HOME"/.bash_completion
  348. if ! grep -o " $AMCLI$" "$completion_file" >/dev/null 2>&1; then
  349. echo "complete -W \"\$(cat $AMDATADIR/list 2>/dev/null)\" $AMCLI" >> "$completion_file"
  350. if [ -f "${ZDOTDIR:-$HOME}"/.zshrc ] && echo "$SHELL" | grep -q "zsh"; then
  351. cat <<-HEREDOC >> "${ZDOTDIR:-$HOME}"/.zshrc
  352. autoload bashcompinit
  353. bashcompinit
  354. source "$completion_file"
  355. HEREDOC
  356. fi
  357. echo "Shell completion has been enabled!"
  358. fi
  359. # VERSION OF THE INSTALLED APPS
  360. # Filters
  361. function _check_version_filters() {
  362. sed -E "s/$arch|amd64|x86-64|x64|basic|standard|full|help|Qt[0-9]//g; s/-/\n/g; s/_/\n/g;" |\
  363. grep -vi "appimage\|$arg\|?\|tar." | grep "[0-9]" | head -1 | sed 's/^v//g; s/^\.//g; s/\.$//g;'
  364. }
  365. function _check_version_grep_numbers() {
  366. grep -Eo "([0-9]{1,}\.)+[0-9]{1,}" | head -1
  367. }
  368. # Versions
  369. function _check_version_if_any_version_reference_is_somewhere() {
  370. APPVERSION=$(grep -i "version=" "$argpath"/* 2>/dev/null | _check_version_grep_numbers)
  371. }
  372. function _check_version_if_version_file_exists() {
  373. APPVERSION=$(sort "$argpath"/version | head -1 | sed 's:.*/::' | _check_version_filters)
  374. if [ -z "$APPVERSION" ]; then
  375. if grep -q "download$" "$argpath"/version; then
  376. APPVERSION=$(sort "$argpath"/version | tr '/' '\n' | _check_version_filters)
  377. elif grep -q "://" "$argpath"/version; then
  378. APPVERSION=$(sort "$argpath"/version | tr '/' '\n' | _check_version_grep_numbers)
  379. elif grep -q "/v[0-9]*" "$argpath"/version; then
  380. APPVERSION=$(sort "$argpath"/version | tr '/' '\n' | grep "^v[0-9]" | head -1 | sed 's/^v//g')
  381. elif [ "$(sort "$argpath"/version | wc -w)" = 1 ]; then
  382. APPVERSION=$(sort "$argpath"/version | head -1)
  383. fi
  384. fi
  385. if [ -z "$APPVERSION" ]; then
  386. if grep -q "http.*download/.*[0-9].*/" "$argpath"/version; then
  387. APPVERSION=$(sort "$argpath"/version | tr '/-' '\n' | grep "[0-9]" | _check_version_filters | tail -1)
  388. fi
  389. fi
  390. }
  391. function _check_version_if_an_updater_exists() {
  392. APPVERSION=$("$argpath"/updater -d "$argpath"/"$arg" 2>/dev/null | grep -i "$arg" |\
  393. _check_version_grep_numbers)
  394. [ -z "$APPVERSION" ] && _check_version_if_any_version_reference_is_somewhere
  395. }
  396. function _check_version_if_library() {
  397. LIBNAME=$(sort "$argpath"/remove | tr ' ' '\n' | grep "usr/local/lib" | head -1 | sed 's:.*/::')
  398. APPVERSION=$(find /usr/local/lib -type f -name "$LIBNAME" -type f | sed 's:.*.so.::' | tail -1)
  399. }
  400. function _check_version_if_binary_in_place() {
  401. APPVERSION=$(date -r "$argpath"/"$arg" "+%Y.%m.%d")
  402. }
  403. function _check_version() {
  404. rm -f "$AMCACHEDIR"/version-args
  405. _determine_args
  406. for arg in $ARGS; do
  407. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  408. if test -f "$argpath"/remove 2>/dev/null; then
  409. if test -f "$argpath"/version 2>/dev/null; then
  410. _check_version_if_version_file_exists
  411. elif test -f "$argpath"/updater 2>/dev/null; then
  412. _check_version_if_an_updater_exists
  413. elif [ "$arg" = "$AMCLI" ]; then
  414. APPVERSION="$AMVERSION"
  415. elif grep -qi "version=" "$argpath"/* 2>/dev/null; then
  416. _check_version_if_any_version_reference_is_somewhere
  417. elif echo "$arg" | grep -q "ffwa-"; then
  418. APPVERSION="WebApp"
  419. elif grep -q "usr/local/lib" "$argpath"/remove 2>/dev/null; then
  420. _check_version_if_library
  421. else
  422. APPVERSION="unknown"
  423. fi
  424. if [ -z "$APPVERSION" ]; then
  425. [ -f "$argpath"/"$arg" ] && _check_version_if_binary_in_place || APPVERSION="unknown"
  426. fi
  427. echo " ◆ $arg | $APPVERSION" >> "$AMCACHEDIR"/version-args
  428. fi
  429. done
  430. }
  431. function _check_version_for_auto_updatable_apps() {
  432. _determine_args
  433. for arg in $ARGS; do
  434. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  435. if test -f "$argpath"/updater 2>/dev/null; then
  436. _check_version_if_an_updater_exists
  437. OLDAPPVERSION=$(grep " ◆ $arg |" "$AMCACHEDIR"/version-args | tr '|' '\n' | sed 's/ //g' | head -2 | tail -1)
  438. sed -i "/ ◆ $arg |/s#$OLDAPPVERSION#$APPVERSION#" "$AMCACHEDIR"/*
  439. fi
  440. done
  441. }
  442. if test -f "$AMCACHEDIR"/version-args; then
  443. _check_version_for_auto_updatable_apps 2>/dev/null
  444. fi
  445. # This function removes all info and versions from the register
  446. function _remove_info_files() {
  447. rm -f "$AMCACHEDIR"/files*
  448. rm -f "$AMCACHEDIR"/version-args
  449. }
  450. ################################################################################
  451. # APIKEY
  452. ################################################################################
  453. ghapikey_file="$AMDATADIR/ghapikey.txt"
  454. # Set header authorization if GitHub API key file exists
  455. [ -f "$ghapikey_file" ] && HeaderAuthWithGITPAT=" --header \"Authorization: token $(<"$ghapikey_file")\" "
  456. function _use_apikey() {
  457. case $2 in
  458. 'del'|'delete'|'remove')
  459. [ -f "$ghapikey_file" ] || { echo " ✖ No file named $ghapikey_file has been found"; exit 1; }
  460. rm -f "$ghapikey_file" && echo " ✔ $ghapikey_file has been removed"
  461. exit 0
  462. esac
  463. if [[ "$2" =~ ^(gh[ps]_[a-zA-Z0-9]{36}|github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})$ ]]; then
  464. test_apikey_output_with_wget=$(curl -Ls --header "Authorization: token $2" 'https://api.github.com/repos/ivan-hc/AM/releases' | head -1)
  465. [ -n "$test_apikey_output_with_wget" ] && echo "$2" > "$ghapikey_file" \
  466. && echo "Validation successful!" || echo "ERROR: This is not a valid key!"
  467. else
  468. echo "ERROR: Wrong expression, validation failed!"
  469. fi
  470. }
  471. function _update_github_api_key_in_the_updater_files() {
  472. if [ -f "$ghapikey_file" ]; then
  473. ghapikey=$(<"$ghapikey_file")
  474. updater_files=("$APPSPATH"/*/AM-updater) # Assuming AM-updater is one level deeper
  475. for f in "${updater_files[@]}"; do
  476. if [ -f "$f" ] && grep -q "https://api.github.com" "$f"; then
  477. # Check if the file already contains a valid API key
  478. if ! grep -qE "(gh[ps]_[a-zA-Z0-9]{36}|github_pat_[a-zA-Z0-9]{22}_[a-zA-Z0-9]{59})" "$f"; then
  479. # Insert HeaderAuthWithGITPAT before the GitHub API URL
  480. sed -i "s#https://api.github.com#$HeaderAuthWithGITPAT https://api.github.com#g" "$f"
  481. else
  482. # Replace existing API key with the one from ghapikey.txt
  483. sed -i "s#\(gh[ps]_[a-zA-Z0-9]\{36\}\|github_pat_[a-zA-Z0-9]\{22\}_[a-zA-Z0-9]\{59\}\)#$ghapikey#g" "$f"
  484. fi
  485. fi
  486. done
  487. fi
  488. }
  489. ################################################################################
  490. # APPMAN MODE
  491. ################################################################################
  492. APPMAN_MSG="$DIVIDING_LINE\n \"AM\" is running as \"AppMan\", use ${Green}am --system\033[0m to switch it back to \"AM\"\n$DIVIDING_LINE"
  493. APPMAN_MSG_OFF="$DIVIDING_LINE\n \"AppMan Mode\" disabled! \n$DIVIDING_LINE"
  494. APPMAN_MSG_THINK="$DIVIDING_LINE\nNOTE: You can also choose to simply use \"--user\" as a flag to install apps locally \
  495. (options \"-i\", \"-ia\" and \"-e\") instead of going fully into \"AppMan Mode\". \"AM\" can handle local applications as well.\n$DIVIDING_LINE"
  496. function _use_appman() {
  497. _online_check
  498. [ "$CLI" = appman ] && echo " This function only works for AM" && exit 0
  499. echo -e "$APPMAN_MSG_THINK" | fold -sw 77 | sed 's/^/ /g'
  500. read -r -p " Do you wish to enter \"AppMan Mode\" (y,N)?" yn
  501. if ! echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
  502. echo "$DIVIDING_LINE"
  503. else
  504. [ ! -f "$APPMANCONFIG"/appman-mode ] && mkdir -p "$APPMANCONFIG" && touch "$APPMANCONFIG"/appman-mode
  505. _appman && echo -e "$APPMAN_MSG"
  506. fi
  507. }
  508. if [ "$AMCLI" = am ]; then
  509. if test -f "$APPMANCONFIG"/appman-mode; then
  510. [ ! -f "$APPMANCONFIG"/appman-config ] && echo -e "$APPMAN_MSG"
  511. _appman
  512. AMCLIPATH="$(realpath "$0")"
  513. elif [ ! -w "$AMPATH" ]; then
  514. read -r -p " \"AM\" is read-only, want to use it in \"AppMan Mode\" (Y,n)? " yn
  515. if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
  516. exit 0
  517. else
  518. echo -e "$DIVIDING_LINE\n \"AppMan Mode\" enabled! \n$DIVIDING_LINE"
  519. _use_appman 1>/dev/null
  520. fi
  521. fi
  522. fi
  523. ################################################################################
  524. # CLEAN
  525. ################################################################################
  526. function _clean_amcachedir_message() {
  527. _clean_amcachedir
  528. [ "$AMCLI" = am ] && [ -d "$CACHEDIR"/am ] && echo " ✔ Clear the contents of $CACHEDIR/am"
  529. [ -d "$CACHEDIR"/appman ] && echo " ✔ Clear the contents of $CACHEDIR/appman"
  530. }
  531. function _clean_all_home_cache_directories_of_appimages() {
  532. if test -d "$APPSPATH"/*/*.home/.cache; then
  533. rm -Rf "$APPSPATH"/*/*.home/.cache/* && clean_home_cache_dirs=1
  534. fi
  535. if [ "$AMCLI" = am ] && [ -n "$APPMAN_APPSPATH" ] && test -d "$APPMAN_APPSPATH"/*/*.home/.cache; then
  536. rm -Rf "$APPMAN_APPSPATH"/*/*.home/.cache/* && clean_home_cache_dirs=1
  537. fi
  538. [ -n "$clean_home_cache_dirs" ] && echo " ✔ Clear the contents of all *.home/.cache directories of AppImages"
  539. }
  540. function _clean_all_tmp_directories_from_appspath() {
  541. if test -d "$APPSPATH"/*/tmp; then
  542. rm -Rf "$APPSPATH"/*/tmp && clean_tmp_dirs=1
  543. fi
  544. if [ "$AMCLI" = am ] && [ -n "$APPMAN_APPSPATH" ] && test -d "$APPMAN_APPSPATH"/*/tmp; then
  545. rm -Rf "$APPMAN_APPSPATH"/*/tmp && clean_tmp_dirs=1
  546. fi
  547. [ -n "$clean_tmp_dirs" ] && echo " ✔ Removed all \"tmp\" directories"
  548. }
  549. function _clean_determine_removable_launchers() {
  550. if ! test -f "$APPIMAGENAME" 2>/dev/null; then
  551. if ! test -d "$MOUNTPOINTS" 2>/dev/null; then
  552. if echo "$MOUNTPOINTS" | grep -q "/media/"; then
  553. unmounted_poin="/media"
  554. elif echo "$MOUNTPOINTS" | grep -q "/mnt/"; then
  555. unmounted_poin="/mnt"
  556. fi
  557. echo " ✖ ERROR: cannot remove \"$(basename "$var")\""
  558. echo " related AppImage is located in an unmounted path of $unmounted_poin"
  559. else
  560. rm -f "$var"
  561. rm -f "$HOME"/.local/bin/"$launcher2del"*
  562. cd "$HOME"/.local/bin && find . -xtype l -delete
  563. fi
  564. fi
  565. }
  566. function _clean_launchers() {
  567. if test -d "$DATADIR"/applications/AppImages 2>/dev/null; then
  568. for var in "$DATADIR"/applications/AppImages/*.desktop; do
  569. APPIMAGENAME=$(grep "Exec=" 0<"$var" 2>/dev/null | head -1 | cut -c 6- | sed 's/\s.*$//')
  570. launcher2del=$(basename -- "$(echo "$APPIMAGENAME" | tr '[:upper:]' '[:lower:]')")
  571. MOUNTPOINTS=$(echo "$APPIMAGENAME" | cut -d'/' -f1-4)
  572. _clean_determine_removable_launchers
  573. done
  574. echo ' ✔ Removed orphaned launchers produced with the "--launcher" option'
  575. rmdir "$DATADIR"/applications/AppImages
  576. fi
  577. }
  578. function _clean_old_modules() {
  579. MODULES=$(sort "$(realpath "$0")" | tr '"' '\n' | grep "[a-z]\.am$" | uniq)
  580. [ -z "$MODULES_PATH" ] && exit 1
  581. for m in "$MODULES_PATH"/*; do
  582. if [[ "${MODULES}" != *"$(basename -- "$m")"* ]];then
  583. rm -f "$m" 2>/dev/null
  584. echo " ✔ Removed obsolete module named \"$(basename -- "$m")\""
  585. fi
  586. done
  587. }
  588. function _use_clean() {
  589. echo " Cleaning temporary files and folders..." && sleep 0.1
  590. for i in {100..000}; do
  591. echo -ne " $i\r" && sleep 0.0001
  592. done
  593. _detect_appman_apps
  594. _clean_amcachedir_message
  595. _clean_all_home_cache_directories_of_appimages
  596. _clean_all_tmp_directories_from_appspath
  597. _clean_launchers 2>/dev/null
  598. _clean_old_modules
  599. }
  600. ################################################################################
  601. # SYNC
  602. ################################################################################
  603. function _sync_installation_scripts() {
  604. echo -e "$DIVIDING_LINE\n Checking for changes of the installation scripts in the online database..."
  605. _determine_args
  606. for arg in $ARGS; do
  607. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  608. if [ -f "$argpath"/AM-updater ]; then
  609. mkdir -p "$argpath"/.am-installer
  610. if test -f "$argpath/.am-installer"/*; then
  611. scriptname=$(ls "$argpath/.am-installer/" | head -1)
  612. CURRENT=$(cat "$argpath"/.am-installer/"$scriptname")
  613. SOURCE=$(curl -Ls "$APPSDB"/"$scriptname")
  614. if [ "$CURRENT" = "$SOURCE" ]; then
  615. echo -ne "\r" 2>/dev/null
  616. else
  617. echo -e " ◆ Changed https://github.com/ivan-hc/AM/blob/main/programs/$arch/$scriptname"
  618. fi
  619. else
  620. if curl --output /dev/null --silent --head --fail "$APPSDB"/"$arg" 1>/dev/null; then
  621. echo -e " ◆ No installation script for $arg, downloading one..."
  622. mkdir -p "$argpath"/.am-installer
  623. wget -q "$APPSDB/$arg" -O "$argpath"/.am-installer/"$arg"
  624. fi
  625. fi
  626. fi
  627. done
  628. }
  629. function _sync_modules() {
  630. echo -e "$DIVIDING_LINE\n Check for updates in modules..."
  631. MODULES=$(curl -Ls "$AMREPO/APP-MANAGER" | tr '"' '\n' | grep "[a-z]\.am$")
  632. for module_name in $MODULES; do
  633. cd "$MODULES_PATH" || return 1
  634. if ! test -f ./"$module_name"; then
  635. echo " ◆ Downloading $module_name (not previously installed)..."
  636. curl -Os "$MODULES_SOURCE/$module_name" 2>/dev/null
  637. chmod a+x ./"$MODULENAME"
  638. fi
  639. CURRENT=$(cat ./"$module_name" 2>/dev/null)
  640. SOURCE=$(curl -Ls "$MODULES_SOURCE/$module_name")
  641. if [ "$CURRENT" = "$SOURCE" ]; then
  642. echo -ne "\r" 2>/dev/null
  643. else
  644. echo " ◆ Updating $module_name..."
  645. curl -Ls "$MODULES_SOURCE/$module_name" > ./"$module_name" 2>/dev/null
  646. fi
  647. done
  648. _clean_old_modules
  649. }
  650. function _sync_amcli() {
  651. echo "$DIVIDING_LINE"
  652. _completion_lists
  653. CURRENT_AM_VERSION="$AMVERSION"
  654. echo -ne "\n ◆ SYNCHRONIZING \"$AMCLIUPPER\" VERSION \"$CURRENT_AM_VERSION\"...\r" && sleep 0.25
  655. _clean_amcachedir 1>/dev/null
  656. cd "$AMCACHEDIR" || return 1
  657. curl -Ls "$AMREPO"/APP-MANAGER > ./APP-MANAGER && chmod a+x ./APP-MANAGER
  658. echo y | mv ./APP-MANAGER "$(realpath "$0")"
  659. NEW_AM_VERSION=$("$AMCLIPATH" -v)
  660. if [ ! "$CURRENT_AM_VERSION" = "$NEW_AM_VERSION" ]; then
  661. echo -ne " A new release of \"$AMCLIUPPER\" is available, please wait...\r"
  662. echo " ◆ \"$AMCLIUPPER\" IS NOW UPDATED TO THE BRAND NEW \"$NEW_AM_VERSION\" VERSION!"
  663. echo -e "\n Replacement of version \"$CURRENT_AM_VERSION\" currently in use, COMPLETED!"
  664. echo -e "\n See https://github.com/ivan-hc/AM/commits/main\n"
  665. else
  666. echo " ◆ \"$AMCLIUPPER\" IS ALREADY UPDATED, CURRENT VERSION \"$CURRENT_AM_VERSION\""
  667. echo -e "\n See https://github.com/ivan-hc/AM/commits/$AMBRANCH\n"
  668. fi
  669. }
  670. function _use_sync() {
  671. _online_check
  672. _betatester_message_on
  673. _sync_installation_scripts
  674. if [ "$(realpath "$0")" != "/usr/bin/am" ]; then
  675. _sync_modules
  676. _sync_amcli
  677. fi
  678. echo "$DIVIDING_LINE"
  679. }
  680. ################################################################################
  681. # UPDATE
  682. ################################################################################
  683. function _update_list_updatable_apps() {
  684. _determine_args
  685. _check_version
  686. for arg in $ARGS; do
  687. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  688. if [ -d "$argpath" ]; then
  689. if [ -f "$argpath/AM-updater" ]; then
  690. app_version=$(grep -w " ◆ $arg |" "$AMCACHEDIR/version-args" | sed 's:.*| ::')
  691. echo " ◆ $arg $app_version" >> "$AMCACHEDIR"/updatable-args-list
  692. fi
  693. fi
  694. done
  695. }
  696. function _update_determine_apps_version_changes() {
  697. if test -f "$AMCACHEDIR"/updatable-args-list; then
  698. mv "$AMCACHEDIR"/updatable-args-list "$AMCACHEDIR"/updatable-args-list-old
  699. _update_list_updatable_apps
  700. OLDVER="$AMCACHEDIR/updatable-args-list-old"
  701. NEWVER="$AMCACHEDIR/updatable-args-list"
  702. if cmp --silent -- "$NEWVER" "$OLDVER"; then
  703. echo ' Nothing to do here!'
  704. else
  705. echo -e " The following apps have been updated:\n"
  706. diff --new-line-format="" --unchanged-line-format="" "$NEWVER" "$OLDVER"
  707. echo ""
  708. fi
  709. else
  710. echo ' No apps to update here!'
  711. fi
  712. }
  713. function _update_updated_app_msg() {
  714. end=$(date +%s)
  715. echo " ◆ $APPNAME is updated, $((end - start)) seconds elapsed!"
  716. }
  717. function _update_run_updater() {
  718. if grep -q "api.github.com" "$argpath"/AM-updater; then
  719. GH_API_ALLOWED=$(curl -Ls $HeaderAuthWithGITPAT https://api.github.com/repos/ivan-hc/AM/releases/latest | sed 's/[()",{} ]/\n/g' | grep "^ivan-hc" | head -1)
  720. if [ -z "$GH_API_ALLOWED" ]; then
  721. if command -v torsocks 1>/dev/null; then
  722. torsocks "$argpath"/AM-updater >/dev/null 2>&1 && _update_updated_app_msg
  723. else
  724. echo " ✖ $APPNAME cannot be updated, you have reached GitHub API limit. Install \"torsocks\" from your system package manager and retry!" \
  725. | fold -sw 72 | sed 's/^/ /g; s/ ✖/✖/g'
  726. fi
  727. else
  728. "$argpath"/AM-updater >/dev/null 2>&1 && _update_updated_app_msg
  729. fi
  730. else
  731. "$argpath"/AM-updater >/dev/null 2>&1 && _update_updated_app_msg
  732. fi
  733. }
  734. function _update_app() {
  735. APPNAME=$(echo "$arg" | tr '[:lower:]' '[:upper:]')
  736. start=$(date +%s)
  737. if [ -w "$argpath"/AM-updater ]; then
  738. _update_run_updater &
  739. else
  740. echo " ✖ $APPNAME is read-only, cannot update it!"
  741. fi
  742. }
  743. function _update_all_apps() {
  744. for arg in $ARGS; do
  745. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  746. cd "$argpath" || exit 1
  747. arg=$(printf '%s\n' "${PWD##*/}")
  748. if test -f "$argpath"/AM-updater; then
  749. _update_app
  750. fi
  751. done
  752. wait
  753. echo "$DIVIDING_LINE"
  754. _update_determine_apps_version_changes
  755. rm -Rf "$APPSPATH"/*/tmp
  756. [ -d "$APPMAN_APPSPATH" ] && rm -Rf "$APPMAN_APPSPATH"/*/tmp
  757. }
  758. function _use_update() {
  759. _online_check
  760. _update_github_api_key_in_the_updater_files
  761. _clean_all_tmp_directories_from_appspath >/dev/null
  762. case $2 in
  763. ''|'--apps')
  764. _clean_amcachedir
  765. _update_list_updatable_apps
  766. echo -e "$DIVIDING_LINE\n \"$AMCLIUPPER\" CAN MANAGE UPDATES FOR THE FOLLOWING PROGRAMS:\n"
  767. [ -f "$AMCACHEDIR/updatable-args-list" ] && grep "◆" "$AMCACHEDIR/updatable-args-list" | sort || echo " None"
  768. echo -e "\n All self-updatable programs are excluded"
  769. echo -e "$DIVIDING_LINE\n >> START OF ALL PROCESSES <<\n$DIVIDING_LINE"
  770. _update_all_apps
  771. [ -z "$2" ] && echo "$DIVIDING_LINE" && _use_sync
  772. echo -e "$DIVIDING_LINE\n >> END OF ALL PROCESSES << \n$DIVIDING_LINE"
  773. sleep 0.2
  774. exit 0
  775. ;;
  776. *)
  777. _determine_args
  778. ENTRIES="$(echo "$@" | cut -f2- -d ' ')"
  779. for arg in $ENTRIES; do
  780. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  781. if test -f "$argpath"/AM-updater; then
  782. cd "$argpath" 2>/dev/null || exit 1
  783. _update_app
  784. else
  785. UPDATERS=$(cd "$argpath" 2>/dev/null && find . -name "*update*" -print 2>/dev/null)
  786. [ -n "$UPDATERS" ] && arg_autoupdatable=", it may have its update system"
  787. echo " ✖ Cannot manage updates for \"$(echo "$arg" | tr '[:lower:]' '[:upper:]')\"$arg_autoupdatable"
  788. fi
  789. done
  790. wait
  791. exit 0
  792. ;;
  793. esac
  794. }
  795. function _use_force_latest() {
  796. _online_check
  797. _determine_args
  798. ENTRIES="$(echo "$@" | cut -f2- -d ' ')"
  799. for arg in $ENTRIES; do
  800. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  801. if ! test -d "$argpath"; then
  802. echo " ERROR: \"$arg\" is not installed, see \"-f\""
  803. elif ! test -f "$argpath"/AM-updater; then
  804. echo " ERROR: \"$AMCLI\" cannot manage updates for \"$arg\""
  805. elif ! grep -q "api.github.com" "$argpath"/AM-updater; then
  806. echo " ERROR: \"$arg\" source is not on Github"
  807. elif ! grep -q "/releases | " "$argpath"/AM-updater; then
  808. echo " ERROR: \"$arg\" does not redirect to a generic \"releases\""
  809. else
  810. sed -i 's#/releases | #/releases/latest | #g' "$argpath"/AM-updater
  811. APPNAME=$(echo "$arg" | tr '[:lower:]' '[:upper:]')
  812. start=$(date +%s)
  813. _update_run_updater
  814. fi
  815. done
  816. }
  817. ################################################################################
  818. # USAGE
  819. ################################################################################
  820. # HANDLE ALL THE EXTERNAL MODULES
  821. function _use_module() {
  822. # Test if module exists
  823. if [ ! -f "$MODULES_PATH/$MODULE" ]; then
  824. _online_check
  825. if ! wget -q "$MODULES_SOURCE/$MODULE" -O "$MODULES_PATH/$MODULE"; then
  826. echo " Module not found, run \"$AMCLI -s\" to update \"$AMCLIUPPER\""
  827. exit 1
  828. fi
  829. chmod a+x "$MODULES_PATH/$MODULE"
  830. fi
  831. # Source module
  832. source "$MODULES_PATH/$MODULE" "$@"
  833. }
  834. case "$1" in
  835. '')
  836. echo " USAGE: $AMCLI [OPTION]"
  837. echo " $AMCLI [OPTION] [ARGUMENT]"
  838. echo ""
  839. echo " Run the \"$AMCLI -h\" command to find out more"
  840. exit 0
  841. ;;
  842. 'about'|'-a'|\
  843. 'files'|'-f'|\
  844. 'list'|'-l'|\
  845. 'query'|'-q')
  846. MODULE="database.am"
  847. if [ -t 1 ]; then _use_module "$@"; else _use_module "$@" | sed -e 's/\x1b\[[0-9;]*m//g'; fi
  848. ;;
  849. 'backup'|'-b'|\
  850. 'downgrade'|'--rollback'|\
  851. 'icons'|'--icons'|\
  852. 'launcher'|'--launcher'|\
  853. 'lock'|'unlock'|\
  854. 'nolibfuse'|\
  855. 'overwrite'|'-o'|\
  856. 'remove'|'-R'|'-r')
  857. MODULE="management.am"
  858. _use_module "$@"
  859. ;;
  860. 'config'|'-C'|'--config'|\
  861. 'home'|'-H'|'--home'|\
  862. 'sandbox'|'--sandbox'|\
  863. '--disable-sandbox')
  864. MODULE="sandboxes.am"
  865. _use_module "$@"
  866. ;;
  867. 'download'|'-d'|\
  868. 'extra'|'-e'|\
  869. 'install'|'-i'|\
  870. 'install-appimage'|'-ia')
  871. MODULE="install.am"
  872. _online_check
  873. [ "$CLI" = am ] && [ -f "$APPMANCONFIG"/appman-mode ] && echo -e "$APPMAN_MSG"
  874. _use_module "$@"
  875. ;;
  876. 'template'|'-t')
  877. MODULE="template.am"
  878. _online_check
  879. _use_module "$@"
  880. ;;
  881. # INBUILT OPTIONS
  882. '--devmode-disable'|'--devmode-enable')
  883. [ "$1" = "--devmode-disable" ] && rm -f "$AMDATADIR"/betatester \
  884. || touch "$AMDATADIR"/betatester && _betatester_message_on
  885. ;;
  886. '--force-latest')
  887. _use_force_latest "$@"
  888. ;;
  889. '--system')
  890. [ -f "$APPMANCONFIG"/appman-mode ] && rm -f "$APPMANCONFIG"/appman-mode && echo -e "$APPMAN_MSG_OFF"
  891. ;;
  892. 'apikey')
  893. _use_apikey "$@"
  894. ;;
  895. 'appman'|'--user')
  896. _use_appman
  897. ;;
  898. 'clean'|'-c')
  899. _use_clean
  900. ;;
  901. 'newrepo'|'neodb')
  902. _use_newrepo "$@"
  903. ;;
  904. 'sync'|'-s')
  905. _use_sync
  906. ;;
  907. 'update'|'-u'|'-U')
  908. _use_update "$@"
  909. ;;
  910. 'version'|'-v'|'--version')
  911. echo "$AMVERSION"
  912. ;;
  913. '--disable-notifications')
  914. _determine_args
  915. for n in $ARGPATHS; do sed -e '/notify-send/ s/^#*/#/' -i "$n/AM-updater" 2>/dev/null; done
  916. ;;
  917. '--enable-notifications')
  918. _determine_args
  919. for n in $ARGPATHS; do sed -e '/notify-send/ s/^#*//' -i "$n/AM-updater" 2>/dev/null; done
  920. ;;
  921. 'help'|'-h')
  922. ################################################################################
  923. # HELP
  924. ################################################################################
  925. function _use_help() {
  926. [ "$CLI" = am ] && [ -f "$APPMANCONFIG"/appman-mode ] && echo -e "$APPMAN_MSG"
  927. echo -e "
  928. NAME: ${Green}$AMCLIUPPER\033[0m VERSION: ${Green}$AMVERSION\033[0m
  929. SYNOPSIS: ${LightBlue}$AMCLI {OPTION}\033[0m
  930. ${LightBlue}$AMCLI {OPTION} {PROGRAM}\033[0m
  931. DESCRIPTION: A command line utility to install and manage AppImages and other portable programs for GNU/Linux thanks to its AUR-inspired database.
  932. OPTIONS:
  933. ${Gold}about, -a\033[0m
  934. ${LightBlue}$AMCLI -a {PROGRAM}\033[0m
  935. Description: Shows more info about one or more apps.
  936. ${Gold}apikey\033[0m
  937. ${LightBlue}$AMCLI apikey {Github Token}
  938. ${LightBlue}$AMCLI apikey delete\033[0m
  939. Description: Accede to github APIs using your personal access tokens. The file named \"ghapikey.txt\" will be saved in $AMDATADIR. Use \"del\" to remove it.
  940. ${Gold}backup, -b\033[0m
  941. ${LightBlue}$AMCLI -b {PROGRAM}\033[0m
  942. Description: Create a snapshot of the current version of an installed program.
  943. ${Gold}clean, -c\033[0m
  944. ${LightBlue}$AMCLI -c\033[0m
  945. Description: Removes all the unnecessary files and folders.
  946. ${Gold}config, -C, --config\033[0m
  947. ${LightBlue}$AMCLI -C {PROGRAM}\033[0m
  948. Description: Set a dedicated \$XDD_CONFIG_HOME for one or more AppImages.
  949. ${Gold}downgrade, --rollback\033[0m
  950. ${LightBlue}$AMCLI --rollback {PROGRAM}\033[0m
  951. Description: Download an older or specific app version.
  952. ${Gold}download, -d\033[0m
  953. ${LightBlue}$AMCLI -d {PROGRAM}
  954. ${LightBlue}$AMCLI -d --convert {PROGRAM}\033[0m
  955. Description: Download one or more installation scripts to your desktop or convert them to local installers for \"AppMan\".
  956. ${Gold}extra, -e\033[0m
  957. ${LightBlue}$AMCLI -e user/project {APPNAME}
  958. ${LightBlue}$AMCLI -e user/project {APPNAME} {KEYWORD}\033[0m
  959. Description: Install AppImages from github.com, outside the database. This allows you to install, update and manage them all like the others. Where \"user/project\" can be the whole URL to the github repository, give a name to the program so that it can be used from the command line. Optionally, add an \"univoque\" keyword if multiple AppImages are listed.
  960. ${Gold}files, -f\033[0m
  961. ${LightBlue}$AMCLI -f
  962. ${LightBlue}$AMCLI -f --byname
  963. ${LightBlue}$AMCLI -f --less\033[0m
  964. Description: Shows the list of all installed programs, with sizes. By default apps are sorted by size, use \"--byname\" to sort by name. With the option \"--less\" it shows only the number of installed apps.
  965. ${Gold}help, -h\033[0m
  966. ${LightBlue}$AMCLI -h\033[0m
  967. Description: Prints this message.
  968. ${Gold}home, -H, --home\033[0m
  969. ${LightBlue}$AMCLI -H {PROGRAM}\033[0m
  970. Description: Set a dedicated \$HOME directory for one or more AppImages.
  971. ${Gold}icons, --icons\033[0m
  972. ${LightBlue}$AMCLI --icons {PROGRAM}
  973. ${LightBlue}$AMCLI --icons --all\033[0m
  974. Description: Allow installed apps to use system icon themes. You can specify the name of the apps to change or use the \"--all\" flag to change all of them at once. This will remove the icon path from the .desktop file and add the symbolic link of all available icons in the $DATADIR/icons/hicolor/scalable/apps directory. The \"--icons\" option can be used as \"flag\" in the \"-i\" and \"-ia\" options.
  975. ${Gold}install, -i\033[0m
  976. ${LightBlue}$AMCLI -i {PROGRAM}
  977. ${LightBlue}$AMCLI -i --debug {PROGRAM}
  978. ${LightBlue}$AMCLI -i --force-latest {PROGRAM}
  979. ${LightBlue}$AMCLI -i --icons {PROGRAM}\033[0m
  980. Description: Install one or more programs or libraries from the list. With the \"--debug\" option you can see log messages to debug the script. For more details on \"--force-latest\", see the dedicated option, below. Use the \"--icons\" flag to allow the program to use icon themes. It can also be extended with additional flags (see \"--toolpack\").
  981. ${Gold}install-appimage, -ia\033[0m
  982. ${LightBlue}$AMCLI -ia {PROGRAM}
  983. ${LightBlue}$AMCLI -ia --debug {PROGRAM}
  984. ${LightBlue}$AMCLI -ia --force-latest {PROGRAM}
  985. ${LightBlue}$AMCLI -ia --icons {PROGRAM}\033[0m
  986. Description: Same as \"install\" (see above) but for AppImages only.
  987. ${Gold}lock\033[0m
  988. ${LightBlue}$AMCLI lock {PROGRAM}\033[0m
  989. Description: Prevent an application being updated, if it has an\"AM-updater\" script.
  990. ${Gold}list, -l\033[0m
  991. ${LightBlue}$AMCLI -l
  992. ${LightBlue}$AMCLI -l --all
  993. ${LightBlue}$AMCLI -l --appimages\033[0m
  994. Description: Shows the list of all the apps available, or just the AppImages. It can also be extended with additional flags, the \"--all\" flag allows you to consult the set of all supported databases (see \"--toolpack\").
  995. ${Gold}newrepo, neodb\033[0m
  996. ${LightBlue}$AMCLI newrepo add {URL}\\{PATH}
  997. ${LightBlue}$AMCLI newrepo select
  998. ${LightBlue}$AMCLI newrepo on\\off
  999. ${LightBlue}$AMCLI newrepo purge
  1000. ${LightBlue}$AMCLI newrepo info\033[0m
  1001. Description: Set a new default repo, use \"add\" to append the path to a local directory or an online URL, then use \"select\" to use it by default, a message will warn you about the usage of this repo instead of the default one. Use \"on\"/\"off\" to enable/disable it. Use \"purge\" to remove all 3rd party repos. Use \"info\" to see the source from where installation scripts and lists are taken.
  1002. ${Gold}nolibfuse\033[0m
  1003. ${LightBlue}$AMCLI nolibfuse {PROGRAM}\033[0m
  1004. Description: Convert old AppImages and get rid of \"libfuse2\" dependence.
  1005. ${Gold}overwrite, -o\033[0m
  1006. ${LightBlue}$AMCLI -o {PROGRAM}\033[0m
  1007. Description: Overwrite apps with snapshots saved previously (see \"-b\").
  1008. ${Gold}query, -q\033[0m
  1009. ${LightBlue}$AMCLI -q {KEYWORD}
  1010. ${LightBlue}$AMCLI -q --all {KEYWORD}
  1011. ${LightBlue}$AMCLI -q --appimages {KEYWORD}
  1012. ${LightBlue}$AMCLI -q --pkg {PROGRAM1} {PROGRAM2}\033[0m
  1013. Description: Search for keywords in the list of available applications, add the \"--appimages\" option to list only the AppImages or add \"--pkg\" to list multiple programs at once. It can also be extended with additional flags, the \"--all\" flag allows you to consult the set of all supported databases (see \"--toolpack\").
  1014. ${Gold}remove, -r\033[0m
  1015. ${LightBlue}$AMCLI -r {PROGRAM}\033[0m
  1016. Description: Removes one or more apps, requires confirmation.
  1017. ${Gold}-R\033[0m
  1018. ${LightBlue}$AMCLI -R {PROGRAM}\033[0m
  1019. Description: Removes one or more apps without asking.
  1020. ${Gold}sandbox, --sandbox\033[0m
  1021. ${LightBlue}$AMCLI sandbox {PROGRAM}\033[0m
  1022. Description: Run an AppImage in a sandbox using Aisap.
  1023. ${Gold}sync, -s\033[0m
  1024. ${LightBlue}$AMCLI -s\033[0m
  1025. Description: Updates this script to the latest version hosted.
  1026. ${Gold}template, -t\033[0m
  1027. ${LightBlue}$AMCLI -t {PROGRAM}\033[0m
  1028. Description: Generate a custom installation script.
  1029. ${Gold}unlock\033[0m
  1030. ${LightBlue}$AMCLI unlock {PROGRAM}\033[0m
  1031. Description: Unlock updates for the selected program (nulls \"lock\").
  1032. ${Gold}update, -u, -U\033[0m
  1033. ${LightBlue}$AMCLI -u
  1034. ${LightBlue}$AMCLI -u --apps
  1035. ${LightBlue}$AMCLI -u {PROGRAM}\033[0m
  1036. Description: Update everything. Add \"--apps\" to update only the apps or write only the apps you want to update by adding their names.
  1037. ${Gold}version, -v\033[0m
  1038. ${LightBlue}$AMCLI -v\033[0m
  1039. Description: Shows the version.
  1040. ${Gold}--devmode-disable\033[0m
  1041. ${LightBlue}$AMCLI --devmode-disable\033[0m
  1042. Description: Undo \"--devmode-enable\" (see below).
  1043. ${Gold}--devmode-enable\033[0m
  1044. ${LightBlue}$AMCLI --devmode-enable\033[0m
  1045. Description: Use the development branch (at your own risk).
  1046. ${Gold}--disable-notifications\033[0m
  1047. ${LightBlue}$AMCLI --disable-notifications\033[0m
  1048. Description: Disable notifications during apps update.
  1049. ${Gold}--disable-sandbox\033[0m
  1050. ${LightBlue}$AMCLI --disable-sandbox {PROGRAM}\033[0m
  1051. Description: Disable the sandbox for the selected app.
  1052. ${Gold}--enable-notifications\033[0m
  1053. ${LightBlue}$AMCLI --enable-notifications\033[0m
  1054. Description: Eable notifications during apps update (nulls \"--disable-notifications\").
  1055. ${Gold}--force-latest\033[0m
  1056. ${LightBlue}$AMCLI --force-latest {PROGRAM}\033[0m
  1057. Description: Downgrades an installed app from pre-release to \"latest\".
  1058. ${Gold}--launcher\033[0m
  1059. ${LightBlue}$AMCLI --launcher /path/to/\${APPIMAGE}\033[0m
  1060. Description: Drag/drop one or more AppImages in the terminal and embed them in the apps menu and customize a command to use from the CLI.
  1061. ${Gold}--system\033[0m
  1062. ${LightBlue}am --system\033[0m
  1063. Description: Switch \"AM\" back to \"AM\" from \"AppMan Mode\" (see \"--user\").
  1064. ${Gold}--toolpack\033[0m
  1065. ${LightBlue}$AMCLI -i --toolpack {PROGRAM}\033[0m
  1066. ${LightBlue}am -i --toolpack --user {PROGRAM}
  1067. ${LightBlue}$AMCLI -l --toolpack
  1068. ${LightBlue}$AMCLI -q --toolpack {KEYWORD}\033[0m
  1069. Description: This is a flag to use in \"-i\" to install Toolpack programs, in \"-l\" to list all available Toolpacks, and \"-q\" to search the Toolpack list. Toolpack is a collection of programs external to the \"AM\" database. Visit $toolpack_repo to learn more.
  1070. NOTE, for installations you can use \".toolpack\" as the package extension instead of using the flag.
  1071. ${Gold}--user\033[0m
  1072. ${LightBlue}am --user\033[0m
  1073. Description: Made \"AM\" run in \"AppMan Mode\", locally, useful for unprivileged users. This option only works with \"AM\".
  1074. The \"--user\" option can also be used just as a flag for installation options. For example:
  1075. - Use it to install applications locally, option \"-i\" or \"install\":
  1076. ${LightBlue}am -i --user {PROGRAM}\033[0m
  1077. - Also suboptions of \"-i\" can work with this flag:
  1078. ${LightBlue}am -i --user --debug {PROGRAM}\033[0m
  1079. ${LightBlue}am -i --user --force-latest {PROGRAM}
  1080. ${LightBlue}am -i --user --icons {PROGRAM}
  1081. ${LightBlue}am -i --user --debug --force-latest {PROGRAM}
  1082. ${LightBlue}am -i --user --debug --force-latest --icons {PROGRAM}\033[0m
  1083. - Same for AppImages only, option \"-ia\" or \"install-appimage\":
  1084. ${LightBlue}am -ia --user {PROGRAM}\033[0m
  1085. ${LightBlue}am -ia --user --debug {PROGRAM}
  1086. ${LightBlue}am -ia --user --force-latest {PROGRAM}
  1087. ${LightBlue}am -ia --user --icons {PROGRAM}
  1088. ${LightBlue}am -ia --user --debug --force-latest {PROGRAM}
  1089. ${LightBlue}am -ia --user --debug --force-latest --icons {PROGRAM}\033[0m
  1090. - External AppImages can be installed like this as well, option \"-e\" or \"extra\":
  1091. ${LightBlue}am -e --user user/project {APPNAME}
  1092. ${LightBlue}am -e --user user/project {APPNAME} {KEYWORD}\033[0m
  1093. NOTE, \"AM\" 9 or higher is also able to, update and manage apps locally, by default, and without having to switch to \"AppMan Mode\".
  1094. $DIVIDING_LINE
  1095. SITES:
  1096. https://github.com/ivan-hc/AM
  1097. https://portable-linux-apps.github.io
  1098. \n" | sed 's/^ //g' | fold -sw 77 | sed 's/^/ /g' | less -Ir
  1099. }
  1100. if [ -t 1 ]; then _use_help; else _use_help | sed -e 's/\x1b\[[0-9;]*m//g'; fi
  1101. ;;
  1102. *)
  1103. exec "$AMCLIPATH"
  1104. ;;
  1105. esac
  1106. # vim:tabstop=4:shiftwidth=4:expandtab