database.am 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. _about_show_markdown() {
  8. if curl -o /dev/null -sIf "$markdown_url"; then
  9. markdown_page=$(curl -Ls "$markdown_url" \
  10. | sed '1,${ /^\s*#/ d; /^\s*!/ d; /\[Applications]/d; /\ --- /d; /\ | - | /d; /\!\[/d; }' \
  11. | sed '$!N;s/^\s*\n\s*$//;P;D')
  12. echo "$markdown_page" | fold -sw 72 | sed 's/^ //g; s/^/ /g'
  13. else
  14. printf ' "%s" IS NOT A VALID ARGUMENT\n' "$package_name"
  15. fi
  16. }
  17. _about_page() {
  18. _determine_args
  19. package_name="${Green}$1\033[0m"
  20. markdown_url="$AMCATALOGUEMARKDOWNS/${1}.md"
  21. printf " PACKAGE: %b\n" "$package_name"
  22. app_name=$(echo "$1" | sed 's/-appimage//g; s/debian-testing-//g')
  23. argpath=$(echo "$ARGPATHS" | grep "/$app_name$")
  24. if [ -f "$argpath/remove" ]; then
  25. app_status="installed\033[0m"
  26. if test -f "$argpath/.am-installer/$app_name"; then
  27. app_status="installed\033[0m"
  28. elif [ "$app_name" != "$1" ]; then
  29. if test -f "$argpath/.am-installer/$1"; then
  30. app_status="installed\033[0m, as ${Green}$app_name\033[0m"
  31. fi
  32. elif test -d "$argpath/.am-installer"; then
  33. scriptname=$(basename -- "$(find "$argpath"/.am-installer/* | head -1)")
  34. if [ "$scriptname" != "$1" ]; then
  35. app_status="installed\033[0m, but replaced by ${Green}$scriptname\033[0m\n\n NOTE: run ${RED}$AMCLI -a $scriptname\033[0m instead"
  36. fi
  37. fi
  38. elif [ -f "$argpath-appimage/remove" ]; then
  39. app_status="installed\033[0m"
  40. app_name="$app_name-appimage"
  41. else
  42. app_status="not installed"
  43. fi
  44. METAPACKAGES="kdegames kdeutils node platform-tools"
  45. for m in $METAPACKAGES; do
  46. metaargpath=$(echo "$ARGPATHS" | grep "/$m$")
  47. if grep -q "◆ $1 : .*\"$m\"" "$AMDATADIR/$ARCH-apps"; then
  48. if test -f "$metaargpath/remove"; then
  49. app_status="installed\033[0m, as part of ${Green}$m\033[0m\n\n NOTE: run ${RED}$AMCLI -a $m\033[0m instead"
  50. app_name="$m"
  51. fi
  52. elif [ "$1" = "$m" ]; then
  53. app_status="installed\033[0m"
  54. fi
  55. done
  56. if echo "$app_status" | grep -q "^installed"; then
  57. printf " STATUS: %b\n" "${LightBlue}$app_status"
  58. disk_usage=$(du -sm "$argpath" | cut -f1)
  59. [ -z "$disk_usage" ] && disk_usage=$(du -sm "$metaargpath" | cut -f1)
  60. app_version=$(grep -w " ◆ $app_name |" "$AMCACHEDIR"/version-args 2>/dev/null | sed 's:.*| ::')
  61. echo ""
  62. echo " Disk usage: $disk_usage MB"
  63. echo " Installed version: $app_version"
  64. else
  65. echo " STATUS: $app_status"
  66. fi
  67. _about_show_markdown
  68. }
  69. #--------------------------------------------------------------------------
  70. # FILES
  71. _files_header() {
  72. echo ""
  73. APPSNUMB=$(find "$APPSPATH" -type f -name 'remove' -exec grep -L 'usr/local/lib' {} \; 2>/dev/null | sed -r 's|/[^/]+$||' | sort | uniq | wc -l)
  74. if [ "$APPSNUMB" = 1 ]; then
  75. APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAM"
  76. else
  77. APPSMESSAGE="YOU HAVE INSTALLED $APPSNUMB PROGRAMS"
  78. fi
  79. if grep -q 'usr/local/lib' "$APPSPATH"/*/remove 2> /dev/null; then
  80. LIBNUMB=$(grep -l "usr/local/lib" "$APPSPATH"/*/remove | wc -l)
  81. if [ "$LIBNUMB" = 1 ]; then
  82. LIBSMESSAGE="AND $LIBNUMB LIBRARY"
  83. else
  84. LIBSMESSAGE="AND $LIBNUMB LIBRARIES"
  85. fi
  86. [ "$APPMAN_ON" = 1 ] && echo "- $APPSMESSAGE $LIBSMESSAGE LOCALLY, AS \"APPMAN\"" || echo "- $APPSMESSAGE $LIBSMESSAGE MANAGED BY \"$AMCLIUPPER\""
  87. else
  88. [ "$APPMAN_ON" = 1 ] && echo "- $APPSMESSAGE LOCALLY, AS \"APPMAN\"" || echo "- $APPSMESSAGE MANAGED BY \"$AMCLIUPPER\""
  89. fi
  90. echo ""
  91. }
  92. _files_sizes() {
  93. if grep -q "usr/local/lib" ./"$arg"/remove; then
  94. LIBNAME=$(<"$APPSPATH"/"$arg"/remove tr ' ' '\n' | grep "usr/local/lib" | head -1 | sed 's/*//g')
  95. SIZE=$(du -sh "$LIBNAME"* | cut -f1 | sort -rh | head -1)
  96. else
  97. SIZE=$(du -sh -- "$arg" | cut -f1 -d" ")
  98. fi
  99. SIZE=$(echo "$SIZE" | sed 's/.$/ &/; s/$/iB/')
  100. echo " ◆ $arg | $SIZE" >> "$AMCACHEDIR"/files-sizes
  101. }
  102. _files_if_binary() {
  103. if file ./"$arg"/* | grep "static" >/dev/null 2>&1; then
  104. echo " ◆ $arg | static-binary" >> "$AMCACHEDIR"/files-type
  105. else
  106. echo " ◆ $arg | dynamic-binary" >> "$AMCACHEDIR"/files-type
  107. fi
  108. }
  109. _files_if_library() {
  110. echo " ◆ $arg | library" >> "$AMCACHEDIR"/files-type
  111. }
  112. _files_if_script() {
  113. if grep "/ffwa" "${LINK:-$FILE}" >/dev/null 2>&1; then
  114. echo " ◆ $arg | launcher" >> "$AMCACHEDIR"/files-type
  115. elif grep '#!.*bash' "${LINK:-$FILE}" >/dev/null 2>&1; then
  116. echo " ◆ $arg | bash-script" >> "$AMCACHEDIR"/files-type
  117. elif grep '#!.*dash' "${LINK:-$FILE}" >/dev/null 2>&1; then
  118. echo " ◆ $arg | dash-script" >> "$AMCACHEDIR"/files-type
  119. elif grep '#!.*bin.*sh' "${LINK:-$FILE}" >/dev/null 2>&1; then
  120. echo " ◆ $arg | posix-script" >> "$AMCACHEDIR"/files-type
  121. elif grep '#!.*awk' "${LINK:-$FILE}" >/dev/null 2>&1; then
  122. echo " ◆ $arg | awk-script" >> "$AMCACHEDIR"/files-type
  123. else
  124. echo " ◆ $arg | script" >> "$AMCACHEDIR"/files-type
  125. fi
  126. }
  127. _files_if_appimage() {
  128. if ! echo "$string" | grep 'AppImages require FUSE to run' >/dev/null 2>&1; then
  129. if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
  130. echo " ◆ $arg | appimage🔒" >> "$AMCACHEDIR"/files-type
  131. else
  132. echo " ◆ $arg | appimage" >> "$AMCACHEDIR"/files-type
  133. fi
  134. else
  135. if grep "SANDBOXDIR" "$FILE" >/dev/null 2>&1; then
  136. echo " ◆ $arg | appimage🔒*" >> "$AMCACHEDIR"/files-type
  137. else
  138. echo " ◆ $arg | appimage*" >> "$AMCACHEDIR"/files-type
  139. fi
  140. fi
  141. }
  142. _files_type() {
  143. APPVERSION=$(<"$AMCACHEDIR"/version-args grep -w " ◆ $arg |" | sed 's:.*| ::')
  144. string=$(strings -d ./"$arg/$arg" 2>/dev/null)
  145. FILE=$(command -v "$arg" 2>/dev/null)
  146. LINK=$(readlink "$FILE" 2>/dev/null)
  147. if echo "$string" | grep -- '--appimage-extract option' >/dev/null 2>&1; then
  148. _files_if_appimage
  149. elif file ./"$arg"/* | grep -E 'LSB|/bin' >/dev/null 2>&1; then
  150. _files_if_binary
  151. elif grep "#!" "${LINK:-$FILE}" >/dev/null 2>&1; then
  152. _files_if_script
  153. elif grep "usr/local/lib" ./"$arg"/remove >/dev/null 2>&1; then
  154. _files_if_library
  155. else
  156. echo " ◆ $arg | other" >> "$AMCACHEDIR"/files-type
  157. fi
  158. }
  159. _files_files() {
  160. cd "$APPSPATH" || exit 1
  161. INSTALLED_APPS=$(find . -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * 2> /dev/null | sort -rh | sed 's@.* @@')
  162. if ! test -f "$AMCACHEDIR"/version-args; then
  163. _check_version
  164. fi
  165. if ! test -f "$AMCACHEDIR"/files-type; then
  166. for arg in $INSTALLED_APPS; do
  167. if test -f ./"$arg"/remove 2>/dev/null; then
  168. _files_type
  169. fi
  170. done
  171. fi
  172. rm -f "$AMCACHEDIR"/files-sizes
  173. for arg in $INSTALLED_APPS; do
  174. if test -f ./"$arg"/remove 2>/dev/null; then
  175. _files_sizes
  176. fi
  177. done
  178. }
  179. _files() {
  180. _files_files
  181. rm -f "$AMCACHEDIR"/files-args
  182. INSTALLED_APPS=$(find . -name 'remove' -printf "%h\n" 2>/dev/null | du -sh -- * 2>/dev/null | sort -rh | sed 's@.* @@')
  183. for arg in $INSTALLED_APPS; do
  184. if test -f ./"$arg"/remove 2>/dev/null; then
  185. APPVERSION=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/version-args | tr ' ' '\n' | tail -1)
  186. APPTYPE=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/files-type | tr ' ' '\n' | tail -1)
  187. APPSYZE=$(grep -w " ◆ $arg |" "$AMCACHEDIR"/files-sizes | tr ' ' '\n' | tail -1)
  188. echo " ◆ $arg | $APPVERSION | $APPTYPE | $APPSYZE" >> "$AMCACHEDIR"/files-args
  189. fi
  190. done
  191. }
  192. _files_number() {
  193. FILES_NUMBER=$(find "$APPSPATH" -type f -name 'remove' 2>/dev/null | uniq | wc -l)
  194. }
  195. _files_appimage_type_notes() {
  196. if grep -qe "appimage\*\|appimage🔒\*" "$AMCACHEDIR"/files*; then
  197. printf " *has dependency to EOL libfuse2 library, please ask the creator to update\n"
  198. printf "\n"
  199. fi
  200. }
  201. _files_total_size() {
  202. printf "\n"
  203. command -v aisap >/dev/null 2>&1 && printf '%s\n\n' " AppImages with 🔒 are sandboxed with aisap"
  204. echo " TOTAL SIZE: $(find . -type f -name 'remove' -printf "%h\n" | du -ch -- */ 2>/dev/null | awk 'END {print $1"iB"}' | sed 's/...$/ &/') of disk space in use"
  205. printf "\n"
  206. }
  207. _files_sort_by_name() {
  208. _files_header
  209. rm -f "$AMCACHEDIR"/files-args-byname
  210. _files
  211. echo "- APPNAME | VERSION | TYPE | SIZE " > "$AMCACHEDIR"/files-args-byname
  212. echo "- ------- | ------- | ---- | ----" >> "$AMCACHEDIR"/files-args-byname
  213. sort "$AMCACHEDIR"/files-args 2>/dev/null >> "$AMCACHEDIR"/files-args-byname
  214. column -t "$AMCACHEDIR"/files-args-byname
  215. _files_total_size
  216. _files_appimage_type_notes
  217. }
  218. _files_sort_by_size() {
  219. _files_header
  220. rm -f "$AMCACHEDIR"/files-args-bysize
  221. _files
  222. echo "- APPNAME | VERSION | TYPE | SIZE " > "$AMCACHEDIR"/files-args-bysize
  223. echo "- ------- | ------- | ---- | ----" >> "$AMCACHEDIR"/files-args-bysize
  224. cat "$AMCACHEDIR"/files-args >> "$AMCACHEDIR"/files-args-bysize 2>/dev/null
  225. column -t "$AMCACHEDIR"/files-args-bysize
  226. _files_total_size
  227. _files_appimage_type_notes
  228. }
  229. #--------------------------------------------------------------------------
  230. # LIST/QUERY
  231. _pretty_list() {
  232. # Remove references to URLs, "-a" elements in "-l" and "-q"
  233. sed -E 's#(http|https|ftp)://[^ ]*##g; s#(SITE|SOURCE):##g; s/^/\n/g' | _colors | fold -sw 75 | sed 's/^/ /g; s/ ◆ /◆ /g; s/ :/ :/g'
  234. }
  235. _pretty_list_compat() {
  236. # Remove references to URLs, "-a" elements in "-l" and "-q"
  237. sed -E 's#(http|https|ftp)://[^ ]*##g; s#(SITE|SOURCE):##g' | _colors | fold -sw 75 | sed 's/^/ /g; s/ ◆ /◆ /g; s/ :/ :/g'
  238. }
  239. _colors() {
  240. awk '{
  241. printf "%s \033[32m%s\033[0m", $1, $2
  242. {$1 = ""; $2 = ""; print $0;}
  243. }'
  244. }
  245. _list() {
  246. # Check if github.com is online, if not, the function will read the offline list
  247. wget -q --tries=10 --timeout=20 --spider https://github.com && _completion_lists
  248. # Check the number of installed apps and libraries
  249. LIBNUMBER=$(grep "usr/local/lib" "$APPSPATH"/*/remove 2> /dev/null | wc -l)
  250. ITEMSNUMBER=$(find "$APPSPATH" -name 'remove' -printf "%h\n" 2>/dev/null | sort -u | wc -l)
  251. _detect_appman_apps
  252. if [ "$AMCLI" = am ]; then
  253. if [ -d "$APPMAN_APPSPATH" ]; then
  254. APPMAN_ITEMS=$(find "$APPMAN_APPSPATH" -name 'remove' -printf "%h\n" 2>/dev/null | sort -u | wc -l)
  255. ITEMSNUMBER=$(("$ITEMSNUMBER"+"$APPMAN_ITEMS"))
  256. fi
  257. if test -f /opt/am/remove; then
  258. ITEMSNUMBER=$(("$ITEMSNUMBER"-1))
  259. fi
  260. fi
  261. APPSNUMBER=$(("$ITEMSNUMBER" - "$LIBNUMBER"))
  262. # Determine the number of available apps from the list
  263. AVAILABLE_APPS_NUMBER=$(grep -v "ffwa-\|\"kdegames\"\|\"kdeutils\"\|\"node\"\|\"platform-tools\"" "$AMDATADIR/$ARCH-apps" | grep -e "$" -c)
  264. # Generate a list of the installed apps with version
  265. [ ! -f "$AMCACHEDIR"/version-args ] && _check_version
  266. if [ -n "$APPMAN_ITEMS" ]; then
  267. _check_version
  268. else
  269. grep " ◆ $ARGS " "$AMCACHEDIR"/version-args > "$AMCACHEDIR"/version-args-real
  270. sort "$AMCACHEDIR"/version-args-real > "$AMCACHEDIR"/version-args
  271. fi
  272. INSTALLED=$(sort "$AMCACHEDIR"/version-args 2>/dev/null | sed 's/ | / /g' | grep -v "◆ am " | uniq)
  273. MESSAGE2="$INSTALLED"
  274. # Check if among the installed apps are available libraries
  275. if [ "$LIBNUMBER" != 0 ]; then
  276. # Check if the installed libraries are more than one
  277. if [ "$LIBNUMBER" = 1 ]; then
  278. MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER APPLICATIONS OUT OF $AVAILABLE_APPS_NUMBER AVAILABLE, AND $LIBNUMBER LIBRARY"
  279. else
  280. MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER APPLICATIONS OUT OF $AVAILABLE_APPS_NUMBER AVAILABLE, AND $LIBNUMBER LIBRARIES"
  281. fi
  282. else
  283. MESSAGE=" YOU HAVE INSTALLED $APPSNUMBER APPLICATIONS OUT OF $AVAILABLE_APPS_NUMBER AVAILABLE"
  284. fi
  285. }
  286. _list_appimages() {
  287. # Determine the number of available apps
  288. APPIMAGE_NAMES=$(curl -Ls https://portable-linux-apps.github.io/appimages.md | tr '/)' '\n' | grep -i ".md$" | uniq | sed 's/.md$//g' | grep -v "\[")
  289. if ! test -f "$AMCACHEDIR/$ARCH-appimages"; then
  290. _online_check
  291. for appimage in $APPIMAGE_NAMES; do
  292. grep "◆ $appimage :" "$AMDATADIR/$ARCH-apps" >> "$AMCACHEDIR/$ARCH-appimages" &
  293. done
  294. fi
  295. AVAILABLE_APPIMAGES_NUMBER=$(grep -e "$" -c "$AMCACHEDIR/$ARCH-appimages")
  296. }
  297. _list_msg_head() {
  298. echo "" && echo "$MESSAGE" && echo "" && echo "$MESSAGE2" && echo ""
  299. }
  300. _list_msg_footer() {
  301. echo "$LIST" && echo ""
  302. }
  303. _list_appimages_msg() {
  304. _list_msg_head
  305. echo " LIST OF THE $AVAILABLE_APPIMAGES_NUMBER APPIMAGES AVAILABLE IN THE 'AM' REPOSITORY:"
  306. _list_msg_footer
  307. }
  308. _list_apps_msg() {
  309. _list_msg_head
  310. echo " LIST OF THE $AVAILABLE_APPS_NUMBER APPLICATIONS AVAILABLE IN THE 'AM' REPOSITORY:"
  311. _list_msg_footer
  312. }
  313. ################################################################################
  314. # USAGE
  315. ################################################################################
  316. case "$1" in
  317. '-a'|'about')
  318. [ -z "$2" ] && echo " USAGE: $AMCLI $1 [ARGUMENT]" && exit 1
  319. _online_check
  320. printf "%s\n" "-----------------------------------------------------------------------"
  321. [ ! -f "$AMCACHEDIR"/version-args ] && _check_version
  322. entries="$(echo "$@" | cut -f2- -d ' ')" # Removes first argument
  323. for arg in $entries; do
  324. if curl -o /dev/null -sIf "$AMCATALOGUEMARKDOWNS/${arg}.md" 1>/dev/null; then
  325. _about_page "$arg"
  326. else
  327. printf " ERROR: %b not found\033[0m\n" "${RED}\"$arg\""
  328. fi
  329. printf "%s\n" "-----------------------------------------------------------------------"
  330. done
  331. ;;
  332. '-f'|'files')
  333. if [ "$2" = "--less" ]; then
  334. _files_number && echo "$FILES_NUMBER"
  335. [ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && echo "$DIVIDING_LINE" && _files_number && echo "$FILES_NUMBER"
  336. elif [ "$2" = "--byname" ]; then
  337. _files_sort_by_name
  338. [ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && APPMAN_ON=1 && _files_number && [ "$FILES_NUMBER" != 0 ] && echo "$DIVIDING_LINE" && _files_sort_by_name
  339. _betatester_message_on
  340. else
  341. _files_sort_by_size
  342. [ "$AMCLI" = am ] && [ -f "$APPMANCONFIG"/appman-config ] && _appman && APPMAN_ON=1 && _files_number && [ "$FILES_NUMBER" != 0 ] && echo "$DIVIDING_LINE" && _files_sort_by_size
  343. _betatester_message_on
  344. fi
  345. ;;
  346. '-l'|'list')
  347. _list
  348. if [ "$2" = --appimages ]; then
  349. _list_appimages
  350. LIST=$(sort "$AMCACHEDIR/$ARCH-appimages" | _pretty_list)
  351. _list_appimages_msg | less -Ir
  352. else
  353. LIST=$(sort "$AMDATADIR/$ARCH-apps" | _pretty_list)
  354. _list_apps_msg | less -Ir
  355. fi
  356. #printf "\n$MESSAGE\n$MESSAGE2\n"
  357. ;;
  358. '-q'|'query')
  359. if [ -z "$2" ]; then
  360. echo " USAGE: $AMCLI $1 [ARGUMENT]"
  361. echo " $AMCLI $1 --appimages [ARGUMENT]"
  362. echo " $AMCLI $1 --pkg [ARGUMENT]"; exit 1
  363. fi
  364. wget -q --tries=10 --timeout=20 --spider https://github.com && _completion_lists
  365. if [ "$2" = --pkg ]; then
  366. entries="$(echo "$@" | cut -f3- -d ' ' | tr -s ' ' '|')"
  367. printf "\n Search results for packages: %s\n\n" "$entries" | tr '[:lower:]' '[:upper:]'
  368. grep -iE "$entries" "$AMDATADIR/$ARCH-apps" --color=always | _pretty_list_compat
  369. elif [ "$2" = --appimages ]; then
  370. entries="$(echo "$@" | cut -f3- -d ' ')"
  371. printf "\n Search results for \"%s\":\n\n" "$entries" | tr '[:lower:]' '[:upper:]'
  372. pattern="$(echo "$entries" | sed 's/ /(?=.*/g; s/$/)/g; s/(/)(/g; s/^/(?=.*/g;')"
  373. _list_appimages
  374. grep -Pi "$pattern" "$AMCACHEDIR/$ARCH-appimages" | _pretty_list_compat
  375. else
  376. entries="$(echo "$@" | cut -f2- -d ' ')"
  377. printf "\n Search results for \"%s\":\n\n" "$entries" | tr '[:lower:]' '[:upper:]'
  378. pattern="$(echo "$entries" | sed 's/ /(?=.*/g; s/$/)/g; s/(/)(/g; s/^/(?=.*/g;')"
  379. grep -Pi "$pattern" "$AMDATADIR/$ARCH-apps" | _pretty_list_compat
  380. fi
  381. printf '\n'
  382. ;;
  383. esac