freedombone-app-vim 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Vim editor
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 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. VARIANTS='full-vim'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=0
  33. VIM_TEMP_DIR='.vimtemp'
  34. VIM_MUTT_EDITOR='vim \"+set nonumber\" \"+set insertmode\" \"+set spell\" +/^$/ +9'
  35. vim_variables=(MY_USERNAME
  36. VIM_MUTT_EDITOR)
  37. function logging_on_vim {
  38. echo -n ''
  39. }
  40. function logging_off_vim {
  41. echo -n ''
  42. }
  43. function reconfigure_vim {
  44. echo -n ''
  45. }
  46. function upgrade_vim {
  47. echo -n ''
  48. }
  49. function backup_local_vim {
  50. for d in /home/*/ ; do
  51. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  52. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  53. echo $"Backing up Vim config for $USERNAME"
  54. # create a temporary directory
  55. if [ ! -d /home/$USERNAME/$VIM_TEMP_DIR ]; then
  56. mkdir /home/$USERNAME/$VIM_TEMP_DIR
  57. fi
  58. # copy config files into the directory
  59. if [ -f /home/$USERNAME/.vimrc ]; then
  60. cp /home/$USERNAME/.vimrc /home/$USERNAME/$VIM_TEMP_DIR
  61. chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR
  62. fi
  63. if [ -f /home/$USERNAME/.viminfo ]; then
  64. cp /home/$USERNAME/.viminfo /home/$USERNAME/$VIM_TEMP_DIR
  65. chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR
  66. fi
  67. # backup the directory
  68. function_check backup_directory_to_usb
  69. backup_directory_to_usb /home/$USERNAME/$VIM_TEMP_DIR vim/$USERNAME
  70. # remove temporary directory
  71. if [ -d /home/$USERNAME/$VIM_TEMP_DIR ]; then
  72. rm -rf /home/$USERNAME/$VIM_TEMP_DIR
  73. fi
  74. fi
  75. done
  76. }
  77. function restore_local_vim {
  78. temp_restore_dir=/root/tempvim
  79. if [ -d $USB_MOUNT/backup/vim ]; then
  80. for d in $USB_MOUNT/backup/vim/*/ ; do
  81. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  82. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  83. if [ ! -d /home/$USERNAME ]; then
  84. ${PROJECT_NAME}-adduser $USERNAME
  85. fi
  86. echo $"Restoring Vim config for $USERNAME"
  87. function_check restore_directory_from_usb
  88. restore_directory_from_usb $temp_restore_dir vim/$USERNAME
  89. cp -r $temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR /home/$USERNAME/
  90. if [ ! "$?" = "0" ]; then
  91. rm -rf $temp_restore_dir
  92. function_check set_user_permissions
  93. set_user_permissions
  94. function_check backup_unmount_drive
  95. backup_unmount_drive
  96. exit 664
  97. fi
  98. cp /home/$USERNAME/$VIM_TEMP_DIR/* /home/$USERNAME
  99. if [ -f /home/$USERNAME/.viminfo ]; then
  100. chown $USERNAME:$USERNAME /home/$USERNAME/.viminfo
  101. fi
  102. if [ -f /home/$USERNAME/.vimrc ]; then
  103. chown $USERNAME:$USERNAME /home/$USERNAME/.vimrc
  104. fi
  105. rm -rf /home/$USERNAME/$VIM_TEMP_DIR
  106. rm -rf $temp_restore_dir
  107. fi
  108. done
  109. fi
  110. }
  111. function backup_remote_vim {
  112. for d in /home/*/ ; do
  113. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  114. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  115. echo $"Backing up Vim config for $USERNAME"
  116. # create a temporary directory
  117. if [ ! -d /home/$USERNAME/$VIM_TEMP_DIR ]; then
  118. mkdir /home/$USERNAME/$VIM_TEMP_DIR
  119. fi
  120. # copy config files into the directory
  121. if [ -f /home/$USERNAME/.vimrc ]; then
  122. cp /home/$USERNAME/.vimrc /home/$USERNAME/$VIM_TEMP_DIR
  123. chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR
  124. fi
  125. if [ -f /home/$USERNAME/.viminfo ]; then
  126. cp /home/$USERNAME/.viminfo /home/$USERNAME/$VIM_TEMP_DIR
  127. chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR
  128. fi
  129. # backup the directory
  130. function_check backup_directory_to_friend
  131. backup_directory_to_friend /home/$USERNAME/$VIM_TEMP_DIR vim/$USERNAME
  132. # remove temporary directory
  133. if [ -d /home/$USERNAME/$VIM_TEMP_DIR ]; then
  134. rm -rf /home/$USERNAME/$VIM_TEMP_DIR
  135. fi
  136. fi
  137. done
  138. }
  139. function restore_remote_vim {
  140. temp_restore_dir=/root/tempvim
  141. if [ -d $USB_MOUNT/backup/vim ]; then
  142. for d in $USB_MOUNT/backup/vim/*/ ; do
  143. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  144. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  145. if [ ! -d /home/$USERNAME ]; then
  146. ${PROJECT_NAME}-adduser $USERNAME
  147. fi
  148. echo $"Restoring Vim config for $USERNAME"
  149. function_check restore_directory_from_friend
  150. restore_directory_from_friend $temp_restore_dir vim/$USERNAME
  151. cp -r $temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR /home/$USERNAME/
  152. if [ ! "$?" = "0" ]; then
  153. rm -rf $temp_restore_dir
  154. function_check set_user_permissions
  155. set_user_permissions
  156. function_check backup_unmount_drive
  157. backup_unmount_drive
  158. exit 664
  159. fi
  160. cp /home/$USERNAME/$VIM_TEMP_DIR/* /home/$USERNAME
  161. if [ -f /home/$USERNAME/.viminfo ]; then
  162. chown $USERNAME:$USERNAME /home/$USERNAME/.viminfo
  163. fi
  164. if [ -f /home/$USERNAME/.vimrc ]; then
  165. chown $USERNAME:$USERNAME /home/$USERNAME/.vimrc
  166. fi
  167. rm -rf /home/$USERNAME/$VIM_TEMP_DIR
  168. rm -rf $temp_restore_dir
  169. fi
  170. done
  171. fi
  172. }
  173. function remove_vim {
  174. # Don't remove vim because vim-common is needed for showing ssh host keys
  175. # This may change with Debian Stretch
  176. # apt-get -yq remove --purge vim
  177. update-alternatives --set editor /usr/bin/nano
  178. sed -i '/install_vim/d' $COMPLETION_FILE
  179. # remove Vim as the mutt email editor
  180. if [ -f /etc/Muttrc ]; then
  181. if grep -q "set editor=" /etc/Muttrc; then
  182. sed -i '/set editor=/d' /etc/Muttrc
  183. fi
  184. for d in /home/*/ ; do
  185. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  186. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  187. if [ -f /home/$USERNAME/.muttrc ]; then
  188. if grep -q "set editor=" /home/$USERNAME/.muttrc; then
  189. sed -i '/set editor=/d' /home/$USERNAME/.muttrc
  190. fi
  191. fi
  192. fi
  193. done
  194. fi
  195. }
  196. function install_vim {
  197. apt-get -yq install vim
  198. update-alternatives --set editor /usr/bin/vim
  199. # add a mutt entry to use Vim to compose emails
  200. if [ -f /etc/Muttrc ]; then
  201. if ! grep -q "set editor=" /etc/Muttrc; then
  202. echo "set editor=\"$VIM_MUTT_EDITOR\"" >> /etc/Muttrc
  203. else
  204. sed -i "s|set editor=.*|set editor=\"$VIM_MUTT_EDITOR\"|g" /etc/Muttrc
  205. fi
  206. for d in /home/*/ ; do
  207. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  208. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  209. if [ -f /home/$USERNAME/.muttrc ]; then
  210. if ! grep -q "set editor=" /home/$USERNAME/.muttrc; then
  211. echo "set editor=\"$VIM_MUTT_EDITOR\"" >> /home/$USERNAME/.muttrc
  212. else
  213. sed -i "s|set editor=.*|set editor=\"$VIM_MUTT_EDITOR\"|g" /home/$USERNAME/.muttrc
  214. fi
  215. fi
  216. fi
  217. done
  218. fi
  219. APP_INSTALLED=1
  220. }
  221. function install_interactive_vim {
  222. install_vim
  223. }
  224. # NOTE: deliberately no exit 0