freedombone-utils-selector 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Functions for selecting which apps to install or remove
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # Array containing names of available apps
  31. APPS_AVAILABLE=()
  32. # Array containing 1 or 0 indicating installed apps
  33. APPS_INSTALLED=()
  34. # Apps selected with checklist
  35. APPS_CHOSEN=()
  36. # A list of the names of installed apps
  37. APPS_INSTALLED_NAMES=()
  38. # file containing a list of removed apps
  39. REMOVED_APPS_FILE=/root/removed
  40. INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
  41. if [ ! $COMPLETION_FILE ]; then
  42. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  43. fi
  44. # Loads variables defined at the beginning of an app script
  45. function app_load_variables {
  46. app_name=$1
  47. config_var_name=${app_name}_variables
  48. if [ ! ${!config_var_name} ]; then
  49. echo $"${app_name}_variables was not found"
  50. return
  51. fi
  52. configvarname=$config_var_name[@]
  53. configvarname=( ${!configvarname} )
  54. for v in "${configvarname[@]}"
  55. do
  56. read_config_param $v
  57. done
  58. }
  59. # Saves variables for a given app script
  60. function app_save_variables {
  61. app_name=$1
  62. config_var_name=${app_name}_variables
  63. if [ ! ${!config_var_name} ]; then
  64. return
  65. fi
  66. configvarname=$config_var_name[@]
  67. configvarname=( ${!configvarname} )
  68. for v in "${configvarname[@]}"
  69. do
  70. write_config_param $v "${!v}"
  71. done
  72. }
  73. # gets the variants list from an app script
  74. function app_variants {
  75. filename=$1
  76. variants_line=$(cat ${filename} | grep 'VARIANTS=')
  77. if [[ "$variants_line" == *"'"* ]]; then
  78. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  79. else
  80. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  81. fi
  82. echo "$variants_list"
  83. }
  84. # whether a given item is in an array
  85. function item_in_array {
  86. local e
  87. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  88. return 1
  89. }
  90. # returns a list of available system variants
  91. # based upon the variants string in each app script
  92. function available_system_variants {
  93. function_check item_in_array
  94. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  95. new_available_variants_list=()
  96. for filename in $FILES
  97. do
  98. system_variants_list=$(app_variants $filename)
  99. variants_array=($system_variants_list)
  100. for variant_str in "${variants_array[@]}"
  101. do
  102. item_in_array "${variant_str}" "${new_available_variants_list[@]}"
  103. if [[ $? != 0 ]]; then
  104. new_available_variants_list+=("$variant_str")
  105. fi
  106. done
  107. done
  108. available_variants_list=($(sort <<<"${new_available_variants_list[*]}"))
  109. }
  110. function is_valid_variant {
  111. sys_type="$1"
  112. available_variants_list=()
  113. function_check available_system_variants
  114. available_system_variants
  115. for variant_str in "${available_variants_list[@]}"
  116. do
  117. if [[ "$sys_type" == "$variant_str" ]]; then
  118. return "1"
  119. fi
  120. done
  121. return "0"
  122. }
  123. function show_available_variants {
  124. available_variants_list=()
  125. function_check available_system_variants
  126. available_system_variants
  127. for variant_str in "${available_variants_list[@]}"
  128. do
  129. echo " $variant_str"
  130. done
  131. }
  132. # mark a given app as having been removed so that it doesn't get reinstalled on updates
  133. function remove_app {
  134. app_name=$1
  135. if [ ! -f $REMOVED_APPS_FILE ]; then
  136. touch $REMOVED_APPS_FILE
  137. fi
  138. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  139. echo "_${app_name}_" >> $REMOVED_APPS_FILE
  140. fi
  141. if grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  142. sed -i "/install_${app_name}/d" $COMPLETION_FILE
  143. fi
  144. if grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  145. sed -i "/install_${app_name}/d" $INSTALLED_APPS_LIST
  146. fi
  147. }
  148. # returns 1 if an app has been marked as removed
  149. function app_is_removed {
  150. app_name="$1"
  151. if [ ! -f $REMOVED_APPS_FILE ]; then
  152. echo "0"
  153. return
  154. fi
  155. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  156. echo "0"
  157. else
  158. echo "1"
  159. fi
  160. }
  161. # Allows an app to be reinstalled even if it was previously marked as being removed
  162. function reinstall_app {
  163. app_name=$1
  164. if [ ! -f $REMOVED_APPS_FILE ]; then
  165. return
  166. fi
  167. if [[ $(app_is_removed $app_name) == "1" ]]; then
  168. sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
  169. fi
  170. }
  171. # returns 1 if an app is installed
  172. function app_is_installed {
  173. app_name="$1"
  174. # Why does this secondary file exist, apart from COMPLETION_FILE ?
  175. # It's so that it is visible to unprivileged users from the user control panel
  176. if [ -f $INSTALLED_APPS_LIST ]; then
  177. if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  178. echo "0"
  179. else
  180. echo "1"
  181. fi
  182. return
  183. fi
  184. # check the completion file to see if it was installed
  185. if [ ! -f $COMPLETION_FILE ]; then
  186. echo "0"
  187. return
  188. fi
  189. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  190. echo "0"
  191. else
  192. echo "1"
  193. fi
  194. }
  195. # called at the end of the install section of an app script
  196. function install_completed {
  197. if [ ! ${1} ]; then
  198. exit 673935
  199. fi
  200. if ! grep -Fxq "install_${1}" $COMPLETION_FILE; then
  201. echo "install_${1}" >> $COMPLETION_FILE
  202. fi
  203. }
  204. # populates an array of "0" or "1" for whether apps are installed
  205. function get_apps_installed {
  206. for a in "${APPS_AVAILABLE[@]}"
  207. do
  208. APPS_INSTALLED+=("$(app_is_installed $a)")
  209. done
  210. }
  211. # populates an array of installed app names
  212. function get_apps_installed_names {
  213. APPS_INSTALLED_NAMES=()
  214. for a in "${APPS_AVAILABLE[@]}"
  215. do
  216. if [[ $(app_is_installed $a) == "1" ]]; then
  217. APPS_INSTALLED_NAMES+=("$a")
  218. fi
  219. done
  220. }
  221. # detects what apps are available
  222. function detect_apps {
  223. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  224. function_check item_in_array
  225. APPS_AVAILABLE=()
  226. APPS_CHOSEN=()
  227. # for all the app scripts
  228. for filename in $FILES
  229. do
  230. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  231. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  232. if [[ $? != 0 ]]; then
  233. APPS_AVAILABLE+=("${app_name}")
  234. APPS_CHOSEN+=("0")
  235. fi
  236. done
  237. function_check get_apps_installed
  238. get_apps_installed
  239. get_apps_installed_names
  240. }
  241. # detects what apps are available and can be installed
  242. # If the variants list within an app script is an empty string then
  243. # it is considered to be too experimental to be installable
  244. function detect_installable_apps {
  245. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  246. APPS_AVAILABLE=()
  247. APPS_CHOSEN=()
  248. APPS_INSTALLED=()
  249. APPS_INSTALLED_NAMES=()
  250. function_check app_variants
  251. function_check app_is_installed
  252. function_check item_in_array
  253. # for all the app scripts
  254. for filename in $FILES
  255. do
  256. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  257. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  258. if [[ $? != 0 ]]; then
  259. variants_list=$(app_variants $filename)
  260. # check for empty string
  261. if [ ${#variants_list} -gt 0 ]; then
  262. APPS_AVAILABLE+=("${app_name}")
  263. APPS_CHOSEN+=("0")
  264. APPS_INSTALLED+=("$(app_is_installed $app_name)")
  265. if [[ $(app_is_installed $app_name) == "1" ]]; then
  266. APPS_INSTALLED_NAMES+=("$app_name")
  267. fi
  268. fi
  269. fi
  270. done
  271. }
  272. function choose_all_installable_apps {
  273. # selects all of the apps
  274. app_index=0
  275. for a in "${APPS_AVAILABLE[@]}"
  276. do
  277. APPS_CHOSEN[$app_index]="1"
  278. app_index=$[app_index+1]
  279. done
  280. }
  281. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  282. # the given variant name
  283. function choose_apps_for_variant {
  284. variant_name="$1"
  285. function_check item_in_array
  286. function_check app_variants
  287. function_check app_is_removed
  288. if [ ${#variant_name} -eq 0 ]; then
  289. echo $"No variant name for choosing apps"
  290. exit 237567
  291. fi
  292. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  293. APPS_CHOSEN=()
  294. # for all the app scripts
  295. for filename in $FILES
  296. do
  297. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  298. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  299. if [[ $? == 0 ]]; then
  300. if grep -q "VARIANTS=" ${filename}; then
  301. variants_list=$(app_variants $filename)
  302. if [[ "${variants_list}" == 'all'* || \
  303. "${variants_list}" == "$variant_name" || \
  304. "${variants_list}" == "$variant_name "* || \
  305. "${variants_list}" == *" $variant_name "* || \
  306. "${variants_list}" == *" $variant_name" ]]; then
  307. if [[ $(app_is_removed ${a}) == "0" ]]; then
  308. echo $"${app_name} chosen"
  309. APPS_CHOSEN+=("1")
  310. else
  311. APPS_CHOSEN+=("0")
  312. fi
  313. else
  314. APPS_CHOSEN+=("0")
  315. fi
  316. else
  317. APPS_CHOSEN+=("0")
  318. fi
  319. fi
  320. done
  321. function_check get_apps_installed
  322. get_apps_installed
  323. }
  324. # show a list of apps which have been chosen
  325. function list_chosen_apps {
  326. app_index=0
  327. for a in "${APPS_AVAILABLE[@]}"
  328. do
  329. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  330. echo $"${a}"
  331. fi
  332. app_index=$[app_index+1]
  333. done
  334. }
  335. function remove_apps {
  336. app_index=0
  337. for a in "${APPS_AVAILABLE[@]}"
  338. do
  339. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  340. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  341. echo $"Removing application: ${a}"
  342. function_check app_load_variables
  343. app_load_variables ${a}
  344. function_check remove_app
  345. remove_app ${a}
  346. function_check remove_${a}
  347. remove_${a}
  348. echo $"${a} was removed"
  349. fi
  350. fi
  351. app_index=$[app_index+1]
  352. done
  353. update_installed_apps_list
  354. }
  355. function install_apps_interactive {
  356. echo $"Interactive installer"
  357. app_index=0
  358. for a in "${APPS_AVAILABLE[@]}"
  359. do
  360. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  361. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  362. # interactively obtain settings for this app
  363. if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
  364. install_interactive_${a}
  365. fi
  366. fi
  367. fi
  368. app_index=$[app_index+1]
  369. done
  370. echo $"Interactive settings complete"
  371. }
  372. function install_apps {
  373. is_interactive=$1
  374. # interactive install configuration for each app
  375. if [ ${is_interactive} ]; then
  376. install_apps_interactive
  377. fi
  378. # now install the apps
  379. app_index=0
  380. for a in "${APPS_AVAILABLE[@]}"
  381. do
  382. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  383. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  384. if [ ${is_interactive} ]; then
  385. # clears any removal indicator
  386. function_check reinstall_app
  387. reinstall_app ${a}
  388. function_check app_load_variables
  389. app_load_variables ${a}
  390. if [[ $(app_is_installed ${a}) == "1" ]]; then
  391. echo $"Upgrading application from interactive: ${a}"
  392. upgrade_${a}
  393. echo $"${a} was upgraded from interactive"
  394. else
  395. echo $"Installing application from interactive: ${a}"
  396. APP_INSTALLED=
  397. install_${a}
  398. if [ $APP_INSTALLED ]; then
  399. function_check app_save_variables
  400. app_save_variables ${a}
  401. function_check install_completed
  402. install_completed ${a}
  403. echo $"${a} was installed from interactive"
  404. else
  405. echo $"${a} was not installed from interactive"
  406. fi
  407. fi
  408. else
  409. # check if the app was removed
  410. if [[ $(app_is_removed ${a}) == "0" ]]; then
  411. function_check app_load_variables
  412. app_load_variables ${a}
  413. if [[ $(app_is_installed ${a}) == "1" ]]; then
  414. echo $"Upgrading application: ${a}"
  415. upgrade_${a}
  416. echo $"${a} was upgraded"
  417. else
  418. echo $"Installing application: ${a}"
  419. APP_INSTALLED=
  420. install_${a}
  421. if [ $APP_INSTALLED ]; then
  422. function_check app_save_variables
  423. app_save_variables ${a}
  424. function_check install_completed
  425. install_completed ${a}
  426. echo $"${a} was installed"
  427. else
  428. echo $"${a} was not installed"
  429. fi
  430. fi
  431. else
  432. echo $"${a} has been removed and so will not be reinstalled"
  433. fi
  434. fi
  435. fi
  436. fi
  437. app_index=$[app_index+1]
  438. done
  439. function_check update_installed_apps_list
  440. update_installed_apps_list
  441. }
  442. # NOTE: deliberately no exit 0