freedombone-app-vim 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Vim editor
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. VARIANTS='full-vim'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=0
  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 logging_on_vim {
  36. echo -n ''
  37. }
  38. function logging_off_vim {
  39. echo -n ''
  40. }
  41. function reconfigure_vim {
  42. echo -n ''
  43. }
  44. function upgrade_vim {
  45. echo -n ''
  46. }
  47. function backup_local_vim {
  48. for d in /home/*/ ; do
  49. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  50. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  51. echo $"Backing up Vim config for $USERNAME"
  52. # create a temporary directory
  53. if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  54. mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
  55. fi
  56. # copy config files into the directory
  57. if [ -f "/home/$USERNAME/.vimrc" ]; then
  58. cp "/home/$USERNAME/.vimrc" "/home/$USERNAME/$VIM_TEMP_DIR"
  59. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
  60. fi
  61. if [ -f "/home/$USERNAME/.viminfo" ]; then
  62. cp "/home/$USERNAME/.viminfo" "/home/$USERNAME/$VIM_TEMP_DIR"
  63. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
  64. fi
  65. # backup the directory
  66. function_check backup_directory_to_usb
  67. backup_directory_to_usb "/home/$USERNAME/$VIM_TEMP_DIR" "vim/$USERNAME"
  68. # remove temporary directory
  69. if [ -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  70. rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
  71. fi
  72. fi
  73. done
  74. }
  75. function restore_local_vim {
  76. temp_restore_dir=/root/tempvim
  77. if [ -d "$USB_MOUNT/backup/vim" ]; then
  78. for d in $USB_MOUNT/backup/vim/*/ ; do
  79. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  80. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  81. if [ ! -d "/home/$USERNAME" ]; then
  82. "${PROJECT_NAME}-adduser" "$USERNAME"
  83. fi
  84. echo $"Restoring Vim config for $USERNAME"
  85. function_check restore_directory_from_usb
  86. restore_directory_from_usb "$temp_restore_dir" "vim/$USERNAME"
  87. if [ -d "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  88. cp -r "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" "/home/$USERNAME/"
  89. else
  90. if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  91. mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
  92. fi
  93. cp -r "$temp_restore_dir/"* "/home/$USERNAME/$VIM_TEMP_DIR/"
  94. fi
  95. # shellcheck disable=SC2181
  96. if [ ! "$?" = "0" ]; then
  97. rm -rf $temp_restore_dir
  98. function_check set_user_permissions
  99. set_user_permissions
  100. function_check backup_unmount_drive
  101. backup_unmount_drive
  102. exit 664
  103. fi
  104. cp "/home/$USERNAME/$VIM_TEMP_DIR/"* "/home/$USERNAME"
  105. if [ -f "/home/$USERNAME/.viminfo" ]; then
  106. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.viminfo"
  107. fi
  108. if [ -f "/home/$USERNAME/.vimrc" ]; then
  109. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.vimrc"
  110. fi
  111. rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
  112. rm -rf $temp_restore_dir
  113. fi
  114. done
  115. fi
  116. }
  117. function backup_remote_vim {
  118. for d in /home/*/ ; do
  119. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  120. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  121. echo $"Backing up Vim config for $USERNAME"
  122. # create a temporary directory
  123. if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  124. mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
  125. fi
  126. # copy config files into the directory
  127. if [ -f "/home/$USERNAME/.vimrc" ]; then
  128. cp "/home/$USERNAME/.vimrc" "/home/$USERNAME/$VIM_TEMP_DIR"
  129. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
  130. fi
  131. if [ -f "/home/$USERNAME/.viminfo" ]; then
  132. cp "/home/$USERNAME/.viminfo" "/home/$USERNAME/$VIM_TEMP_DIR"
  133. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
  134. fi
  135. # backup the directory
  136. function_check backup_directory_to_friend
  137. backup_directory_to_friend "/home/$USERNAME/$VIM_TEMP_DIR" "vim/$USERNAME"
  138. # remove temporary directory
  139. if [ -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  140. rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
  141. fi
  142. fi
  143. done
  144. }
  145. function restore_remote_vim {
  146. temp_restore_dir=/root/tempvim
  147. if [ -d "$USB_MOUNT/backup/vim" ]; then
  148. for d in $USB_MOUNT/backup/vim/*/ ; do
  149. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  150. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  151. if [ ! -d "/home/$USERNAME" ]; then
  152. "${PROJECT_NAME}-adduser" "$USERNAME"
  153. fi
  154. echo $"Restoring Vim config for $USERNAME"
  155. function_check restore_directory_from_friend
  156. restore_directory_from_friend "$temp_restore_dir vim/$USERNAME"
  157. if [ -d "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  158. cp -r "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" "/home/$USERNAME/"
  159. else
  160. if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
  161. mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
  162. fi
  163. cp -r "$temp_restore_dir/"* "/home/$USERNAME/$VIM_TEMP_DIR/"
  164. fi
  165. # shellcheck disable=SC2181
  166. if [ ! "$?" = "0" ]; then
  167. rm -rf $temp_restore_dir
  168. function_check set_user_permissions
  169. set_user_permissions
  170. function_check backup_unmount_drive
  171. backup_unmount_drive
  172. exit 664
  173. fi
  174. cp "/home/$USERNAME/$VIM_TEMP_DIR/"* "/home/$USERNAME"
  175. if [ -f "/home/$USERNAME/.viminfo" ]; then
  176. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.viminfo"
  177. fi
  178. if [ -f "/home/$USERNAME/.vimrc" ]; then
  179. chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.vimrc"
  180. fi
  181. rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
  182. rm -rf $temp_restore_dir
  183. fi
  184. done
  185. fi
  186. }
  187. function remove_vim {
  188. # Don't remove vim because vim-common is needed for showing ssh host keys
  189. # This may change with Debian Stretch
  190. # apt-get -yq remove --purge vim
  191. update-alternatives --set editor /usr/bin/nano
  192. sed -i '/install_vim/d' "$COMPLETION_FILE"
  193. # remove Vim as the mutt email editor
  194. if [ -f /etc/Muttrc ]; then
  195. if grep -q "set editor=" /etc/Muttrc; then
  196. sed -i '/set editor=/d' /etc/Muttrc
  197. fi
  198. for d in /home/*/ ; do
  199. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  200. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  201. if [ -f "/home/$USERNAME/.muttrc" ]; then
  202. if grep -q "set editor=" "/home/$USERNAME/.muttrc"; then
  203. sed -i '/set editor=/d' "/home/$USERNAME/.muttrc"
  204. fi
  205. fi
  206. fi
  207. done
  208. fi
  209. }
  210. function install_vim {
  211. apt-get -yq install vim
  212. update-alternatives --set editor /usr/bin/vim.tiny
  213. # add a mutt entry to use Vim to compose emails
  214. if [ -f /etc/Muttrc ]; then
  215. if ! grep -q "set editor=" /etc/Muttrc; then
  216. echo "set editor=\"$VIM_MUTT_EDITOR\"" >> /etc/Muttrc
  217. else
  218. sed -i "s|set editor=.*|set editor=\"$VIM_MUTT_EDITOR\"|g" /etc/Muttrc
  219. fi
  220. for d in /home/*/ ; do
  221. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  222. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  223. if [ -f "/home/$USERNAME/.muttrc" ]; then
  224. if ! grep -q "set editor=" "/home/$USERNAME/.muttrc"; then
  225. echo "set editor=\"$VIM_MUTT_EDITOR\"" >> "/home/$USERNAME/.muttrc"
  226. else
  227. sed -i "s|set editor=.*|set editor=\"$VIM_MUTT_EDITOR\"|g" "/home/$USERNAME/.muttrc"
  228. fi
  229. fi
  230. fi
  231. done
  232. fi
  233. APP_INSTALLED=1
  234. }
  235. function install_interactive_vim {
  236. install_vim
  237. }
  238. # NOTE: deliberately no exit 0