freedombone-addremove 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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@robotics.uk.to>
  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 selected 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 show_apps {
  70. select_all_apps=$1
  71. applist=""
  72. n=1
  73. app_index=0
  74. for a in "${APPS_AVAILABLE[@]}"
  75. do
  76. if [[ ${APPS_INSTALLED[$app_index]} == "0" && "$select_all_apps" != "add-all" ]]; then
  77. applist="$applist $n $a off"
  78. else
  79. if [[ "$a" == "vim" && "$select_all_apps" == "add-all" ]]; then
  80. applist="$applist $n $a off"
  81. else
  82. applist="$applist $n $a on"
  83. fi
  84. fi
  85. n=$[n+1]
  86. app_index=$[app_index+1]
  87. done
  88. choices=$(dialog --stdout --backtitle $"Freedombone" \
  89. --title $"Add/Remove Applications" \
  90. --checklist $'Choose:' \
  91. 27 40 20 $applist)
  92. if [ $? -eq 0 ]; then
  93. for choice in $choices
  94. do
  95. app_index=$[choice-1]
  96. APPS_CHOSEN[$app_index]="1"
  97. done
  98. else
  99. exit 0
  100. fi
  101. }
  102. function remove_apps_selected {
  103. # which apps need to be removed?
  104. removals=""
  105. app_index=0
  106. n=0
  107. for a in "${APPS_INSTALLED[@]}"
  108. do
  109. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  110. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  111. if [ ${n} -gt 0 ]; then
  112. removals="$removals ${APPS_AVAILABLE[$app_index]}"
  113. else
  114. removals="${APPS_AVAILABLE[$app_index]}"
  115. fi
  116. n=$[n+1]
  117. fi
  118. fi
  119. app_index=$[app_index+1]
  120. done
  121. # if no apps to be removed then don't do anything
  122. if [ ${n} -eq 0 ]; then
  123. return
  124. fi
  125. # ask for confirmation
  126. dialog --title $"Remove applications" \
  127. --backtitle $"Freedombone" \
  128. --defaultno \
  129. --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
  130. sel=$?
  131. case $sel in
  132. 1) return;;
  133. 255) return;;
  134. esac
  135. clear
  136. # remove the apps
  137. read_configuration
  138. remove_apps
  139. }
  140. function install_apps_selected {
  141. # which apps need to be installed?
  142. select_all_apps=$1
  143. installs=""
  144. app_index=0
  145. n=0
  146. for a in "${APPS_INSTALLED[@]}"
  147. do
  148. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  149. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  150. if [ ${n} -gt 0 ]; then
  151. installs="$installs ${APPS_AVAILABLE[$app_index]}"
  152. else
  153. installs="${APPS_AVAILABLE[$app_index]}"
  154. fi
  155. n=$[n+1]
  156. fi
  157. fi
  158. app_index=$[app_index+1]
  159. done
  160. # if no apps to be installed then don't do anything
  161. if [ ${n} -eq 0 ]; then
  162. return
  163. fi
  164. if [[ "$select_all_apps" != "add-all" ]]; then
  165. # ask for confirmation
  166. dialog --title $"Remove applications" \
  167. --backtitle $"Freedombone" \
  168. --defaultno \
  169. --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
  170. sel=$?
  171. case $sel in
  172. 1) return;;
  173. 255) return;;
  174. esac
  175. fi
  176. clear
  177. # install the apps
  178. read_configuration
  179. install_apps interactive
  180. if [ ! $APP_INSTALLED_SUCCESS ]; then
  181. echo $'One or more apps failed to install'
  182. exit 357223
  183. fi
  184. }
  185. if [[ $1 == "test"* ]]; then
  186. ${PROJECT_NAME}-tests
  187. if [ ! "$?" = "0" ]; then
  188. exit 2
  189. fi
  190. fi
  191. detect_installable_apps
  192. # if no applications were found
  193. if [[ ${#APPS_AVAILABLE[@]} == 0 ]]; then
  194. exit 1
  195. fi
  196. show_apps $1
  197. mark_unselected_apps_as_removed $1
  198. clear
  199. remove_apps_selected
  200. if [[ $1 == "add-all" ]]; then
  201. install_apps_selected add-all
  202. else
  203. install_apps_selected
  204. fi
  205. exit 0