freedombone-addremove 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. for app_name in "${APPS_AVAILABLE[@]}"
  64. do
  65. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  66. echo "_${app_name}_" >> "$REMOVED_APPS_FILE"
  67. fi
  68. app_index=$((app_index+1))
  69. done
  70. }
  71. function app_expected_to_be_installed {
  72. # is the given application expected to be installed by default?
  73. select_all_apps="$1"
  74. app_name="$2"
  75. read_config_param ONION_ONLY
  76. if [[ "$select_all_apps" == "add-all" ]]; then
  77. if [[ $ONION_ONLY != 'no' && "$app_name" == "hubzilla" ]]; then
  78. echo "0"
  79. return
  80. fi
  81. if ! grep -q "IN_DEFAULT_INSTALL=1" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}"; then
  82. echo "0"
  83. return
  84. fi
  85. fi
  86. echo "1"
  87. }
  88. function show_apps {
  89. select_all_apps="$1"
  90. applist=""
  91. n=1
  92. app_index=0
  93. for a in "${APPS_AVAILABLE[@]}"
  94. do
  95. if [[ ${APPS_INSTALLED[$app_index]} == "0" && "$select_all_apps" != "add-all" ]]; then
  96. applist="$applist $n $a off"
  97. else
  98. if [[ $(app_expected_to_be_installed "$select_all_apps" "$a") == "0" ]]; then
  99. applist="$applist $n $a off"
  100. else
  101. applist="$applist $n $a on"
  102. fi
  103. fi
  104. n=$((n+1))
  105. app_index=$((app_index+1))
  106. done
  107. # shellcheck disable=SC2086
  108. choices=$(dialog --stdout --backtitle $"Freedombone" \
  109. --title $"Add/Remove Applications" \
  110. --checklist $'Choose:' \
  111. 27 40 20 $applist)
  112. # shellcheck disable=SC2181
  113. if [ $? -eq 0 ]; then
  114. for choice in $choices
  115. do
  116. app_index=$((choice-1))
  117. APPS_CHOSEN[$app_index]="1"
  118. done
  119. else
  120. exit 0
  121. fi
  122. }
  123. function remove_apps_selected {
  124. # which apps need to be removed?
  125. removals=""
  126. app_index=0
  127. n=0
  128. for a in "${APPS_INSTALLED[@]}"
  129. do
  130. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  131. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  132. if [ ${n} -gt 0 ]; then
  133. removals="$removals ${APPS_AVAILABLE[$app_index]}"
  134. else
  135. removals="${APPS_AVAILABLE[$app_index]}"
  136. fi
  137. n=$((n+1))
  138. fi
  139. fi
  140. app_index=$((app_index+1))
  141. done
  142. # if no apps to be removed then don't do anything
  143. if [ ${n} -eq 0 ]; then
  144. return
  145. fi
  146. # ask for confirmation
  147. dialog --title $"Remove applications" \
  148. --backtitle $"Freedombone" \
  149. --defaultno \
  150. --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
  151. sel=$?
  152. case $sel in
  153. 1) return;;
  154. 255) return;;
  155. esac
  156. clear
  157. # remove the apps
  158. read_configuration
  159. remove_apps
  160. }
  161. function install_apps_selected {
  162. # which apps need to be installed?
  163. select_all_apps=$1
  164. installs=""
  165. app_index=0
  166. n=0
  167. for a in "${APPS_INSTALLED[@]}"
  168. do
  169. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  170. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  171. if [ ${n} -gt 0 ]; then
  172. installs="$installs ${APPS_AVAILABLE[$app_index]}"
  173. else
  174. installs="${APPS_AVAILABLE[$app_index]}"
  175. fi
  176. n=$((n+1))
  177. fi
  178. fi
  179. app_index=$((app_index+1))
  180. done
  181. # if no apps to be installed then don't do anything
  182. if [ ${n} -eq 0 ]; then
  183. return
  184. fi
  185. if [[ "$select_all_apps" != "add-all" ]]; then
  186. # ask for confirmation
  187. if [ $n -eq 1 ]; then
  188. dialog --title $"$installs" \
  189. --backtitle $"Freedombone" \
  190. --defaultno \
  191. --yesno $"\\nThis will install the $installs app\\n\\nProceed?" 9 40
  192. else
  193. dialog_height=$((15 + "$n"))
  194. dialog --title $"Add applications" \
  195. --backtitle $"Freedombone" \
  196. --defaultno \
  197. --yesno $"\\nYou have chosen to install $n apps\\n\\n $installs\\n\\nProceed?" $dialog_height 60
  198. fi
  199. sel=$?
  200. case $sel in
  201. 1) return;;
  202. 255) return;;
  203. esac
  204. fi
  205. clear
  206. # install the apps
  207. read_configuration
  208. install_apps interactive
  209. if [ ! "$APP_INSTALLED_SUCCESS" ]; then
  210. echo $'One or more apps failed to install'
  211. fi
  212. }
  213. if [[ $1 == "test"* ]]; then
  214. if ! ${PROJECT_NAME}-tests; then
  215. exit 2
  216. fi
  217. fi
  218. detect_installable_apps
  219. # if no applications were found
  220. if [[ ${#APPS_AVAILABLE[@]} == 0 ]]; then
  221. exit 1
  222. fi
  223. show_apps "$1"
  224. mark_unselected_apps_as_removed "$1"
  225. clear
  226. remove_apps_selected
  227. if [[ "$1" == "add-all" ]]; then
  228. install_apps_selected add-all
  229. else
  230. install_apps_selected
  231. fi
  232. exit 0