freedombone-utils-selector 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  94. new_available_variants_list=()
  95. for filename in $FILES
  96. do
  97. system_variants_list=$(app_variants $filename)
  98. variants_array=($system_variants_list)
  99. for variant_str in "${variants_array[@]}"
  100. do
  101. item_in_array "${variant_str}" "${new_available_variants_list[@]}"
  102. if [[ $? != 0 ]]; then
  103. new_available_variants_list+=("$variant_str")
  104. fi
  105. done
  106. done
  107. available_variants_list=($(sort <<<"${new_available_variants_list[*]}"))
  108. }
  109. function is_valid_variant {
  110. sys_type="$1"
  111. available_variants_list=()
  112. available_system_variants
  113. for variant_str in "${available_variants_list[@]}"
  114. do
  115. if [[ "$sys_type" == "$variant_str" ]]; then
  116. return "1"
  117. fi
  118. done
  119. return "0"
  120. }
  121. function show_available_variants {
  122. available_variants_list=()
  123. available_system_variants
  124. for variant_str in "${available_variants_list[@]}"
  125. do
  126. echo " $variant_str"
  127. done
  128. }
  129. # mark a given app as having been removed so that it doesn't get reinstalled on updates
  130. function remove_app {
  131. app_name=$1
  132. if [ ! -f $REMOVED_APPS_FILE ]; then
  133. touch $REMOVED_APPS_FILE
  134. fi
  135. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  136. echo "_${app_name}_" >> $REMOVED_APPS_FILE
  137. fi
  138. if grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  139. sed -i "/install_${app_name}/d" $COMPLETION_FILE
  140. fi
  141. if grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  142. sed -i "/install_${app_name}/d" $INSTALLED_APPS_LIST
  143. fi
  144. }
  145. # returns 1 if an app has been marked as removed
  146. function app_is_removed {
  147. app_name="$1"
  148. if [ ! -f $REMOVED_APPS_FILE ]; then
  149. echo "0"
  150. return
  151. fi
  152. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  153. echo "0"
  154. else
  155. echo "1"
  156. fi
  157. }
  158. # Allows an app to be reinstalled even if it was previously marked as being removed
  159. function reinstall_app {
  160. app_name=$1
  161. if [ ! -f $REMOVED_APPS_FILE ]; then
  162. return
  163. fi
  164. if [[ $(app_is_removed $app_name) == "1" ]]; then
  165. sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
  166. fi
  167. }
  168. # returns 1 if an app is installed
  169. function app_is_installed {
  170. app_name="$1"
  171. # Why does this secondary file exist, apart from COMPLETION_FILE ?
  172. # It's so that it is visible to unprivileged users from the user control panel
  173. if [ -f $INSTALLED_APPS_LIST ]; then
  174. if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  175. echo "0"
  176. else
  177. echo "1"
  178. fi
  179. return
  180. fi
  181. # check the completion file to see if it was installed
  182. if [ ! -f $COMPLETION_FILE ]; then
  183. echo "0"
  184. return
  185. fi
  186. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  187. echo "0"
  188. else
  189. echo "1"
  190. fi
  191. }
  192. # called at the end of the install section of an app script
  193. function install_completed {
  194. if [ ! ${1} ]; then
  195. exit 673935
  196. fi
  197. echo "install_${1}" >> $COMPLETION_FILE
  198. }
  199. # populates an array of "0" or "1" for whether apps are installed
  200. function get_apps_installed {
  201. for a in "${APPS_AVAILABLE[@]}"
  202. do
  203. APPS_INSTALLED+=("$(app_is_installed $a)")
  204. done
  205. }
  206. # populates an array of installed app names
  207. function get_apps_installed_names {
  208. APPS_INSTALLED_NAMES=()
  209. for a in "${APPS_AVAILABLE[@]}"
  210. do
  211. if [[ $(app_is_installed $a) == "1" ]]; then
  212. APPS_INSTALLED_NAMES+=("$a")
  213. fi
  214. done
  215. }
  216. # detects what apps are available
  217. function detect_apps {
  218. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  219. APPS_AVAILABLE=()
  220. APPS_CHOSEN=()
  221. # for all the app scripts
  222. for filename in $FILES
  223. do
  224. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  225. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  226. if [[ $? != 0 ]]; then
  227. APPS_AVAILABLE+=("${app_name}")
  228. APPS_CHOSEN+=("0")
  229. fi
  230. done
  231. function_check get_apps_installed
  232. get_apps_installed
  233. get_apps_installed_names
  234. }
  235. # detects what apps are available and can be installed
  236. # If the variants list within an app script is an empty string then
  237. # it is considered to be too experimental to be installable
  238. function detect_installable_apps {
  239. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  240. APPS_AVAILABLE=()
  241. APPS_CHOSEN=()
  242. APPS_INSTALLED=()
  243. APPS_INSTALLED_NAMES=()
  244. function_check app_is_installed
  245. # for all the app scripts
  246. for filename in $FILES
  247. do
  248. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  249. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  250. if [[ $? != 0 ]]; then
  251. variants_list=$(app_variants $filename)
  252. # check for empty string
  253. if [ ${#variants_list} -gt 0 ]; then
  254. APPS_AVAILABLE+=("${app_name}")
  255. APPS_CHOSEN+=("0")
  256. APPS_INSTALLED+=("$(app_is_installed $app_name)")
  257. if [[ $(app_is_installed $app_name) == "1" ]]; then
  258. APPS_INSTALLED_NAMES+=("$app_name")
  259. fi
  260. fi
  261. fi
  262. done
  263. }
  264. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  265. # the given variant name
  266. function choose_apps_for_variant {
  267. variant_name="$1"
  268. if [ ${#variant_name} -eq 0 ]; then
  269. echo $"No variant name for choosing apps"
  270. exit 237567
  271. fi
  272. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  273. APPS_CHOSEN=()
  274. # for all the app scripts
  275. for filename in $FILES
  276. do
  277. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  278. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  279. if [[ $? == 0 ]]; then
  280. if grep -q "VARIANTS=" ${filename}; then
  281. variants_list=$(app_variants $filename)
  282. if [[ "${variants_list}" == 'all'* || \
  283. "${variants_list}" == "$variant_name" || \
  284. "${variants_list}" == "$variant_name "* || \
  285. "${variants_list}" == *" $variant_name "* || \
  286. "${variants_list}" == *" $variant_name" ]]; then
  287. if [[ $(app_is_removed ${a}) == "0" ]]; then
  288. echo $"${app_name} chosen"
  289. APPS_CHOSEN+=("1")
  290. else
  291. APPS_CHOSEN+=("0")
  292. fi
  293. else
  294. APPS_CHOSEN+=("0")
  295. fi
  296. else
  297. APPS_CHOSEN+=("0")
  298. fi
  299. fi
  300. done
  301. function_check get_apps_installed
  302. get_apps_installed
  303. }
  304. # show a list of apps which have been chosen
  305. function list_chosen_apps {
  306. app_index=0
  307. for a in "${APPS_AVAILABLE[@]}"
  308. do
  309. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  310. echo $"${a}"
  311. fi
  312. app_index=$[app_index+1]
  313. done
  314. }
  315. function remove_apps {
  316. app_index=0
  317. for a in "${APPS_AVAILABLE[@]}"
  318. do
  319. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  320. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  321. echo $"Removing application: ${a}"
  322. app_load_variables ${a}
  323. remove_app ${a}
  324. remove_${a}
  325. echo $"${a} was removed"
  326. fi
  327. fi
  328. app_index=$[app_index+1]
  329. done
  330. update_installed_apps_list
  331. }
  332. function install_apps {
  333. is_interactive=$1
  334. # interactive install configuration for each app
  335. if [ ${is_interactive} ]; then
  336. echo $"Interactive installer"
  337. app_index=0
  338. for a in "${APPS_AVAILABLE[@]}"
  339. do
  340. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  341. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  342. # interactively obtain settings for this app
  343. if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
  344. echo $"Interactive settings for ${a}"
  345. install_interactive_${a}
  346. fi
  347. fi
  348. fi
  349. app_index=$[app_index+1]
  350. done
  351. echo $"Interactive settings complete"
  352. fi
  353. # now install the apps
  354. app_index=0
  355. for a in "${APPS_AVAILABLE[@]}"
  356. do
  357. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  358. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  359. if [ ${is_interactive} ]; then
  360. # clears any removal indicator
  361. reinstall_app ${a}
  362. app_load_variables ${a}
  363. if [[ $(app_is_installed ${a}) == "1" ]]; then
  364. echo $"Upgrading application from interactive: ${a}"
  365. upgrade_${a}
  366. echo $"${a} was upgraded from interactive"
  367. else
  368. echo $"Installing application from interactive: ${a}"
  369. APP_INSTALLED=
  370. install_${a}
  371. if [ $APP_INSTALLED ]; then
  372. app_save_variables ${a}
  373. install_completed ${a}
  374. echo $"${a} was installed from interactive"
  375. else
  376. echo $"${a} was not installed from interactive"
  377. fi
  378. fi
  379. else
  380. # check if the app was removed
  381. if [[ $(app_is_removed ${a}) == "0" ]]; then
  382. app_load_variables ${a}
  383. if [[ $(app_is_installed ${a}) == "1" ]]; then
  384. echo $"Upgrading application: ${a}"
  385. upgrade_${a}
  386. echo $"${a} was upgraded"
  387. else
  388. echo $"Installing application: ${a}"
  389. APP_INSTALLED=
  390. install_${a}
  391. if [ $APP_INSTALLED ]; then
  392. app_save_variables ${a}
  393. install_completed ${a}
  394. echo $"${a} was installed"
  395. else
  396. echo $"${a} was not installed"
  397. fi
  398. fi
  399. else
  400. echo $"${a} has been removed and so will not be reinstalled"
  401. fi
  402. fi
  403. fi
  404. fi
  405. app_index=$[app_index+1]
  406. done
  407. update_installed_apps_list
  408. }
  409. # NOTE: deliberately no exit 0