freedombone-app-vim 8.7KB

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