APP-MANAGER 48 KB

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