From 4cbb0ee74512504174702e7613c98163da53d9a1 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 12 Oct 2016 10:49:12 +0100 Subject: [PATCH] vim config remote backup and restore --- src/freedombone-app-blog | 2 +- src/freedombone-app-dlna | 2 +- src/freedombone-app-emacs | 2 +- src/freedombone-app-gnusocial | 2 +- src/freedombone-app-gogs | 2 +- src/freedombone-app-hubzilla | 2 +- src/freedombone-app-irc | 2 +- src/freedombone-app-mumble | 2 +- src/freedombone-app-rss | 2 +- src/freedombone-app-sip | 2 +- src/freedombone-app-syncthing | 2 +- src/freedombone-app-tox | 2 +- src/freedombone-app-vim | 239 ++++++++++++++++++++++++++++++++++ src/freedombone-app-wiki | 2 +- src/freedombone-app-xmpp | 2 +- 15 files changed, 253 insertions(+), 14 deletions(-) create mode 100755 src/freedombone-app-vim diff --git a/src/freedombone-app-blog b/src/freedombone-app-blog index e27332fd..d3d498b6 100755 --- a/src/freedombone-app-blog +++ b/src/freedombone-app-blog @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS="full writer" +VARIANTS="full full-vim writer" FULLBLOG_DOMAIN_NAME= FULLBLOG_CODE= diff --git a/src/freedombone-app-dlna b/src/freedombone-app-dlna index 81343780..16df1000 100755 --- a/src/freedombone-app-dlna +++ b/src/freedombone-app-dlna @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full media' +VARIANTS='full full-vim media' dlna_variables=(SYSTEM_TYPE USB_MOUNT diff --git a/src/freedombone-app-emacs b/src/freedombone-app-emacs index 93c39624..276f12df 100755 --- a/src/freedombone-app-emacs +++ b/src/freedombone-app-emacs @@ -55,7 +55,7 @@ function backup_local_emacs { cp /home/$USERNAME/.emacs /home/$USERNAME/.emacs.d/dotemacs fi function_check backup_directory_to_usb - backup_directory_to_usb /home/$USERNAME/.emacs.d config/$USERNAME + backup_directory_to_usb /home/$USERNAME/.emacs.d emacs/$USERNAME fi fi done diff --git a/src/freedombone-app-gnusocial b/src/freedombone-app-gnusocial index 1dc0462d..198e805c 100755 --- a/src/freedombone-app-gnusocial +++ b/src/freedombone-app-gnusocial @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full media' +VARIANTS='full full-vim media' MICROBLOG_DOMAIN_NAME= MICROBLOG_CODE= diff --git a/src/freedombone-app-gogs b/src/freedombone-app-gogs index 5ce2f639..cf47ba2d 100755 --- a/src/freedombone-app-gogs +++ b/src/freedombone-app-gogs @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full developer' +VARIANTS='full full-vim developer' GOGS_USERNAME='gogs' GOGS_VERSION='0.9.97' diff --git a/src/freedombone-app-hubzilla b/src/freedombone-app-hubzilla index 6d24d49e..9219ad7b 100755 --- a/src/freedombone-app-hubzilla +++ b/src/freedombone-app-hubzilla @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full social' +VARIANTS='full full-vim social' HUBZILLA_DOMAIN_NAME= HUBZILLA_CODE= diff --git a/src/freedombone-app-irc b/src/freedombone-app-irc index 66ee52d4..bc8ceb40 100755 --- a/src/freedombone-app-irc +++ b/src/freedombone-app-irc @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full chat' +VARIANTS='full full-vim chat' IRC_PORT=6697 IRC_ONION_PORT=6697 diff --git a/src/freedombone-app-mumble b/src/freedombone-app-mumble index 76658ee5..ca763a45 100755 --- a/src/freedombone-app-mumble +++ b/src/freedombone-app-mumble @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full chat' +VARIANTS='full full-vim chat' # Password used for mumble server MUMBLE_SERVER_PASSWORD= diff --git a/src/freedombone-app-rss b/src/freedombone-app-rss index cff28a2f..f83e23e4 100755 --- a/src/freedombone-app-rss +++ b/src/freedombone-app-rss @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full writer' +VARIANTS='full full-vim writer' RSS_READER_REPO="https://tt-rss.org/git/tt-rss.git" RSS_READER_COMMIT='48007463861d8db8b2b79c2f4f54e0564edb0ec0' diff --git a/src/freedombone-app-sip b/src/freedombone-app-sip index 8af613e6..97af130d 100755 --- a/src/freedombone-app-sip +++ b/src/freedombone-app-sip @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full chat' +VARIANTS='full full-vim chat' SIP_SERVER_PASSWORD= SIP_PORT=5060 diff --git a/src/freedombone-app-syncthing b/src/freedombone-app-syncthing index 373b904e..fba30ccd 100755 --- a/src/freedombone-app-syncthing +++ b/src/freedombone-app-syncthing @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full cloud' +VARIANTS='full full-vim cloud' SYNCTHING_ID= SYNCTHING_CONFIG_PATH=/root/.config/syncthing diff --git a/src/freedombone-app-tox b/src/freedombone-app-tox index a0050118..1b5dff71 100755 --- a/src/freedombone-app-tox +++ b/src/freedombone-app-tox @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full chat' +VARIANTS='full full-vim chat' TOX_PORT=33445 TOXCORE_REPO="https://github.com/bashrc/toxcore" diff --git a/src/freedombone-app-vim b/src/freedombone-app-vim new file mode 100755 index 00000000..3ddf23fa --- /dev/null +++ b/src/freedombone-app-vim @@ -0,0 +1,239 @@ +#!/bin/bash +# +# .---. . . +# | | | +# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. +# | | (.-' (.-' ( | ( )| | | | )( )| | (.-' +# ' ' --' --' -' - -' ' ' -' -' -' ' - --' +# +# Freedom in the Cloud +# +# Vim editor +# +# License +# ======= +# +# Copyright (C) 2016 Bob Mottram +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +VARIANTS='full-vim' + +vim_variables=(MY_USERNAME) +VIM_TEMP_DIR='.vimtemp' + +function change_password_vim { + echo -n '' +} + +function reconfigure_vim { + echo -n '' +} + +function upgrade_vim { + echo -n '' +} + +function backup_local_vim { + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $(is_valid_user "$USERNAME") == "1" ]]; then + echo $"Backing up Vim config for $USERNAME" + + # create a temporary directory + if [ ! -d /home/$USERNAME/$VIM_TEMP_DIR ]; then + mkdir /home/$USERNAME/$VIM_TEMP_DIR + fi + + # copy config files into the directory + if [ -f /home/$USERNAME/.vimrc ]; then + cp /home/$USERNAME/.vimrc /home/$USERNAME/$VIM_TEMP_DIR + chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR + fi + if [ -f /home/$USERNAME/.viminfo ]; then + cp /home/$USERNAME/.viminfo /home/$USERNAME/$VIM_TEMP_DIR + chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR + fi + + # backup the directory + function_check backup_directory_to_usb + backup_directory_to_usb /home/$USERNAME/$VIM_TEMP_DIR vim/$USERNAME + + # remove temporary directory + if [ -d /home/$USERNAME/$VIM_TEMP_DIR ]; then + rm -rf /home/$USERNAME/$VIM_TEMP_DIR + fi + fi + done +} + +function restore_local_vim { + temp_restore_dir=/root/tempvim + if [ -d $USB_MOUNT/backup/vim ]; then + for d in $USB_MOUNT/backup/vim/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $6}') + if [[ $(is_valid_user "$USERNAME") == "1" ]]; then + if [ ! -d /home/$USERNAME ]; then + ${PROJECT_NAME}-adduser $USERNAME + fi + echo $"Restoring Vim config for $USERNAME" + function_check restore_directory_from_usb + restore_directory_from_usb $temp_restore_dir vim/$USERNAME + cp -r $temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR /home/$USERNAME/ + if [ ! "$?" = "0" ]; then + rm -rf $temp_restore_dir + function_check set_user_permissions + set_user_permissions + function_check backup_unmount_drive + backup_unmount_drive + exit 664 + fi + cp /home/$USERNAME/$VIM_TEMP_DIR/* /home/$USERNAME + if [ -f /home/$USERNAME/.viminfo ]; then + chown $USERNAME:$USERNAME /home/$USERNAME/.viminfo + fi + if [ -f /home/$USERNAME/.vimrc ]; then + chown $USERNAME:$USERNAME /home/$USERNAME/.vimrc + fi + rm -rf /home/$USERNAME/$VIM_TEMP_DIR + rm -rf $temp_restore_dir + fi + done + fi +} + +function backup_remote_vim { + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $(is_valid_user "$USERNAME") == "1" ]]; then + echo $"Backing up Vim config for $USERNAME" + + # create a temporary directory + if [ ! -d /home/$USERNAME/$VIM_TEMP_DIR ]; then + mkdir /home/$USERNAME/$VIM_TEMP_DIR + fi + + # copy config files into the directory + if [ -f /home/$USERNAME/.vimrc ]; then + cp /home/$USERNAME/.vimrc /home/$USERNAME/$VIM_TEMP_DIR + chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR + fi + if [ -f /home/$USERNAME/.viminfo ]; then + cp /home/$USERNAME/.viminfo /home/$USERNAME/$VIM_TEMP_DIR + chown -R $USERNAME:$USERNAME /home/$USERNAME/$VIM_TEMP_DIR + fi + + # backup the directory + function_check backup_directory_to_friend + backup_directory_to_friend /home/$USERNAME/$VIM_TEMP_DIR vim/$USERNAME + + # remove temporary directory + if [ -d /home/$USERNAME/$VIM_TEMP_DIR ]; then + rm -rf /home/$USERNAME/$VIM_TEMP_DIR + fi + fi + done +} + +function restore_remote_vim { + temp_restore_dir=/root/tempvim + if [ -d $USB_MOUNT/backup/vim ]; then + for d in $USB_MOUNT/backup/vim/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $6}') + if [[ $(is_valid_user "$USERNAME") == "1" ]]; then + if [ ! -d /home/$USERNAME ]; then + ${PROJECT_NAME}-adduser $USERNAME + fi + echo $"Restoring Vim config for $USERNAME" + function_check restore_directory_from_friend + restore_directory_from_friend $temp_restore_dir vim/$USERNAME + cp -r $temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR /home/$USERNAME/ + if [ ! "$?" = "0" ]; then + rm -rf $temp_restore_dir + function_check set_user_permissions + set_user_permissions + function_check backup_unmount_drive + backup_unmount_drive + exit 664 + fi + cp /home/$USERNAME/$VIM_TEMP_DIR/* /home/$USERNAME + if [ -f /home/$USERNAME/.viminfo ]; then + chown $USERNAME:$USERNAME /home/$USERNAME/.viminfo + fi + if [ -f /home/$USERNAME/.vimrc ]; then + chown $USERNAME:$USERNAME /home/$USERNAME/.vimrc + fi + rm -rf /home/$USERNAME/$VIM_TEMP_DIR + rm -rf $temp_restore_dir + fi + done + fi +} + +function remove_vim { + apt-get -y remove --purge vim + update-alternatives --set editor /usr/bin/nano + sed -i '/install_vim/d' $COMPLETION_FILE + + # remove Vim as the mutt email editor + if [ -f /etc/Muttrc ]; then + if grep -q "set editor=" /etc/Muttrc; then + sed -i '/set editor=/d' /etc/Muttrc + fi + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $(is_valid_user "$USERNAME") == "1" ]]; then + if [ -f /home/$USERNAME/.muttrc ]; then + if grep -q "set editor=" /home/$USERNAME/.muttrc; then + sed -i '/set editor=/d' /home/$USERNAME/.muttrc + fi + fi + fi + done + fi +} + +function install_vim { + apt-get -y install vim + update-alternatives --set editor /usr/bin/vim + + # add a mutt entry to use Vim to compose emails + if [ -f /etc/Muttrc ]; then + if ! grep -q "set editor=" /etc/Muttrc; then + echo 'set editor="vim"' >> /etc/Muttrc + else + sed -i 's|set editor=.*|set editor="vim"|g' /etc/Muttrc + fi + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $(is_valid_user "$USERNAME") == "1" ]]; then + if [ -f /home/$USERNAME/.muttrc ]; then + if ! grep -q "set editor=" /home/$USERNAME/.muttrc; then + echo 'set editor="vim"' >> /home/$USERNAME/.muttrc + else + sed -i 's|set editor=.*|set editor="vim"|g' /home/$USERNAME/.muttrc + fi + fi + fi + done + fi + + APP_INSTALLED=1 +} + +function install_interactive_vim { + install_vim +} + +# NOTE: deliberately no exit 0 diff --git a/src/freedombone-app-wiki b/src/freedombone-app-wiki index 3ad6f570..55441cde 100755 --- a/src/freedombone-app-wiki +++ b/src/freedombone-app-wiki @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full writer' +VARIANTS='full full-vim writer' WIKI_DOMAIN_NAME= WIKI_ADMIN_PASSWORD= diff --git a/src/freedombone-app-xmpp b/src/freedombone-app-xmpp index 684a9dde..82f667ad 100755 --- a/src/freedombone-app-xmpp +++ b/src/freedombone-app-xmpp @@ -28,7 +28,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -VARIANTS='full chat' +VARIANTS='full full-vim chat' # Directory where XMPP settings are stored XMPP_DIRECTORY="/var/lib/prosody"