freedombone-addremove 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. choices=$(dialog --stdout --backtitle $"Freedombone" \
  108. --title $"Add/Remove Applications" \
  109. --checklist $'Choose:' \
  110. 27 40 20 "$applist")
  111. # shellcheck disable=SC2181
  112. if [ $? -eq 0 ]; then
  113. for choice in $choices
  114. do
  115. app_index=$((choice-1))
  116. APPS_CHOSEN[$app_index]="1"
  117. done
  118. else
  119. exit 0
  120. fi
  121. }
  122. function remove_apps_selected {
  123. # which apps need to be removed?
  124. removals=""
  125. app_index=0
  126. n=0
  127. for a in "${APPS_INSTALLED[@]}"
  128. do
  129. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  130. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  131. if [ ${n} -gt 0 ]; then
  132. removals="$removals ${APPS_AVAILABLE[$app_index]}"
  133. else
  134. removals="${APPS_AVAILABLE[$app_index]}"
  135. fi
  136. n=$((n+1))
  137. fi
  138. fi
  139. app_index=$((app_index+1))
  140. done
  141. # if no apps to be removed then don't do anything
  142. if [ ${n} -eq 0 ]; then
  143. return
  144. fi
  145. # ask for confirmation
  146. dialog --title $"Remove applications" \
  147. --backtitle $"Freedombone" \
  148. --defaultno \
  149. --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
  150. sel=$?
  151. case $sel in
  152. 1) return;;
  153. 255) return;;
  154. esac
  155. clear
  156. # remove the apps
  157. read_configuration
  158. remove_apps
  159. }
  160. function install_apps_selected {
  161. # which apps need to be installed?
  162. select_all_apps=$1
  163. installs=""
  164. app_index=0
  165. n=0
  166. for a in "${APPS_INSTALLED[@]}"
  167. do
  168. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  169. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  170. if [ ${n} -gt 0 ]; then
  171. installs="$installs ${APPS_AVAILABLE[$app_index]}"
  172. else
  173. installs="${APPS_AVAILABLE[$app_index]}"
  174. fi
  175. n=$((n+1))
  176. fi
  177. fi
  178. app_index=$((app_index+1))
  179. done
  180. # if no apps to be installed then don't do anything
  181. if [ ${n} -eq 0 ]; then
  182. return
  183. fi
  184. if [[ "$select_all_apps" != "add-all" ]]; then
  185. # ask for confirmation
  186. if [ $n -eq 1 ]; then
  187. dialog --title $"$installs" \
  188. --backtitle $"Freedombone" \
  189. --defaultno \
  190. --yesno $"\\nThis will install the $installs app\\n\\nProceed?" 9 40
  191. else
  192. dialog_height=$((15 + "$n"))
  193. dialog --title $"Add applications" \
  194. --backtitle $"Freedombone" \
  195. --defaultno \
  196. --yesno $"\\nYou have chosen to install $n apps\\n\\n $installs\\n\\nProceed?" $dialog_height 60
  197. fi
  198. sel=$?
  199. case $sel in
  200. 1) return;;
  201. 255) return;;
  202. esac
  203. fi
  204. clear
  205. # install the apps
  206. read_configuration
  207. install_apps interactive
  208. if [ ! "$APP_INSTALLED_SUCCESS" ]; then
  209. echo $'One or more apps failed to install'
  210. fi
  211. }
  212. if [[ $1 == "test"* ]]; then
  213. if ! ${PROJECT_NAME}-tests; then
  214. exit 2
  215. fi
  216. fi
  217. detect_installable_apps
  218. # if no applications were found
  219. if [[ ${#APPS_AVAILABLE[@]} == 0 ]]; then
  220. exit 1
  221. fi
  222. show_apps "$1"
  223. mark_unselected_apps_as_removed "$1"
  224. clear
  225. remove_apps_selected
  226. if [[ $1 == "add-all" ]]; then
  227. install_apps_selected add-all
  228. else
  229. install_apps_selected
  230. fi
  231. exit 0