freedombone-addremove 6.7KB

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