APP-MANAGER 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. #!/usr/bin/env bash
  2. AMVERSION="9.1.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" -maxdepth 2 -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" -maxdepth 2 -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. 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 "$AMDATADIR"/"$ARCH"-toolpack ] && awk '{print $2}' "$AMDATADIR"/"$ARCH"-toolpack | sed -e 's/$/.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. for arg in $ARGPATHS; do
  533. if test -d "$arg"/*.home/.cache; then
  534. rm -Rf "$arg"/*/*.home/.cache/* && echo " ✔ Clear the contents of $arg/*.home/.cache"
  535. fi
  536. done
  537. }
  538. function _clean_all_tmp_directories_from_appspath() {
  539. for arg in $ARGPATHS; do
  540. if test -d "$arg"/tmp; then
  541. rm -Rf "$arg"/tmp && echo " ✔ Removed $arg/tmp"
  542. fi
  543. done
  544. }
  545. function _clean_determine_removable_launchers() {
  546. if ! test -f "$APPIMAGENAME" 2>/dev/null; then
  547. if ! test -d "$MOUNTPOINTS" 2>/dev/null; then
  548. if echo "$MOUNTPOINTS" | grep -q "/media/"; then
  549. unmounted_poin="/media"
  550. elif echo "$MOUNTPOINTS" | grep -q "/mnt/"; then
  551. unmounted_poin="/mnt"
  552. fi
  553. echo " ✖ ERROR: cannot remove \"$(basename "$var")\""
  554. echo " related AppImage is located in an unmounted path of $unmounted_poin"
  555. else
  556. rm -f "$var"
  557. rm -f "$HOME"/.local/bin/"$launcher2del"*
  558. cd "$HOME"/.local/bin && find . -xtype l -delete
  559. fi
  560. fi
  561. }
  562. function _clean_launchers() {
  563. if test -d "$DATADIR"/applications/AppImages 2>/dev/null; then
  564. for var in "$DATADIR"/applications/AppImages/*.desktop; do
  565. APPIMAGENAME=$(grep "Exec=" 0<"$var" 2>/dev/null | head -1 | cut -c 6- | sed 's/\s.*$//')
  566. launcher2del=$(basename -- "$(echo "$APPIMAGENAME" | tr '[:upper:]' '[:lower:]')")
  567. MOUNTPOINTS=$(echo "$APPIMAGENAME" | cut -d'/' -f1-4)
  568. _clean_determine_removable_launchers
  569. done
  570. echo ' ✔ Removed orphaned launchers produced with the "--launcher" option'
  571. rmdir "$DATADIR"/applications/AppImages
  572. fi
  573. }
  574. function _clean_old_modules() {
  575. MODULES=$(sort "$(realpath "$0")" | tr '"' '\n' | grep "[a-z]\.am$" | uniq)
  576. [ -z "$MODULES_PATH" ] && exit 1
  577. for m in "$MODULES_PATH"/*; do
  578. if [[ "${MODULES}" != *"$(basename -- "$m")"* ]];then
  579. rm -f "$m" 2>/dev/null
  580. echo " ✔ Removed obsolete module named \"$(basename -- "$m")\""
  581. fi
  582. done
  583. }
  584. function _use_clean() {
  585. echo " Cleaning temporary files and folders..." && sleep 0.1
  586. for i in {100..000}; do
  587. echo -ne " $i\r" && sleep 0.0001
  588. done
  589. _detect_appman_apps
  590. _determine_args
  591. _clean_amcachedir_message
  592. _clean_all_home_cache_directories_of_appimages
  593. _clean_all_tmp_directories_from_appspath
  594. _clean_launchers 2>/dev/null
  595. _clean_old_modules
  596. }
  597. ################################################################################
  598. # SYNC
  599. ################################################################################
  600. function _sync_installation_scripts() {
  601. echo -e "$DIVIDING_LINE\n Checking for changes of the installation scripts in the online database..."
  602. _determine_args
  603. for arg in $ARGS; do
  604. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  605. if [ -f "$argpath"/AM-updater ]; then
  606. mkdir -p "$argpath"/.am-installer
  607. if test -f "$argpath/.am-installer"/*; then
  608. scriptname=$(ls "$argpath/.am-installer/" | head -1)
  609. CURRENT=$(cat "$argpath"/.am-installer/"$scriptname")
  610. SOURCE=$(curl -Ls "$APPSDB"/"$scriptname")
  611. if [ "$CURRENT" = "$SOURCE" ]; then
  612. echo -ne "\r" 2>/dev/null
  613. else
  614. echo -e " ◆ Changed https://github.com/ivan-hc/AM/blob/main/programs/$arch/$scriptname"
  615. fi
  616. else
  617. if curl --output /dev/null --silent --head --fail "$APPSDB"/"$arg" 1>/dev/null; then
  618. echo -e " ◆ No installation script for $arg, downloading one..."
  619. mkdir -p "$argpath"/.am-installer
  620. wget -q "$APPSDB/$arg" -O "$argpath"/.am-installer/"$arg"
  621. fi
  622. fi
  623. fi
  624. done
  625. }
  626. function _sync_appimages_list() {
  627. rm -f "$AMDATADIR/$ARCH-appimages"
  628. APPIMAGE_NAMES=$(curl -Ls https://portable-linux-apps.github.io/appimages.md | tr '/)' '\n' | grep -i ".md$" | uniq | sed 's/.md$//g' | grep -v "\[")
  629. for appimage in $APPIMAGE_NAMES; do
  630. grep "◆ $appimage :" "$AMDATADIR/$ARCH-apps" >> "$AMDATADIR/$ARCH-appimages" &
  631. done
  632. wait
  633. }
  634. function _sync_toolpacks_list() {
  635. rm -f "$AMDATADIR/$ARCH-toolpack"
  636. curl -Ls "$toolpack_readme" | grep -v "\.appimage " | cut -d":" -f1 | cut -d"@" -f2 | sed 's/^| /◆ /g; s/ | https$/./g; s/ | / : /g' > "$AMDATADIR/$ARCH-toolpack"
  637. }
  638. function _sync_databases() {
  639. echo -e "$DIVIDING_LINE\n Check and update offline lists of additional databases..."
  640. _sync_appimages_list
  641. _sync_toolpacks_list
  642. _completion_lists
  643. }
  644. function _sync_modules() {
  645. echo -e "$DIVIDING_LINE\n Check for updates in modules..."
  646. MODULES=$(curl -Ls "$AMREPO/APP-MANAGER" | tr '"' '\n' | grep "[a-z]\.am$")
  647. for module_name in $MODULES; do
  648. cd "$MODULES_PATH" || return 1
  649. if ! test -f ./"$module_name"; then
  650. echo " ◆ Downloading $module_name (not previously installed)..."
  651. curl -Os "$MODULES_SOURCE/$module_name" 2>/dev/null
  652. chmod a+x ./"$MODULENAME"
  653. fi
  654. CURRENT=$(cat ./"$module_name" 2>/dev/null)
  655. SOURCE=$(curl -Ls "$MODULES_SOURCE/$module_name")
  656. if [ "$CURRENT" = "$SOURCE" ]; then
  657. echo -ne "\r" 2>/dev/null
  658. else
  659. echo " ◆ Updating $module_name..."
  660. curl -Ls "$MODULES_SOURCE/$module_name" > ./"$module_name" 2>/dev/null
  661. fi
  662. done
  663. _clean_old_modules
  664. }
  665. function _sync_amcli() {
  666. echo "$DIVIDING_LINE"
  667. CURRENT_AM_VERSION="$AMVERSION"
  668. echo -ne "\n ◆ SYNCHRONIZING \"$AMCLIUPPER\" VERSION \"$CURRENT_AM_VERSION\"...\r" && sleep 0.25
  669. _clean_amcachedir 1>/dev/null
  670. cd "$AMCACHEDIR" || return 1
  671. curl -Ls "$AMREPO"/APP-MANAGER > ./APP-MANAGER && chmod a+x ./APP-MANAGER
  672. echo y | mv ./APP-MANAGER "$(realpath "$0")"
  673. NEW_AM_VERSION=$("$AMCLIPATH" -v)
  674. if [ ! "$CURRENT_AM_VERSION" = "$NEW_AM_VERSION" ]; then
  675. echo -ne " A new release of \"$AMCLIUPPER\" is available, please wait...\r"
  676. echo " ◆ \"$AMCLIUPPER\" IS NOW UPDATED TO THE BRAND NEW \"$NEW_AM_VERSION\" VERSION!"
  677. echo -e "\n Replacement of version \"$CURRENT_AM_VERSION\" currently in use, COMPLETED!"
  678. echo -e "\n See https://github.com/ivan-hc/AM/commits/main\n"
  679. else
  680. echo " ◆ \"$AMCLIUPPER\" IS ALREADY UPDATED, CURRENT VERSION \"$CURRENT_AM_VERSION\""
  681. echo -e "\n See https://github.com/ivan-hc/AM/commits/$AMBRANCH\n"
  682. fi
  683. }
  684. function _use_sync() {
  685. _online_check
  686. _betatester_message_on
  687. _sync_installation_scripts
  688. _sync_databases
  689. if [ "$(realpath "$0")" != "/usr/bin/am" ]; then
  690. _sync_modules
  691. _sync_amcli
  692. fi
  693. echo "$DIVIDING_LINE"
  694. }
  695. ################################################################################
  696. # UPDATE
  697. ################################################################################
  698. function _update_list_updatable_apps() {
  699. _determine_args
  700. _check_version
  701. for arg in $ARGS; do
  702. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  703. if [ -d "$argpath" ]; then
  704. if [ -f "$argpath/AM-updater" ]; then
  705. app_version=$(grep -w " ◆ $arg |" "$AMCACHEDIR/version-args" | sed 's:.*| ::')
  706. echo " ◆ $arg $app_version" >> "$AMCACHEDIR"/updatable-args-list
  707. fi
  708. fi
  709. done
  710. }
  711. function _update_determine_apps_version_changes() {
  712. if test -f "$AMCACHEDIR"/updatable-args-list; then
  713. mv "$AMCACHEDIR"/updatable-args-list "$AMCACHEDIR"/updatable-args-list-old
  714. _update_list_updatable_apps
  715. OLDVER="$AMCACHEDIR/updatable-args-list-old"
  716. NEWVER="$AMCACHEDIR/updatable-args-list"
  717. if cmp --silent -- "$NEWVER" "$OLDVER"; then
  718. echo ' Nothing to do here!'
  719. else
  720. echo -e " The following apps have been updated:\n"
  721. diff --new-line-format="" --unchanged-line-format="" "$NEWVER" "$OLDVER"
  722. echo ""
  723. fi
  724. else
  725. echo ' No apps to update here!'
  726. fi
  727. }
  728. function _update_updated_app_msg() {
  729. end=$(date +%s)
  730. echo " ◆ $APPNAME is updated, $((end - start)) seconds elapsed!"
  731. }
  732. function _update_run_updater() {
  733. if grep -q "api.github.com" "$argpath"/AM-updater; then
  734. 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)
  735. if [ -z "$GH_API_ALLOWED" ]; then
  736. if command -v torsocks 1>/dev/null; then
  737. torsocks "$argpath"/AM-updater >/dev/null 2>&1 && _update_updated_app_msg
  738. else
  739. echo " ✖ $APPNAME cannot be updated, you have reached GitHub API limit. Install \"torsocks\" from your system package manager and retry!" \
  740. | fold -sw 72 | sed 's/^/ /g; s/ ✖/✖/g'
  741. fi
  742. else
  743. "$argpath"/AM-updater >/dev/null 2>&1 && _update_updated_app_msg
  744. fi
  745. else
  746. "$argpath"/AM-updater >/dev/null 2>&1 && _update_updated_app_msg
  747. fi
  748. }
  749. function _update_app() {
  750. APPNAME=$(echo "$arg" | tr '[:lower:]' '[:upper:]')
  751. start=$(date +%s)
  752. if [ -w "$argpath"/AM-updater ]; then
  753. _update_run_updater &
  754. else
  755. echo " ✖ $APPNAME is read-only, cannot update it!"
  756. fi
  757. }
  758. function _update_all_apps() {
  759. for arg in $ARGS; do
  760. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  761. cd "$argpath" || exit 1
  762. arg=$(printf '%s\n' "${PWD##*/}")
  763. if test -f "$argpath"/AM-updater; then
  764. _update_app
  765. fi
  766. done
  767. wait
  768. echo "$DIVIDING_LINE"
  769. _update_determine_apps_version_changes
  770. rm -Rf "$APPSPATH"/*/tmp
  771. [ -d "$APPMAN_APPSPATH" ] && rm -Rf "$APPMAN_APPSPATH"/*/tmp
  772. }
  773. function _use_update() {
  774. _online_check
  775. _update_github_api_key_in_the_updater_files
  776. _clean_all_tmp_directories_from_appspath >/dev/null
  777. case $2 in
  778. ''|'--apps')
  779. _clean_amcachedir
  780. _update_list_updatable_apps
  781. echo -e "$DIVIDING_LINE\n \"$AMCLIUPPER\" CAN MANAGE UPDATES FOR THE FOLLOWING PROGRAMS:\n"
  782. [ -f "$AMCACHEDIR/updatable-args-list" ] && grep "◆" "$AMCACHEDIR/updatable-args-list" | sort || echo " None"
  783. echo -e "\n All self-updatable programs are excluded"
  784. echo -e "$DIVIDING_LINE\n >> START OF ALL PROCESSES <<\n$DIVIDING_LINE"
  785. _update_all_apps
  786. [ -z "$2" ] && echo "$DIVIDING_LINE" && _use_sync
  787. echo -e "$DIVIDING_LINE\n >> END OF ALL PROCESSES << \n$DIVIDING_LINE"
  788. sleep 0.2
  789. exit 0
  790. ;;
  791. *)
  792. _determine_args
  793. ENTRIES="$(echo "$@" | cut -f2- -d ' ')"
  794. for arg in $ENTRIES; do
  795. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  796. if test -f "$argpath"/AM-updater; then
  797. cd "$argpath" 2>/dev/null || exit 1
  798. _update_app
  799. else
  800. UPDATERS=$(cd "$argpath" 2>/dev/null && find . -name "*update*" -print 2>/dev/null)
  801. [ -n "$UPDATERS" ] && arg_autoupdatable=", it may have its update system"
  802. echo " ✖ Cannot manage updates for \"$(echo "$arg" | tr '[:lower:]' '[:upper:]')\"$arg_autoupdatable"
  803. fi
  804. done
  805. wait
  806. exit 0
  807. ;;
  808. esac
  809. }
  810. function _use_force_latest() {
  811. _online_check
  812. _determine_args
  813. ENTRIES="$(echo "$@" | cut -f2- -d ' ')"
  814. for arg in $ENTRIES; do
  815. argpath=$(echo "$ARGPATHS" | grep "/$arg$")
  816. if ! test -d "$argpath"; then
  817. echo " ERROR: \"$arg\" is not installed, see \"-f\""
  818. elif ! test -f "$argpath"/AM-updater; then
  819. echo " ERROR: \"$AMCLI\" cannot manage updates for \"$arg\""
  820. elif ! grep -q "api.github.com" "$argpath"/AM-updater; then
  821. echo " ERROR: \"$arg\" source is not on Github"
  822. elif ! grep -q "/releases | " "$argpath"/AM-updater; then
  823. echo " ERROR: \"$arg\" does not redirect to a generic \"releases\""
  824. else
  825. sed -i 's#/releases | #/releases/latest | #g' "$argpath"/AM-updater
  826. APPNAME=$(echo "$arg" | tr '[:lower:]' '[:upper:]')
  827. start=$(date +%s)
  828. _update_run_updater
  829. fi
  830. done
  831. }
  832. ################################################################################
  833. # USAGE
  834. ################################################################################
  835. # HANDLE ALL THE EXTERNAL MODULES
  836. function _use_module() {
  837. # Test if module exists
  838. if [ ! -f "$MODULES_PATH/$MODULE" ]; then
  839. _online_check
  840. if ! wget -q "$MODULES_SOURCE/$MODULE" -O "$MODULES_PATH/$MODULE"; then
  841. echo " Module not found, run \"$AMCLI -s\" to update \"$AMCLIUPPER\""
  842. exit 1
  843. fi
  844. chmod a+x "$MODULES_PATH/$MODULE"
  845. fi
  846. # Source module
  847. source "$MODULES_PATH/$MODULE" "$@"
  848. }
  849. case "$1" in
  850. '')
  851. echo " USAGE: $AMCLI [OPTION]"
  852. echo " $AMCLI [OPTION] [ARGUMENT]"
  853. echo ""
  854. echo " Run the \"$AMCLI -h\" command to find out more"
  855. exit 0
  856. ;;
  857. 'about'|'-a'|\
  858. 'files'|'-f'|\
  859. 'list'|'-l'|\
  860. 'query'|'-q')
  861. MODULE="database.am"
  862. if [ -t 1 ]; then _use_module "$@"; else _use_module "$@" | sed -e 's/\x1b\[[0-9;]*m//g'; fi
  863. ;;
  864. 'backup'|'-b'|\
  865. 'downgrade'|'--rollback'|\
  866. 'icons'|'--icons'|\
  867. 'launcher'|'--launcher'|\
  868. 'lock'|'unlock'|\
  869. 'nolibfuse'|\
  870. 'overwrite'|'-o'|\
  871. 'remove'|'-R'|'-r')
  872. MODULE="management.am"
  873. _use_module "$@"
  874. ;;
  875. 'config'|'-C'|'--config'|\
  876. 'home'|'-H'|'--home'|\
  877. 'sandbox'|'--sandbox'|\
  878. '--disable-sandbox')
  879. MODULE="sandboxes.am"
  880. _use_module "$@"
  881. ;;
  882. 'download'|'-d'|\
  883. 'extra'|'-e'|\
  884. 'install'|'-i'|\
  885. 'install-appimage'|'-ia')
  886. MODULE="install.am"
  887. _online_check
  888. [ "$CLI" = am ] && [ -f "$APPMANCONFIG"/appman-mode ] && echo -e "$APPMAN_MSG"
  889. _use_module "$@"
  890. ;;
  891. 'template'|'-t')
  892. MODULE="template.am"
  893. _online_check
  894. _use_module "$@"
  895. ;;
  896. # INBUILT OPTIONS
  897. '--devmode-disable'|'--devmode-enable')
  898. [ "$1" = "--devmode-disable" ] && rm -f "$AMDATADIR"/betatester \
  899. || touch "$AMDATADIR"/betatester && _betatester_message_on
  900. ;;
  901. '--force-latest')
  902. _use_force_latest "$@"
  903. ;;
  904. '--system')
  905. [ -f "$APPMANCONFIG"/appman-mode ] && rm -f "$APPMANCONFIG"/appman-mode && echo -e "$APPMAN_MSG_OFF"
  906. ;;
  907. 'apikey')
  908. _use_apikey "$@"
  909. ;;
  910. 'appman'|'--user')
  911. _use_appman
  912. ;;
  913. 'clean'|'-c')
  914. _use_clean
  915. ;;
  916. 'newrepo'|'neodb')
  917. _use_newrepo "$@"
  918. ;;
  919. 'sync'|'-s')
  920. _use_sync
  921. ;;
  922. 'update'|'-u'|'-U')
  923. _use_update "$@"
  924. ;;
  925. 'version'|'-v'|'--version')
  926. echo "$AMVERSION"
  927. ;;
  928. '--disable-notifications')
  929. _determine_args
  930. for n in $ARGPATHS; do sed -e '/notify-send/ s/^#*/#/' -i "$n/AM-updater" 2>/dev/null; done
  931. ;;
  932. '--enable-notifications')
  933. _determine_args
  934. for n in $ARGPATHS; do sed -e '/notify-send/ s/^#*//' -i "$n/AM-updater" 2>/dev/null; done
  935. ;;
  936. 'help'|'-h')
  937. ################################################################################
  938. # HELP
  939. ################################################################################
  940. function _use_help() {
  941. [ "$CLI" = am ] && [ -f "$APPMANCONFIG"/appman-mode ] && echo -e "$APPMAN_MSG"
  942. echo -e "
  943. NAME: ${Green}$AMCLIUPPER\033[0m VERSION: ${Green}$AMVERSION\033[0m
  944. SYNOPSIS: ${LightBlue}$AMCLI {OPTION}\033[0m
  945. ${LightBlue}$AMCLI {OPTION} {PROGRAM}\033[0m
  946. DESCRIPTION: A command line utility to install and manage AppImages and other portable programs for GNU/Linux thanks to its AUR-inspired database.
  947. OPTIONS:
  948. ${Gold}about, -a\033[0m
  949. ${LightBlue}$AMCLI -a {PROGRAM}\033[0m
  950. Description: Shows more info about one or more apps.
  951. ${Gold}apikey\033[0m
  952. ${LightBlue}$AMCLI apikey {Github Token}
  953. ${LightBlue}$AMCLI apikey delete\033[0m
  954. 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.
  955. ${Gold}backup, -b\033[0m
  956. ${LightBlue}$AMCLI -b {PROGRAM}\033[0m
  957. Description: Create a snapshot of the current version of an installed program.
  958. ${Gold}clean, -c\033[0m
  959. ${LightBlue}$AMCLI -c\033[0m
  960. Description: Removes all the unnecessary files and folders.
  961. ${Gold}config, -C, --config\033[0m
  962. ${LightBlue}$AMCLI -C {PROGRAM}\033[0m
  963. Description: Set a dedicated \$XDD_CONFIG_HOME for one or more AppImages.
  964. ${Gold}downgrade, --rollback\033[0m
  965. ${LightBlue}$AMCLI --rollback {PROGRAM}\033[0m
  966. Description: Download an older or specific app version.
  967. ${Gold}download, -d\033[0m
  968. ${LightBlue}$AMCLI -d {PROGRAM}
  969. ${LightBlue}$AMCLI -d --convert {PROGRAM}\033[0m
  970. Description: Download one or more installation scripts to your desktop or convert them to local installers for \"AppMan\".
  971. ${Gold}extra, -e\033[0m
  972. ${LightBlue}$AMCLI -e user/project {APPNAME}
  973. ${LightBlue}$AMCLI -e user/project {APPNAME} {KEYWORD}\033[0m
  974. 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.
  975. ${Gold}files, -f\033[0m
  976. ${LightBlue}$AMCLI -f
  977. ${LightBlue}$AMCLI -f --byname
  978. ${LightBlue}$AMCLI -f --less\033[0m
  979. 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.
  980. ${Gold}help, -h\033[0m
  981. ${LightBlue}$AMCLI -h\033[0m
  982. Description: Prints this message.
  983. ${Gold}home, -H, --home\033[0m
  984. ${LightBlue}$AMCLI -H {PROGRAM}\033[0m
  985. Description: Set a dedicated \$HOME directory for one or more AppImages.
  986. ${Gold}icons, --icons\033[0m
  987. ${LightBlue}$AMCLI --icons {PROGRAM}
  988. ${LightBlue}$AMCLI --icons --all\033[0m
  989. 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.
  990. ${Gold}install, -i\033[0m
  991. ${LightBlue}$AMCLI -i {PROGRAM}
  992. ${LightBlue}$AMCLI -i --debug {PROGRAM}
  993. ${LightBlue}$AMCLI -i --force-latest {PROGRAM}
  994. ${LightBlue}$AMCLI -i --icons {PROGRAM}\033[0m
  995. 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\").
  996. ${Gold}install-appimage, -ia\033[0m
  997. ${LightBlue}$AMCLI -ia {PROGRAM}
  998. ${LightBlue}$AMCLI -ia --debug {PROGRAM}
  999. ${LightBlue}$AMCLI -ia --force-latest {PROGRAM}
  1000. ${LightBlue}$AMCLI -ia --icons {PROGRAM}\033[0m
  1001. Description: Same as \"install\" (see above) but for AppImages only.
  1002. ${Gold}lock\033[0m
  1003. ${LightBlue}$AMCLI lock {PROGRAM}\033[0m
  1004. Description: Prevent an application being updated, if it has an\"AM-updater\" script.
  1005. ${Gold}list, -l\033[0m
  1006. ${LightBlue}$AMCLI -l
  1007. ${LightBlue}$AMCLI -l --all
  1008. ${LightBlue}$AMCLI -l --appimages
  1009. ${LightBlue}$AMCLI -l --toolpack {KEYWORD}\033[0m
  1010. 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\").
  1011. ${Gold}newrepo, neodb\033[0m
  1012. ${LightBlue}$AMCLI newrepo add {URL}\\{PATH}
  1013. ${LightBlue}$AMCLI newrepo select
  1014. ${LightBlue}$AMCLI newrepo on\\off
  1015. ${LightBlue}$AMCLI newrepo purge
  1016. ${LightBlue}$AMCLI newrepo info\033[0m
  1017. 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.
  1018. ${Gold}nolibfuse\033[0m
  1019. ${LightBlue}$AMCLI nolibfuse {PROGRAM}\033[0m
  1020. Description: Convert old AppImages and get rid of \"libfuse2\" dependence.
  1021. ${Gold}overwrite, -o\033[0m
  1022. ${LightBlue}$AMCLI -o {PROGRAM}\033[0m
  1023. Description: Overwrite apps with snapshots saved previously (see \"-b\").
  1024. ${Gold}query, -q\033[0m
  1025. ${LightBlue}$AMCLI -q {KEYWORD}
  1026. ${LightBlue}$AMCLI -q --all {KEYWORD}
  1027. ${LightBlue}$AMCLI -q --appimages {KEYWORD}
  1028. ${LightBlue}$AMCLI -q --pkg {PROGRAM1} {PROGRAM2}
  1029. ${LightBlue}$AMCLI -q --toolpack {KEYWORD}\033[0m
  1030. 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\").
  1031. ${Gold}remove, -r\033[0m
  1032. ${LightBlue}$AMCLI -r {PROGRAM}\033[0m
  1033. Description: Removes one or more apps, requires confirmation.
  1034. ${Gold}-R\033[0m
  1035. ${LightBlue}$AMCLI -R {PROGRAM}\033[0m
  1036. Description: Removes one or more apps without asking.
  1037. ${Gold}sandbox, --sandbox\033[0m
  1038. ${LightBlue}$AMCLI sandbox {PROGRAM}\033[0m
  1039. Description: Run an AppImage in a sandbox using Aisap.
  1040. ${Gold}sync, -s\033[0m
  1041. ${LightBlue}$AMCLI -s\033[0m
  1042. Description: Updates this script to the latest version hosted.
  1043. ${Gold}template, -t\033[0m
  1044. ${LightBlue}$AMCLI -t {PROGRAM}\033[0m
  1045. Description: Generate a custom installation script.
  1046. ${Gold}unlock\033[0m
  1047. ${LightBlue}$AMCLI unlock {PROGRAM}\033[0m
  1048. Description: Unlock updates for the selected program (nulls \"lock\").
  1049. ${Gold}update, -u, -U\033[0m
  1050. ${LightBlue}$AMCLI -u
  1051. ${LightBlue}$AMCLI -u --apps
  1052. ${LightBlue}$AMCLI -u {PROGRAM}\033[0m
  1053. Description: Update everything. Add \"--apps\" to update only the apps or write only the apps you want to update by adding their names.
  1054. ${Gold}version, -v\033[0m
  1055. ${LightBlue}$AMCLI -v\033[0m
  1056. Description: Shows the version.
  1057. ${Gold}--devmode-disable\033[0m
  1058. ${LightBlue}$AMCLI --devmode-disable\033[0m
  1059. Description: Undo \"--devmode-enable\" (see below).
  1060. ${Gold}--devmode-enable\033[0m
  1061. ${LightBlue}$AMCLI --devmode-enable\033[0m
  1062. Description: Use the development branch (at your own risk).
  1063. ${Gold}--disable-notifications\033[0m
  1064. ${LightBlue}$AMCLI --disable-notifications\033[0m
  1065. Description: Disable notifications during apps update.
  1066. ${Gold}--disable-sandbox\033[0m
  1067. ${LightBlue}$AMCLI --disable-sandbox {PROGRAM}\033[0m
  1068. Description: Disable the sandbox for the selected app.
  1069. ${Gold}--enable-notifications\033[0m
  1070. ${LightBlue}$AMCLI --enable-notifications\033[0m
  1071. Description: Eable notifications during apps update (nulls \"--disable-notifications\").
  1072. ${Gold}--force-latest\033[0m
  1073. ${LightBlue}$AMCLI --force-latest {PROGRAM}\033[0m
  1074. Description: Downgrades an installed app from pre-release to \"latest\".
  1075. ${Gold}--launcher\033[0m
  1076. ${LightBlue}$AMCLI --launcher /path/to/\${APPIMAGE}\033[0m
  1077. 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.
  1078. ${Gold}--system\033[0m
  1079. ${LightBlue}am --system\033[0m
  1080. Description: Switch \"AM\" back to \"AM\" from \"AppMan Mode\" (see \"--user\").
  1081. ${Gold}--toolpack\033[0m
  1082. ${LightBlue}$AMCLI -i --toolpack {PROGRAM}\033[0m
  1083. ${LightBlue}am -i --toolpack --user {PROGRAM}
  1084. ${LightBlue}$AMCLI -l --toolpack
  1085. ${LightBlue}$AMCLI -q --toolpack {KEYWORD}\033[0m
  1086. 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.
  1087. NOTE, for installations you can use \".toolpack\" as the package extension instead of using the flag.
  1088. ${Gold}--user\033[0m
  1089. ${LightBlue}am --user\033[0m
  1090. Description: Made \"AM\" run in \"AppMan Mode\", locally, useful for unprivileged users. This option only works with \"AM\".
  1091. The \"--user\" option can also be used just as a flag for installation options. For example:
  1092. - Use it to install applications locally, option \"-i\" or \"install\":
  1093. ${LightBlue}am -i --user {PROGRAM}\033[0m
  1094. - Also suboptions of \"-i\" can work with this flag:
  1095. ${LightBlue}am -i --user --debug {PROGRAM}\033[0m
  1096. ${LightBlue}am -i --user --force-latest {PROGRAM}
  1097. ${LightBlue}am -i --user --icons {PROGRAM}
  1098. ${LightBlue}am -i --user --debug --force-latest {PROGRAM}
  1099. ${LightBlue}am -i --user --debug --force-latest --icons {PROGRAM}\033[0m
  1100. - Same for AppImages only, option \"-ia\" or \"install-appimage\":
  1101. ${LightBlue}am -ia --user {PROGRAM}\033[0m
  1102. ${LightBlue}am -ia --user --debug {PROGRAM}
  1103. ${LightBlue}am -ia --user --force-latest {PROGRAM}
  1104. ${LightBlue}am -ia --user --icons {PROGRAM}
  1105. ${LightBlue}am -ia --user --debug --force-latest {PROGRAM}
  1106. ${LightBlue}am -ia --user --debug --force-latest --icons {PROGRAM}\033[0m
  1107. - External AppImages can be installed like this as well, option \"-e\" or \"extra\":
  1108. ${LightBlue}am -e --user user/project {APPNAME}
  1109. ${LightBlue}am -e --user user/project {APPNAME} {KEYWORD}\033[0m
  1110. NOTE, \"AM\" 9 or higher is also able to, update and manage apps locally, by default, and without having to switch to \"AppMan Mode\".
  1111. $DIVIDING_LINE
  1112. SITES:
  1113. https://github.com/ivan-hc/AM
  1114. https://portable-linux-apps.github.io
  1115. \n" | sed 's/^ //g' | fold -sw 77 | sed 's/^/ /g' | less -Ir
  1116. }
  1117. if [ -t 1 ]; then _use_help; else _use_help | sed -e 's/\x1b\[[0-9;]*m//g'; fi
  1118. ;;
  1119. *)
  1120. exec "$AMCLIPATH"
  1121. ;;
  1122. esac
  1123. # vim:tabstop=4:shiftwidth=4:expandtab