database.am 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. #!/bin/sh
  2. ############################################################################################
  3. # THIS MODULE INCLUDES ALL THE ACTIONS INTENDED FOR THE MANAGEMENT OF LISTS AND SINGLE PAGES
  4. ############################################################################################
  5. ################################################################################
  6. # ABOUT
  7. ################################################################################
  8. AMCATALOGUEMARKDOWNS="${AMCATALOGUEMARKDOWNS:-https://portable-linux-apps.github.io/apps}"
  9. _about_description_for_third_party() {
  10. if echo "$appname" | grep -q ".appbundle$"; then
  11. readme_source="$appbundle_readme"
  12. repo_source="$appbundle_repo"
  13. elif echo "$arg" | grep -q ".toolpack$"; then
  14. readme_source="$toolpack_readme"
  15. repo_source="$toolpack_repo"
  16. fi
  17. appname_arg=$(curl -Ls "$readme_source" 2>/dev/null | grep -i "^| $appname " | tr '|' '\n' | cut -c 2- | grep .)
  18. about_description=$(echo "$appname_arg" | awk -F: "NR==$awk_description")
  19. about_site=$(echo "$appname_arg" | awk -F: "NR==$awk_site")
  20. printf "\n%b\n\nSOURCE: %b\n\nDATABASE: %b\n" "$about_description" "$about_site" "$repo_source"
  21. }
  22. _about_description() {
  23. if curl -o /dev/null -sIf "$markdown_url"; then
  24. markdown_page=$(curl -Ls "$markdown_url" | grep -v -- "^#\|^.*| \[Applications\|^.*| \-\|^.*\!\[")
  25. echo "$markdown_page"
  26. elif grep -q "^◆ $package_name " "$AMDATADIR"/"$ARCH"-*; then
  27. appname=$(echo "$package_name" | sed 's/\.toolpack//g')
  28. _about_description_for_third_party
  29. elif grep -q "^◆ $app_name " "$AMDATADIR"/"$ARCH"-*; then
  30. appname=$(echo "$app_name" | sed 's/\.toolpack//g')
  31. _about_description_for_third_party
  32. elif grep -q "^◆ $app_name.nixappimage " "$AMDATADIR"/"$ARCH"-*; then
  33. appname=$(echo "$app_name.nixappimage" | sed 's/\.toolpack//g')
  34. _about_description_for_third_party
  35. elif grep -q "^◆ $app_name.dwfs.appbundle " "$AMDATADIR"/"$ARCH"-*; then
  36. appname=$(echo "$app_name.dwfs.appbundle" | sed 's/\.toolpack//g')
  37. _about_description_for_third_party
  38. else
  39. printf ' "%s" IS NOT A VALID ARGUMENT\n' "$package_name"
  40. fi
  41. }
  42. _about_page() {
  43. package_name=$(echo "$arg" | sed 's/.appbundle.toolpack/.appbundle/g')
  44. app_name=$(echo "$arg" | sed 's/-appimage//g; s/\.toolpack//g; s/\.dwfs.appbundle//g; s/\.appbundle//g; s/\.nixappimage//g')
  45. argpath=$(echo "$ARGPATHS" | grep "/$app_name$")
  46. markdown_url="$AMCATALOGUEMARKDOWNS/${arg}.md"
  47. printf " PACKAGE: %b%b\033[0m\n" "${Green}" "$package_name"
  48. argpath=$(echo "$ARGPATHS" | grep "/$app_name$")
  49. # Determine status of the app
  50. if [ -f "$argpath/remove" ]; then
  51. app_status="installed\033[0m"
  52. if test -f "$argpath"/.am-installer/*; then
  53. scriptname=$(basename -- "$(find "$argpath"/.am-installer/* | head -1)" | sed 's/appbundle.toolpack$/appbundle/g')
  54. if [ "$app_name" != "$scriptname" ]; then
  55. app_status="installed\033[0m, but replaced by ${Green}$scriptname\033[0m\n\n Note, run ${RED}$AMCLI -a $scriptname\033[0m instead"
  56. if test -f "$argpath"/.am-installer/*appbundle* && ! grep -q "◆ $arg : " "$AMDATADIR/$ARCH-apps" && grep -q "◆ $arg.dwfs.appbundle : " "$AMDATADIR"/"$ARCH"-*; then
  57. app_status="installed\033[0m\n\n Real package name: ${Gold}$arg.dwfs.appbundle\033[0m"
  58. elif [ "$package_name" = "$scriptname" ]; then
  59. app_status="installed\033[0m, as ${Green}$app_name\033[0m"
  60. fi
  61. fi
  62. fi
  63. else
  64. app_status="not installed\n"
  65. fi
  66. if echo "$arg" | grep -q -- "-appimage$" && ! test -f "$argpath"/.am-installer/"$arg"; then
  67. app_status="not installed\n"
  68. fi
  69. if ! grep -q "◆ $arg : " "$AMDATADIR/$ARCH-apps" && grep -q "◆ $arg.dwfs.appbundle : " "$AMDATADIR"/"$ARCH"-*; then
  70. if ! echo "$app_status" | grep -q "^installed"; then
  71. app_status="not installed\n\n Real package name: ${Gold}$arg.dwfs.appbundle\033[0m\n"
  72. fi
  73. fi
  74. # Determine if the referenced app is a metapackage or part of it
  75. METAPACKAGES="kdegames kdeutils node platform-tools"
  76. for m in $METAPACKAGES; do
  77. metaargpath=$(echo "$ARGPATHS" | grep "/$m$")
  78. if grep -q "◆ $arg : .*\"$m\"" "$AMDATADIR/$ARCH-apps"; then
  79. if test -f "$metaargpath/remove"; then
  80. app_status="installed\033[0m, as part of ${Green}$m\033[0m\n\n NOTE, run ${RED}$AMCLI -a $m\033[0m instead"
  81. app_name="$m"
  82. fi
  83. elif [ "$arg" = "$m" ] && [ -d "$metaargpath" ]; then
  84. app_status="installed\033[0m" || app_status="not installed\n"
  85. fi
  86. done
  87. # Create the page
  88. if echo "$app_status" | grep -q "^installed"; then
  89. printf " STATUS: %b\n" "${LightBlue}$app_status"
  90. disk_usage=$(du -sm "$argpath" 2>/dev/null | cut -f1)
  91. [ -z "$disk_usage" ] && disk_usage=$(du -sm "$metaargpath" | cut -f1)
  92. app_version=$(grep -w " ◆ $app_name |" "$AMCACHEDIR"/version-args 2>/dev/null | sed 's:.*| ::')
  93. echo ""
  94. echo " Disk usage: $disk_usage MB"
  95. echo " Installed version: $app_version"
  96. else
  97. printf " STATUS: %b" "$app_status"
  98. fi
  99. _about_description | fold -sw 78 | sed 's/^ //g; s/^/ /g' | awk '!NF && !a++; NF {print;a=0}'
  100. }
  101. ################################################################################
  102. # FILES
  103. ################################################################################
  104. _files_header() {
  105. for f in $third_party_lists; do
  106. [ -z "$CHECK_SUPPORTED_THIRD_PARTY_APPS" ] && CHECK_SUPPORTED_THIRD_PARTY_APPS="$(find "$APPSPATH"/* -type f -name "*.$f*" -print -quit 2>/dev/null)"
  107. done
  108. echo ""
  109. _files_number
  110. APPSNUMB="$FILES_NUMBER"
  111. if grep -q 'usr/local/lib' "$APPSPATH"/*/remove 2>/dev/null; then
  112. LIBNUMB=$(grep -l "usr/local/lib" "$APPSPATH"/*/remove | wc -l | sed 's/ //g')
  113. APPSNUMB=$(("$APPSNUMB"-"$LIBNUMB"))
  114. fi
  115. if [ "$APPSNUMB" = 1 ]; then
  116. APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAM"
  117. else
  118. APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAMS"
  119. fi
  120. if grep -q 'usr/local/lib' "$APPSPATH"/*/remove 2>/dev/null; then
  121. LIBNUMB=$(grep -l "usr/local/lib" "$APPSPATH"/*/remove | wc -l | sed 's/ //g')
  122. if [ "$LIBNUMB" = 1 ]; then
  123. LIBSMESSAGE="AND $LIBNUMB LIBRARY"
  124. else
  125. LIBSMESSAGE="AND $LIBNUMB LIBRARIES"
  126. fi
  127. [ "$APPMAN_ON" = 1 ] && echo "- $APPSMESSAGE $LIBSMESSAGE LOCALLY, AS \"APPMAN\"" || echo "- $APPSMESSAGE $LIBSMESSAGE MANAGED BY \"$AMCLIUPPER\""
  128. else
  129. [ "$APPMAN_ON" = 1 ] && echo "- $APPSMESSAGE LOCALLY, AS \"APPMAN\"" || echo "- $APPSMESSAGE MANAGED BY \"$AMCLIUPPER\""
  130. fi
  131. echo ""
  132. if [ -n "$CHECK_SUPPORTED_THIRD_PARTY_APPS" ]; then
  133. files_table_header="- APPNAME | DB | VERSION | TYPE | SIZE "
  134. files_table_header_lines="- ------- | -- | ------- | ---- | ----"
  135. else
  136. files_table_header="- APPNAME | VERSION | TYPE | SIZE "
  137. files_table_header_lines="- ------- | ------- | ---- | ----"
  138. fi
  139. }
  140. # Functions to determine file's type
  141. _files_if_binary() {
  142. if file ./"$arg"/* | grep "static" >/dev/null 2>&1; then
  143. echo " ◆ $arg | static-binary" >> "$AMCACHEDIR"/files-type
  144. else
  145. echo " ◆ $arg | dynamic-binary" >> "$AMCACHEDIR"/files-type
  146. fi
  147. }
  148. _files_if_library() {
  149. echo " ◆ $arg | library" >> "$AMCACHEDIR"/files-type
  150. }
  151. _files_if_script() {
  152. if grep "/ffwa" "${LINK:-$FILE}" >/dev/null 2>&1; then
  153. echo " ◆ $arg | launcher" >> "$AMCACHEDIR"/files-type
  154. elif grep '#!.*bash' "${LINK:-$FILE}" >/dev/null 2>&1; then
  155. echo " ◆ $arg | bash-script" >> "$AMCACHEDIR"/files-type
  156. elif grep '#!.*dash' "${LINK:-$FILE}" >/dev/null 2>&1; then
  157. echo " ◆ $arg | dash-script" >> "$AMCACHEDIR"/files-type
  158. elif grep '__ARCHIVE_MARKER__' "${LINK:-$FILE}" >/dev/null 2>&1; then
  159. echo " ◆ $arg | dwarfs-appbundle" >> "$AMCACHEDIR"/files-type
  160. elif grep '#!.*bin.*sh' "${LINK:-$FILE}" >/dev/null 2>&1; then
  161. echo " ◆ $arg | posix-script" >> "$AMCACHEDIR"/files-type
  162. elif grep '#!.*awk' "${LINK:-$FILE}" >/dev/null 2>&1; then
  163. echo " ◆ $arg | awk-script" >> "$AMCACHEDIR"/files-type
  164. else
  165. echo " ◆ $arg | script" >> "$AMCACHEDIR"/files-type
  166. fi
  167. }
  168. _files_if_appimage() {
  169. if file ./"$arg/$arg" | grep -qi "static"; then
  170. if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
  171. echo " ◆ $arg | appimage🔒" >> "$AMCACHEDIR"/files-type
  172. else
  173. echo " ◆ $arg | appimage" >> "$AMCACHEDIR"/files-type
  174. fi
  175. else
  176. if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
  177. echo " ◆ $arg | appimage🔒*" >> "$AMCACHEDIR"/files-type
  178. else
  179. echo " ◆ $arg | appimage*" >> "$AMCACHEDIR"/files-type
  180. fi
  181. fi
  182. }
  183. _files_type() {
  184. APPVERSION=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/version-args | sed 's:.*| ::')
  185. FILE=$(command -v "$arg" 2>/dev/null)
  186. LINK=$(readlink "$FILE" 2>/dev/null)
  187. if grep -Eaoq -m 1 'github.com/AppImage/AppImageKit/wiki/FUSE' ./"$arg/$arg" 2>/dev/null; then
  188. _files_if_appimage
  189. elif file ./"$arg"/* | grep -E 'LSB|/bin' >/dev/null 2>&1; then
  190. _files_if_binary
  191. elif grep "#!" "${LINK:-$FILE}" >/dev/null 2>&1; then
  192. _files_if_script
  193. elif grep "usr/local/lib" ./"$arg"/remove >/dev/null 2>&1; then
  194. _files_if_library
  195. else
  196. echo " ◆ $arg | other" >> "$AMCACHEDIR"/files-type
  197. fi
  198. }
  199. # Function to determine file's size
  200. _files_sizes() {
  201. if grep -q "usr/local/lib" ./"$arg"/remove; then
  202. LIBNAME=$(<"$APPSPATH"/"$arg"/remove tr ' ' '\n' | grep "usr/local/lib" | head -1 | sed 's/*//g')
  203. SIZE=$(du -sh "$LIBNAME"* | cut -f1 | sort -rh | head -1)
  204. else
  205. SIZE=$(du -sh -- "$arg" | cut -f1 -d" ")
  206. fi
  207. SIZE=$(echo "$SIZE" | sed 's/.$/ &/; s/$/iB/')
  208. echo " ◆ $arg | $SIZE" >> "$AMCACHEDIR"/files-sizes
  209. }
  210. # Function to determine file's database
  211. _files_db() {
  212. if test -f "$APPSPATH"/"$arg"/.am-installer/*.appbundle*; then
  213. DB="appbundlehub"
  214. elif test -f "$APPSPATH"/"$arg"/.am-installer/*toolpack; then
  215. DB="toolpacks"
  216. elif [ "$arg" = am ]; then
  217. DB="am"
  218. elif test -f "$CACHEDIR"/extra/"$arg" || test -f "$APPSPATH"/"$arg"/.am-installer/"$arg" && ! grep -q "^APP=" "$APPSPATH"/"$arg"/.am-installer/"$arg"; then
  219. DB="none"
  220. elif test -f "$AMDATADIR/$ARCH-apps" && ! grep -q "^◆ $arg :" "$AMDATADIR/$ARCH-apps"; then
  221. DB="unknown"
  222. else
  223. DB="am"
  224. fi
  225. echo " ◆ $arg | $DB" >> "$AMCACHEDIR"/files-db
  226. }
  227. # Functions to create file's table
  228. _files_files() {
  229. cd "$APPSPATH" || exit 1
  230. INSTALLED_APPS_BY_SIZE=$(du -sh $INSTALLED_APPS_PATHS 2>/dev/null | sort -rh)
  231. INSTALLED_APPS=$(echo "$INSTALLED_APPS_BY_SIZE" | xargs -n 1 basename 2>/dev/null)
  232. if [ ! -f "$AMCACHEDIR"/version-args ]; then
  233. _check_version
  234. fi
  235. if [ ! -f "$AMCACHEDIR"/files-type ]; then
  236. for arg in $INSTALLED_APPS; do
  237. if [ -f ./"$arg"/remove ]; then
  238. _files_type &
  239. fi
  240. done
  241. wait
  242. fi
  243. rm -f "$AMCACHEDIR"/files-sizes "$AMCACHEDIR"/files-db
  244. for arg in $INSTALLED_APPS; do
  245. if [ -f ./"$arg"/remove ]; then
  246. _files_sizes &
  247. _files_db
  248. fi
  249. done
  250. wait
  251. }
  252. _files() {
  253. _files_files
  254. rm -f "$AMCACHEDIR"/files-args
  255. for arg in $INSTALLED_APPS; do
  256. if test -f ./"$arg"/remove 2>/dev/null; then
  257. APPVERSION=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/version-args | tr ' ' '\n' | tail -1)
  258. [ -f ./"$arg"/AM-LOCK ] && APPVERSION="$APPVERSION🔒"
  259. echo "$APPVERSION" | grep -q "🔒$" && APPLOCKED=1
  260. APPTYPE=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/files-type | tr ' ' '\n' | tail -1)
  261. APPSYZE=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/files-sizes | tr ' ' '\n' | tail -1)
  262. APPDB=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/files-db | tr ' ' '\n' | tail -1)
  263. if [ -n "$CHECK_SUPPORTED_THIRD_PARTY_APPS" ]; then
  264. echo " ◆ $arg | $APPDB | $APPVERSION | $APPTYPE | $APPSYZE" >> "$AMCACHEDIR"/files-args
  265. else
  266. echo " ◆ $arg | $APPVERSION | $APPTYPE | $APPSYZE" >> "$AMCACHEDIR"/files-args
  267. fi
  268. fi
  269. done
  270. }
  271. _files_number() {
  272. _determine_args
  273. INSTALLED_APPS_PATHS=$(echo "$ARGPATHS" | grep "$APPSPATH")
  274. FILES_NUMBER=$(find "$APPSPATH" -type f -name 'remove' 2>/dev/null | uniq | wc -l | sed 's/ //g')
  275. if [ "$FILES_NUMBER" != 0 ]; then
  276. FILES_NUMBER=$(echo "$INSTALLED_APPS_PATHS" | uniq | wc -l | sed 's/ //g')
  277. fi
  278. }
  279. _files_appimage_type_notes() {
  280. if grep -qe "appimage\*\|appimage🔒\*" "$AMCACHEDIR"/files*; then
  281. printf " *has dependency to EOL libfuse2 library, please ask the creator to update\n"
  282. printf "\n"
  283. fi
  284. }
  285. _files_total_size() {
  286. printf "\n"
  287. if command -v aisap >/dev/null 2>&1 && grep -qe "appimage🔒" "$AMCACHEDIR"/files* && [ -n "$APPLOCKED" ]; then
  288. printf '%s\n\n' " AppImages with 🔒 are sandboxed with aisap, versions with 🔒 are locked"
  289. elif command -v aisap >/dev/null 2>&1 && grep -qe "appimage🔒" "$AMCACHEDIR"/files*; then
  290. printf '%s\n\n' " AppImages with 🔒 are sandboxed with aisap"
  291. elif [ -n "$APPLOCKED" ]; then
  292. printf '%s\n\n' " Versions with 🔒 are locked"
  293. fi
  294. APPLOCKED=""
  295. INSTALLED_APPS_PLAN=$(echo "$INSTALLED_APPS_PATHS" | tr '\n' ' ')
  296. TOTAL_SIZE=$(du -shc $INSTALLED_APPS_PLAN | awk 'END {print $1"iB"}' | sed 's/...$/ &/')
  297. echo " TOTAL SIZE: $TOTAL_SIZE of disk space in use"
  298. printf "\n"
  299. }
  300. _files_sort_by_name() {
  301. _files_header
  302. rm -f "$AMCACHEDIR"/files-args-byname
  303. _files
  304. echo "$files_table_header" > "$AMCACHEDIR"/files-args-byname
  305. echo "$files_table_header_lines" >> "$AMCACHEDIR"/files-args-byname
  306. sort "$AMCACHEDIR"/files-args 2>/dev/null >> "$AMCACHEDIR"/files-args-byname
  307. column -t "$AMCACHEDIR"/files-args-byname
  308. _files_total_size
  309. _files_appimage_type_notes
  310. }
  311. _files_sort_by_size() {
  312. _files_header
  313. rm -f "$AMCACHEDIR"/files-args-bysize
  314. _files
  315. echo "$files_table_header" > "$AMCACHEDIR"/files-args-bysize
  316. echo "$files_table_header_lines" >> "$AMCACHEDIR"/files-args-bysize
  317. cat "$AMCACHEDIR"/files-args >> "$AMCACHEDIR"/files-args-bysize 2>/dev/null
  318. column -t "$AMCACHEDIR"/files-args-bysize
  319. _files_total_size
  320. _files_appimage_type_notes
  321. }
  322. _files_appman_mode_view() {
  323. [ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && APPMAN_ON=1 && _files_number && [ "$FILES_NUMBER" != 0 ] && echo "$DIVIDING_LINE"
  324. }
  325. _files_launcher_message() {
  326. echo "- APPIMAGE | PATH | SIZE "
  327. echo "- -------- | ---- | ----"
  328. for var in "$DATADIR"/applications/AppImages/*.desktop; do
  329. appimage_full_path=$(grep "^Exec=" 0<"$var" 2>/dev/null | head -1 | cut -c 6- | sed 's/"//g; s/\s.*$//')
  330. appimagename=$(basename -- "$appimage_full_path")
  331. appimage_path=$(echo "$appimage_full_path" | sed -E 's|/[^/]+$|/|; s/\/*$//g')
  332. appimage_size="$(du -sh -- "$appimage_full_path" 2>/dev/null | cut -f1 -d" ")"
  333. appimage_size_unit=$(echo "$appimage_size" | sed 's/.$/ &/; s/$/iB/')
  334. [ ! -d "$appimage_path" ] && appimage_path="$appimage_path*" && appimage_size_unit="unknown"
  335. echo "◆ $appimagename | $appimage_path | $appimage_size_unit"
  336. #echo "◆ File: $appimagename"
  337. #echo " Path: $appimage_path"
  338. #echo " Size: $appimage_size_unit"
  339. #echo ""
  340. done
  341. }
  342. _files_missing_launcher_message() {
  343. MISSING_LAUNCHERS_MSG=" No launcher found, use option \"${Green}--launcher\033[0m\" to create them."
  344. printf "%b\n%b\n%b\n" "$DIVIDING_LINE" "$MISSING_LAUNCHERS_MSG" "$DIVIDING_LINE"
  345. }
  346. _files_launcher() {
  347. _clean_launchers 2>/dev/null 1>/dev/null
  348. MISSING_LAUNCHERS_MSG="No launcher found, use option \"--launcher\" to create them."
  349. [ ! -d "$DATADIR"/applications/AppImages ] && _files_missing_launcher_message && exit 0
  350. [ -d "$DATADIR"/applications/AppImages ] && [ -z "$( ls -A "$DATADIR"/applications/AppImages )" ] && _files_missing_launcher_message && exit 0
  351. FILES_LAUNCHERS_NUMBER=$(find "$DATADIR"/applications/AppImages -type f -name '*.desktop' 2>/dev/null | uniq | wc -l | sed 's/ //g')
  352. [ "$FILES_LAUNCHERS_NUMBER" = 1 ] && APPIMAGES="APPIMAGE" || APPIMAGES="APPIMAGES"
  353. LAUNCHERS_MESSAGE="- YOU HAVE INTEGRATED $FILES_LAUNCHERS_NUMBER $APPIMAGES USING THE \"--launcher\" OPTION"
  354. printf "\n%b\n\n" "$LAUNCHERS_MESSAGE"
  355. #_files_launcher_message | _fit
  356. _files_launcher_message | column -t
  357. _files_launcher_message | grep -q " unknown$" && printf "\n *this path is listed but not mounted, file size unknown\n\n" || printf "\n"
  358. }
  359. ################################################################################
  360. # LIST/QUERY
  361. ################################################################################
  362. # Functions to beautify lists
  363. _pretty_list() {
  364. # Remove references to URLs, "-a" elements in "-l" and "-q"
  365. sed -E 's#(http|https|ftp)://[^ ]*##g; s#(SITE|SOURCE):##g; s/^/\n/g' | fold -sw 78 | sed 's/^/ /g; s/ ◆ /◆ /g; s/ :/ :/g' | _colors
  366. }
  367. _pretty_list_compat() {
  368. # Remove references to URLs, "-a" elements in "-l" and "-q"
  369. sed -E 's#(http|https|ftp)://[^ ]*##g; s#(SITE|SOURCE):##g' | fold -sw 78 | sed 's/^/ /g; s/ ◆ /◆ /g; s/ :/ :/g' | _colors
  370. }
  371. _colors() {
  372. awk '/^ ◆ /{
  373. printf "%s \033[32m%s\033[0m", $1, $2
  374. {$1 = ""; $2 = ""; print $0;}
  375. }1' | grep -v "^ :" | sed 's/^ / /g; s/ : / : /g'
  376. }
  377. #functions to create and handle lists
  378. _list() {
  379. # Check if github.com is online, if not, the function will read the offline list
  380. wget -q --tries=10 --timeout=20 --spider https://github.com && _completion_lists
  381. # Check the number of installed apps and libraries
  382. LIBNUMBER=$(grep -q "usr/local/lib" "$APPSPATH"/*/remove | wc -l | sed 's/ //g')
  383. ITEMSNUMBER=$(find "$APPSPATH" -name 'remove' -print 2>/dev/null | sort -u | wc -l | sed 's/ //g')
  384. _detect_appman_apps
  385. if [ "$AMCLI" = am ]; then
  386. if [ -d "$APPMAN_APPSPATH" ]; then
  387. APPMAN_ITEMS=$(find "$APPMAN_APPSPATH" -name 'remove' -print 2>/dev/null | sort -u | wc -l | sed 's/ //g')
  388. ITEMSNUMBER=$(("$ITEMSNUMBER"+"$APPMAN_ITEMS"))
  389. fi
  390. if test -f /opt/am/remove; then
  391. ITEMSNUMBER=$(("$ITEMSNUMBER"-1))
  392. fi
  393. fi
  394. APPSNUMBER=$(("$ITEMSNUMBER" - "$LIBNUMBER"))
  395. # Determine the number of available apps from the list
  396. AVAILABLE_APPS_NUMBER=$(grep -v "ffwa-\|\"kdegames\"\|\"kdeutils\"\|\"node\"\|\"platform-tools\"" "$AMDATADIR/$ARCH-apps" | grep -e "^◆.*$" -c)
  397. if ! test -f "$AMDATADIR/$ARCH-appimages"; then
  398. _online_check
  399. _sync_appimages_list
  400. AVAILABLE_APPIMAGES_NUMBER=$(grep -e "^◆.*$" -c "$AMDATADIR/$ARCH-appimages")
  401. else
  402. AVAILABLE_APPIMAGES_NUMBER=$(grep -e "^◆.*$" -c "$AMDATADIR/$ARCH-appimages")
  403. fi
  404. # Determine the number of third-party apps
  405. for t in $third_party_lists; do
  406. [ ! -f "$AMDATADIR/$ARCH-$t" ] && _online_check && _sync_third_party_lists && _completion_lists
  407. done
  408. tp_lists=$(find "$AMDATADIR" -type f -name "$ARCH-*" | grep -v "appimages\|apps")
  409. for t in $tp_lists; do
  410. [ -z "$TP_LISTS" ] && TP_LISTS=$(sort -u "$t") || TP_LISTS="$TP_LISTS\n$(sort -u "$t")"
  411. done
  412. [ -n "$third_party_lists" ] && AVAILABLE_THIRD_PARTY_NUMBER=$(printf "%b" "$TP_LISTS" | sort -u | grep -e "^◆.*$" -c)
  413. [ -n "$AVAILABLE_THIRD_PARTY_NUMBER" ] && AVAILABLE_TOTAL_APPS_NUMBER=$(("$AVAILABLE_APPS_NUMBER" + "$AVAILABLE_THIRD_PARTY_NUMBER"))
  414. # Generate a list of the installed apps with version
  415. [ ! -f "$AMCACHEDIR"/version-args ] && _check_version
  416. if [ -n "$APPMAN_ITEMS" ]; then
  417. _check_version
  418. else
  419. grep " ◆ $ARGS " "$AMCACHEDIR"/version-args > "$AMCACHEDIR"/version-args-real
  420. sort "$AMCACHEDIR"/version-args-real > "$AMCACHEDIR"/version-args
  421. fi
  422. INSTALLED=$(sort "$AMCACHEDIR"/version-args 2>/dev/null | sed 's/ | / /g' | grep -v "◆ am " | uniq)
  423. MESSAGE2="$INSTALLED"
  424. # Check if among the installed apps are available libraries
  425. if [ "$LIBNUMBER" != 0 ]; then
  426. # Check if the installed libraries are more than one
  427. if [ "$LIBNUMBER" = 1 ]; then
  428. MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER PROGRAMS OUT OF $AVAILABLE_TOTAL_APPS_NUMBER AVAILABLE, AND $LIBNUMBER LIBRARY"
  429. else
  430. MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER PROGRAMS OUT OF $AVAILABLE_TOTAL_APPS_NUMBER AVAILABLE, AND $LIBNUMBER LIBRARIES"
  431. fi
  432. else
  433. MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER PROGRAMS OUT OF $AVAILABLE_TOTAL_APPS_NUMBER AVAILABLE"
  434. fi
  435. }
  436. _list_msg() {
  437. printf "\n%b\n\n%b\n\n" "$MESSAGE" "$MESSAGE2"
  438. SUBJECT_UPPER=$(echo "$SUBJECT" | tr '[:lower:]' '[:upper:]')
  439. [ "$2" != --all ] && printf " To list all installable programs, use the \"%b--all\033[0m\" flag\n\n" "${LightBlue}"
  440. printf " LIST OF THE %b" "$SUBJECT_UPPER:"
  441. printf "\n%b\n" "$LIST"
  442. }
  443. # Functions used in "query"
  444. _query_lists() {
  445. printf "\n Search results for \"%s\":\n\n" "$@" | tr '[:lower:]' '[:upper:]'
  446. }
  447. _find_no_order() {
  448. grep -i "$1" | grep -i "$2" | grep -i "$3" | grep -i "$4" | grep -i "$5" \
  449. | grep -i "$6" | grep -i "$7" | grep -i "$8" | grep -i "$9" | grep -i "${10}" \
  450. | grep -i "${11}" | grep -i "${12}" | grep -i "${13}" | grep -i "${14}" | grep -i "${15}" \
  451. | grep -i "${16}" | grep -i "${17}" | grep -i "${18}" | grep -i "${19}" | grep -i "${20}"
  452. }
  453. ################################################################################
  454. # USAGE
  455. ################################################################################
  456. case "$1" in
  457. '-a'|'about')
  458. [ -z "$2" ] && echo " USAGE: $AMCLI $1 [ARGUMENT]" && exit 1
  459. _online_check
  460. _sync_third_party_lists
  461. _determine_args
  462. echo "$DIVIDING_LINE"
  463. rm -f "$AMCACHEDIR"/version-args && _check_version
  464. entries="$(echo "$@" | cut -f2- -d ' ')" # Removes first argument
  465. for arg in $entries; do
  466. tparg=$(echo "$arg" | sed 's/\.toolpack//g; s/\.appimage//g; s/\.dwfs.appbundle$//g; s/\.appbundle$//g; s/\.nixappimage//g')
  467. if ! grep -q "^◆ $arg \|^◆ $arg.toolpack \|^◆ $arg.dwfs.appbundle \|^◆ $tparg " "$AMDATADIR"/"$ARCH"-*; then
  468. printf " ERROR: %b not found\033[0m\n" "${RED}\"$arg\""
  469. else
  470. _about_page "$arg"
  471. fi
  472. echo "$DIVIDING_LINE"
  473. done
  474. ;;
  475. '-f'|'files'|'-fi')
  476. if [ "$2" = "--less" ]; then
  477. _files_number && echo "$FILES_NUMBER"
  478. [ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && echo "$DIVIDING_LINE" && _files_number && echo "$FILES_NUMBER"
  479. elif [ "$2" = "--byname" ]; then
  480. _files_sort_by_name
  481. _files_appman_mode_view && _files_sort_by_name
  482. [ "$1" != "-fi" ] && [ -d "$DATADIR"/applications/AppImages ] && echo "$DIVIDING_LINE" && _files_launcher
  483. _betatester_message_on
  484. else
  485. _files_sort_by_size
  486. _files_appman_mode_view && _files_sort_by_size
  487. [ "$1" != "-fi" ] && [ -d "$DATADIR"/applications/AppImages ] && echo "$DIVIDING_LINE" && _files_launcher
  488. _betatester_message_on
  489. fi
  490. ;;
  491. '-l'|'list')
  492. if [ "$2" = --all ]; then
  493. wget -q --tries=10 --timeout=20 --spider https://github.com && _sync_databases 1>/dev/null
  494. _list
  495. SUBJECT="$AVAILABLE_TOTAL_APPS_NUMBER programs available in all supported databases"
  496. LIST=$(sort -u "$AMDATADIR"/"$ARCH"-* | grep "^◆ .*$" 2>/dev/null | _pretty_list)
  497. _list_msg "$@" | less -Ir
  498. printf "%b\n %b\n%b\n" "$DIVIDING_LINE" "$SUBJECT" "$DIVIDING_LINE"
  499. elif [ "$2" = --appimages ]; then
  500. wget -q --tries=10 --timeout=20 --spider https://github.com && _sync_appimages_list
  501. _list
  502. SUBJECT="$AVAILABLE_APPIMAGES_NUMBER AppImages available in the \"AM\" database"
  503. LIST=$(sort -u "$AMDATADIR"/"$ARCH"-appimages | grep "^◆ .*$" 2>/dev/null | _pretty_list)
  504. _list_msg "$@" | less -Ir
  505. printf "%b\n %b\n%b\n" "$DIVIDING_LINE" "$SUBJECT" "$DIVIDING_LINE"
  506. elif [ "$2" = --toolpack ]; then
  507. wget -q --tries=10 --timeout=20 --spider https://github.com && _sync_third_party_lists 2>/dev/null
  508. _list
  509. SUBJECT="$AVAILABLE_THIRD_PARTY_NUMBER programs available on third-party databases"
  510. tp_lists=$(find "$AMDATADIR" -type f -name "$ARCH-*" | grep -v "appimages\|apps")
  511. for t in $tp_lists; do
  512. [ -z "$TP_LISTS" ] && TP_LISTS=$(sort -u "$t") || TP_LISTS="$TP_LISTS\n$(sort -u "$t")"
  513. done
  514. LIST=$(printf "%b" "$TP_LISTS" | sort -u | grep "^◆ .*$" 2>/dev/null | _pretty_list)
  515. _list_msg "$@" | less -Ir
  516. printf "%b\n %b\n%b\n" "$DIVIDING_LINE" "$SUBJECT" "$DIVIDING_LINE"
  517. else
  518. _list
  519. SUBJECT="$AVAILABLE_APPS_NUMBER programs available in the \"AM\" database"
  520. LIST=$(sort -u "$AMDATADIR"/"$ARCH"-apps | grep "^◆ .*$" 2>/dev/null | _pretty_list)
  521. _list_msg "$@" | less -Ir
  522. printf "%b\n %b\n%b\n" "$DIVIDING_LINE" "$SUBJECT" "$DIVIDING_LINE"
  523. fi
  524. ;;
  525. '-q'|'query')
  526. if [ -z "$2" ] || echo "$2" | grep -q -- "^--" && [ -z "$3" ]; then
  527. echo ""
  528. echo " USAGE: $AMCLI $1 [ARGUMENT]"
  529. echo " $AMCLI $1 --all [ARGUMENT]"
  530. echo " $AMCLI $1 --appimages [ARGUMENT]"
  531. echo " $AMCLI $1 --pkg [ARGUMENT]"
  532. [ -n "$toolpack_readme" ] && echo " $AMCLI $1 --toolpack [ARGUMENT]"
  533. echo ""; exit 1
  534. fi
  535. wget -q --tries=10 --timeout=20 --spider https://github.com && _completion_lists
  536. shift
  537. if [ "$1" = --pkg ]; then
  538. shift
  539. regex="$(echo "$@" | tr -s ' ' '|')"
  540. printf "\n Search results for packages: %s\n\n" "$regex" | tr '[:lower:]' '[:upper:]'
  541. sort -u "$AMDATADIR"/"$ARCH"-* 2>/dev/null | grep "^◆ .*$" 2>/dev/null \
  542. | grep -iE "$regex" --color=always | _pretty_list_compat
  543. elif [ "$1" = --all ]; then
  544. shift
  545. wget -q --tries=10 --timeout=20 --spider https://github.com && _sync_databases 1>/dev/null
  546. _query_lists "$*"
  547. sort -u "$AMDATADIR"/"$ARCH"-* 2>/dev/null | grep "^◆ .*$" 2>/dev/null | _find_no_order "$@" | _pretty_list_compat
  548. elif [ "$1" = --appimages ]; then
  549. shift
  550. wget -q --tries=10 --timeout=20 --spider https://github.com && _sync_appimages_list
  551. _query_lists "$*"
  552. sort -u "$AMDATADIR/$ARCH-appimages" 2>/dev/null | grep "^◆ .*$" 2>/dev/null | _find_no_order "$@" | _pretty_list_compat
  553. elif [ "$1" = --toolpack ]; then
  554. shift
  555. wget -q --tries=10 --timeout=20 --spider https://github.com && _sync_third_party_lists 2>/dev/null
  556. _query_lists "$*"
  557. sort -u "$AMDATADIR/$ARCH-toolpack" 2>/dev/null | grep "^◆ .*$" 2>/dev/null | _find_no_order "$@" | _pretty_list_compat
  558. else
  559. _query_lists "$*"
  560. LISTS=$(find "$AMDATADIR" -type f -name "$ARCH-app*" 2>/dev/null | grep -v appimage | xargs)
  561. sort -u $LISTS 2>/dev/null | grep "^◆ .*$" 2>/dev/null | _find_no_order "$@" | _pretty_list_compat
  562. fi
  563. printf '\n'
  564. ;;
  565. esac