freedombone-addremove 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Add or remove apps
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  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. PROJECT_NAME='freedombone'
  31. export TEXTDOMAIN=${PROJECT_NAME}-addremove
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. PROJECT_INSTALL_DIR=/usr/local/bin
  34. if [ -f "/usr/bin/${PROJECT_NAME}" ]; then
  35. PROJECT_INSTALL_DIR=/usr/bin
  36. fi
  37. COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt"
  38. CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
  39. # Start including files
  40. source "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars"
  41. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  42. for f in $UTILS_FILES
  43. do
  44. source "$f"
  45. done
  46. APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
  47. for f in $APP_FILES
  48. do
  49. source "$f"
  50. done
  51. # End including files
  52. function mark_unselected_apps_as_removed {
  53. # Initially mark the apps not chosen on first install as being removed
  54. # otherwise they may be automatically installed on the next update
  55. select_all_apps=$1
  56. if [[ "$select_all_apps" != "add-all" ]]; then
  57. return
  58. fi
  59. if [ -f "$REMOVED_APPS_FILE" ]; then
  60. rm "$REMOVED_APPS_FILE"
  61. fi
  62. app_index=0
  63. # shellcheck disable=SC2068
  64. for app_name in ${APPS_AVAILABLE[@]}
  65. do
  66. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  67. echo "_${app_name}_" >> "$REMOVED_APPS_FILE"
  68. fi
  69. app_index=$((app_index+1))
  70. done
  71. }
  72. function app_expected_to_be_installed {
  73. # is the given application expected to be installed by default?
  74. select_all_apps="$1"
  75. app_name="$2"
  76. read_config_param ONION_ONLY
  77. if [[ "$select_all_apps" == "add-all" ]]; then
  78. if [[ $ONION_ONLY != 'no' && "$app_name" == "hubzilla" ]]; then
  79. echo "0"
  80. return
  81. fi
  82. if ! grep -q "IN_DEFAULT_INSTALL=1" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}"; then
  83. echo "0"
  84. return
  85. fi
  86. fi
  87. echo "1"
  88. }
  89. function show_apps {
  90. select_all_apps="$1"
  91. applist=""
  92. n=1
  93. app_index=0
  94. # shellcheck disable=SC2068
  95. for a in ${APPS_AVAILABLE[@]}
  96. do
  97. if [[ ${APPS_INSTALLED[$app_index]} == "0" && "$select_all_apps" != "add-all" ]]; then
  98. applist="$applist $n $a off"
  99. else
  100. if [[ $(app_expected_to_be_installed "$select_all_apps" "$a") == "0" ]]; then
  101. applist="$applist $n $a off"
  102. else
  103. applist="$applist $n $a on"
  104. fi
  105. fi
  106. n=$((n+1))
  107. app_index=$((app_index+1))
  108. done
  109. # shellcheck disable=SC2086
  110. choices=$(dialog --stdout --backtitle $"Freedombone" \
  111. --title $"Add/Remove Applications" \
  112. --checklist $'Choose:' \
  113. 27 40 20 $applist)
  114. # shellcheck disable=SC2181
  115. if [ $? -eq 0 ]; then
  116. for choice in $choices
  117. do
  118. app_index=$((choice-1))
  119. APPS_CHOSEN[$app_index]="1"
  120. done
  121. else
  122. exit 0
  123. fi
  124. }
  125. function remove_apps_selected {
  126. # which apps need to be removed?
  127. removals=""
  128. app_index=0
  129. n=0
  130. # shellcheck disable=SC2068
  131. for a in ${APPS_INSTALLED[@]}
  132. do
  133. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  134. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  135. if [ ${n} -gt 0 ]; then
  136. removals="$removals ${APPS_AVAILABLE[$app_index]}"
  137. else
  138. removals="${APPS_AVAILABLE[$app_index]}"
  139. fi
  140. n=$((n+1))
  141. fi
  142. fi
  143. app_index=$((app_index+1))
  144. done
  145. # if no apps to be removed then don't do anything
  146. if [ ${n} -eq 0 ]; then
  147. return
  148. fi
  149. # ask for confirmation
  150. dialog --title $"Remove applications" \
  151. --backtitle $"Freedombone" \
  152. --defaultno \
  153. --yesno $"\\nYou have chosen to remove $n apps.\\n\\n $removals\\n\\nIf you choose 'yes' then this will remove both the applications and their data/messages. If you don't have a backup then you will not be able to recover the data for these applications.\\n\\nAre you sure that you wish to continue?" 15 60
  154. sel=$?
  155. case $sel in
  156. 1) return;;
  157. 255) return;;
  158. esac
  159. clear
  160. # remove the apps
  161. read_configuration
  162. remove_apps
  163. }
  164. function install_apps_selected {
  165. # which apps need to be installed?
  166. select_all_apps=$1
  167. installs=""
  168. app_index=0
  169. n=0
  170. # shellcheck disable=SC2068
  171. for a in ${APPS_INSTALLED[@]}
  172. do
  173. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  174. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  175. if [ ${n} -gt 0 ]; then
  176. installs="$installs ${APPS_AVAILABLE[$app_index]}"
  177. else
  178. installs="${APPS_AVAILABLE[$app_index]}"
  179. fi
  180. n=$((n+1))
  181. fi
  182. fi
  183. app_index=$((app_index+1))
  184. done
  185. # if no apps to be installed then don't do anything
  186. if [ ${n} -eq 0 ]; then
  187. return
  188. fi
  189. if [[ "$select_all_apps" != "add-all" ]]; then
  190. # ask for confirmation
  191. if [ $n -eq 1 ]; then
  192. dialog --title $"$installs" \
  193. --backtitle $"Freedombone" \
  194. --defaultno \
  195. --yesno $"\\nThis will install the $installs app\\n\\nProceed?" 9 40
  196. else
  197. dialog_height=$((15 + "$n"))
  198. dialog --title $"Add applications" \
  199. --backtitle $"Freedombone" \
  200. --defaultno \
  201. --yesno $"\\nYou have chosen to install $n apps\\n\\n $installs\\n\\nProceed?" $dialog_height 60
  202. fi
  203. sel=$?
  204. case $sel in
  205. 1) return;;
  206. 255) return;;
  207. esac
  208. fi
  209. clear
  210. # install the apps
  211. read_configuration
  212. install_apps interactive
  213. if [ ! "$APP_INSTALLED_SUCCESS" ]; then
  214. echo $'One or more apps failed to install'
  215. fi
  216. }
  217. if [[ $1 == "test"* ]]; then
  218. if ! ${PROJECT_NAME}-tests; then
  219. exit 2
  220. fi
  221. fi
  222. detect_installable_apps
  223. # if no applications were found
  224. if [[ ${#APPS_AVAILABLE[@]} == 0 ]]; then
  225. exit 1
  226. fi
  227. show_apps "$1"
  228. mark_unselected_apps_as_removed "$1"
  229. clear
  230. remove_apps_selected
  231. if [[ "$1" == "add-all" ]]; then
  232. install_apps_selected add-all
  233. else
  234. install_apps_selected
  235. fi
  236. exit 0