freedombone-app-vim 8.6KB

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