freedombone-addremove 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Add or remove apps
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-addremove
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. PROJECT_INSTALL_DIR=/usr/local/bin
  32. if [ -f "/usr/bin/${PROJECT_NAME}" ]; then
  33. PROJECT_INSTALL_DIR=/usr/bin
  34. fi
  35. COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt"
  36. CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
  37. # Start including files
  38. source "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars"
  39. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  40. for f in $UTILS_FILES
  41. do
  42. source "$f"
  43. done
  44. APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
  45. for f in $APP_FILES
  46. do
  47. source "$f"
  48. done
  49. # End including files
  50. function mark_unselected_apps_as_removed {
  51. # Initially mark the apps not chosen on first install as being removed
  52. # otherwise they may be automatically installed on the next update
  53. select_all_apps=$1
  54. if [[ "$select_all_apps" != "add-all" ]]; then
  55. return
  56. fi
  57. if [ -f "$REMOVED_APPS_FILE" ]; then
  58. rm "$REMOVED_APPS_FILE"
  59. fi
  60. app_index=0
  61. # shellcheck disable=SC2068
  62. for app_name in ${APPS_AVAILABLE[@]}
  63. do
  64. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  65. echo "_${app_name}_" >> "$REMOVED_APPS_FILE"
  66. fi
  67. app_index=$((app_index+1))
  68. done
  69. }
  70. function app_expected_to_be_installed {
  71. # is the given application expected to be installed by default?
  72. select_all_apps="$1"
  73. app_name="$2"
  74. read_config_param ONION_ONLY
  75. if [[ "$select_all_apps" == "add-all" ]]; then
  76. if [[ $ONION_ONLY != 'no' && "$app_name" == "hubzilla" ]]; then
  77. echo "0"
  78. return
  79. fi
  80. if ! grep -q "IN_DEFAULT_INSTALL=1" "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}"; then
  81. echo "0"
  82. return
  83. fi
  84. fi
  85. echo "1"
  86. }
  87. function show_apps {
  88. select_all_apps="$1"
  89. applist=""
  90. n=1
  91. app_index=0
  92. # shellcheck disable=SC2068
  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. # shellcheck disable=SC2068
  129. for a in ${APPS_INSTALLED[@]}
  130. do
  131. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  132. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  133. if [ ${n} -gt 0 ]; then
  134. removals="$removals ${APPS_AVAILABLE[$app_index]}"
  135. else
  136. removals="${APPS_AVAILABLE[$app_index]}"
  137. fi
  138. n=$((n+1))
  139. fi
  140. fi
  141. app_index=$((app_index+1))
  142. done
  143. # if no apps to be removed then don't do anything
  144. if [ ${n} -eq 0 ]; then
  145. return
  146. fi
  147. # ask for confirmation
  148. dialog --title $"Remove applications" \
  149. --backtitle $"Freedombone" \
  150. --defaultno \
  151. --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
  152. sel=$?
  153. case $sel in
  154. 1) return;;
  155. 255) return;;
  156. esac
  157. clear
  158. # remove the apps
  159. read_configuration
  160. remove_apps
  161. }
  162. function install_apps_selected {
  163. # which apps need to be installed?
  164. select_all_apps=$1
  165. installs=""
  166. app_index=0
  167. n=0
  168. # shellcheck disable=SC2068
  169. for a in ${APPS_INSTALLED[@]}
  170. do
  171. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  172. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  173. if [ ${n} -gt 0 ]; then
  174. installs="$installs ${APPS_AVAILABLE[$app_index]}"
  175. else
  176. installs="${APPS_AVAILABLE[$app_index]}"
  177. fi
  178. n=$((n+1))
  179. fi
  180. fi
  181. app_index=$((app_index+1))
  182. done
  183. # if no apps to be installed then don't do anything
  184. if [ ${n} -eq 0 ]; then
  185. return
  186. fi
  187. if [[ "$select_all_apps" != "add-all" ]]; then
  188. # ask for confirmation
  189. if [ $n -eq 1 ]; then
  190. dialog --title $"$installs" \
  191. --backtitle $"Freedombone" \
  192. --defaultno \
  193. --yesno $"\\nThis will install the $installs app\\n\\nProceed?" 9 40
  194. else
  195. dialog_height=$((15 + "$n"))
  196. dialog --title $"Add applications" \
  197. --backtitle $"Freedombone" \
  198. --defaultno \
  199. --yesno $"\\nYou have chosen to install $n apps\\n\\n $installs\\n\\nProceed?" $dialog_height 60
  200. fi
  201. sel=$?
  202. case $sel in
  203. 1) return;;
  204. 255) return;;
  205. esac
  206. fi
  207. clear
  208. # install the apps
  209. read_configuration
  210. install_apps interactive
  211. if [ ! "$APP_INSTALLED_SUCCESS" ]; then
  212. echo $'One or more apps failed to install'
  213. fi
  214. }
  215. if [[ $1 == "test"* ]]; then
  216. if ! ${PROJECT_NAME}-tests; then
  217. exit 2
  218. fi
  219. fi
  220. detect_installable_apps
  221. # if no applications were found
  222. if [[ ${#APPS_AVAILABLE[@]} == 0 ]]; then
  223. exit 1
  224. fi
  225. show_apps "$1"
  226. mark_unselected_apps_as_removed "$1"
  227. clear
  228. remove_apps_selected
  229. if [[ "$1" == "add-all" ]]; then
  230. install_apps_selected add-all
  231. else
  232. install_apps_selected
  233. fi
  234. android_update_apps
  235. exit 0