2016-10-12 11:49:12 +02:00
|
|
|
#!/bin/bash
|
2018-04-08 14:30:21 +02:00
|
|
|
# _____ _ _
|
|
|
|
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
|
|
|
|
# | __| _| -_| -_| . | . | | . | . | | -_|
|
|
|
|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
|
2016-10-12 11:49:12 +02:00
|
|
|
#
|
2018-04-08 14:30:21 +02:00
|
|
|
# Freedom in the Cloud
|
2016-10-12 11:49:12 +02:00
|
|
|
#
|
|
|
|
# Vim editor
|
|
|
|
#
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
2018-02-21 20:32:13 +01:00
|
|
|
# Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
|
2016-10-12 11:49:12 +02:00
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
VARIANTS='full-vim'
|
|
|
|
|
2016-10-19 22:52:59 +02:00
|
|
|
IN_DEFAULT_INSTALL=0
|
2016-11-20 22:14:37 +01:00
|
|
|
SHOW_ON_ABOUT=0
|
2016-10-19 22:52:59 +02:00
|
|
|
|
2016-10-12 11:49:12 +02:00
|
|
|
VIM_TEMP_DIR='.vimtemp'
|
2016-10-12 12:01:15 +02:00
|
|
|
VIM_MUTT_EDITOR='vim \"+set nonumber\" \"+set insertmode\" \"+set spell\" +/^$/ +9'
|
|
|
|
|
|
|
|
vim_variables=(MY_USERNAME
|
|
|
|
VIM_MUTT_EDITOR)
|
2016-10-12 11:49:12 +02:00
|
|
|
|
2017-07-01 20:14:28 +02:00
|
|
|
function logging_on_vim {
|
|
|
|
echo -n ''
|
|
|
|
}
|
|
|
|
|
|
|
|
function logging_off_vim {
|
|
|
|
echo -n ''
|
|
|
|
}
|
|
|
|
|
2016-10-12 11:49:12 +02:00
|
|
|
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
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# copy config files into the directory
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.vimrc" ]; then
|
|
|
|
cp "/home/$USERNAME/.vimrc" "/home/$USERNAME/$VIM_TEMP_DIR"
|
|
|
|
chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.viminfo" ]; then
|
|
|
|
cp "/home/$USERNAME/.viminfo" "/home/$USERNAME/$VIM_TEMP_DIR"
|
|
|
|
chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# backup the directory
|
|
|
|
function_check backup_directory_to_usb
|
2018-03-01 12:45:51 +01:00
|
|
|
backup_directory_to_usb "/home/$USERNAME/$VIM_TEMP_DIR" "vim/$USERNAME"
|
2016-10-12 11:49:12 +02:00
|
|
|
|
|
|
|
# remove temporary directory
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore_local_vim {
|
|
|
|
temp_restore_dir=/root/tempvim
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -d "$USB_MOUNT/backup/vim" ]; then
|
2016-10-12 11:49:12 +02:00
|
|
|
for d in $USB_MOUNT/backup/vim/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
|
|
|
|
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ ! -d "/home/$USERNAME" ]; then
|
|
|
|
"${PROJECT_NAME}-adduser" "$USERNAME"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
echo $"Restoring Vim config for $USERNAME"
|
|
|
|
function_check restore_directory_from_usb
|
2018-03-01 12:45:51 +01:00
|
|
|
restore_directory_from_usb "$temp_restore_dir" "vim/$USERNAME"
|
|
|
|
if [ -d "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
cp -r "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" "/home/$USERNAME/"
|
2017-08-25 18:35:33 +02:00
|
|
|
else
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
|
2017-08-25 18:35:33 +02:00
|
|
|
fi
|
2018-03-03 12:49:17 +01:00
|
|
|
cp -r "$temp_restore_dir/"* "/home/$USERNAME/$VIM_TEMP_DIR/"
|
2017-08-25 18:35:33 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
# shellcheck disable=SC2181
|
2016-10-12 11:49:12 +02:00
|
|
|
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
|
2018-03-03 12:49:17 +01:00
|
|
|
cp "/home/$USERNAME/$VIM_TEMP_DIR/"* "/home/$USERNAME"
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.viminfo" ]; then
|
|
|
|
chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.viminfo"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.vimrc" ]; then
|
|
|
|
chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.vimrc"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
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
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# copy config files into the directory
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.vimrc" ]; then
|
|
|
|
cp "/home/$USERNAME/.vimrc" "/home/$USERNAME/$VIM_TEMP_DIR"
|
|
|
|
chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.viminfo" ]; then
|
|
|
|
cp "/home/$USERNAME/.viminfo" "/home/$USERNAME/$VIM_TEMP_DIR"
|
|
|
|
chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# backup the directory
|
|
|
|
function_check backup_directory_to_friend
|
2018-03-01 12:45:51 +01:00
|
|
|
backup_directory_to_friend "/home/$USERNAME/$VIM_TEMP_DIR" "vim/$USERNAME"
|
2016-10-12 11:49:12 +02:00
|
|
|
|
|
|
|
# remove temporary directory
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function restore_remote_vim {
|
|
|
|
temp_restore_dir=/root/tempvim
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -d "$USB_MOUNT/backup/vim" ]; then
|
2016-10-12 11:49:12 +02:00
|
|
|
for d in $USB_MOUNT/backup/vim/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
|
|
|
|
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ ! -d "/home/$USERNAME" ]; then
|
|
|
|
"${PROJECT_NAME}-adduser" "$USERNAME"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
echo $"Restoring Vim config for $USERNAME"
|
|
|
|
function_check restore_directory_from_friend
|
2018-03-01 12:45:51 +01:00
|
|
|
restore_directory_from_friend "$temp_restore_dir vim/$USERNAME"
|
|
|
|
if [ -d "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
cp -r "$temp_restore_dir/home/$USERNAME/$VIM_TEMP_DIR" "/home/$USERNAME/"
|
2017-08-25 18:35:33 +02:00
|
|
|
else
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ ! -d "/home/$USERNAME/$VIM_TEMP_DIR" ]; then
|
|
|
|
mkdir "/home/$USERNAME/$VIM_TEMP_DIR"
|
2017-08-25 18:35:33 +02:00
|
|
|
fi
|
2018-03-03 12:49:17 +01:00
|
|
|
cp -r "$temp_restore_dir/"* "/home/$USERNAME/$VIM_TEMP_DIR/"
|
2017-08-25 18:35:33 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
# shellcheck disable=SC2181
|
2016-10-12 11:49:12 +02:00
|
|
|
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
|
2018-03-03 12:49:17 +01:00
|
|
|
cp "/home/$USERNAME/$VIM_TEMP_DIR/"* "/home/$USERNAME"
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.viminfo" ]; then
|
|
|
|
chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.viminfo"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.vimrc" ]; then
|
|
|
|
chown "$USERNAME":"$USERNAME" "/home/$USERNAME/.vimrc"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
2018-03-01 12:45:51 +01:00
|
|
|
rm -rf "/home/${USERNAME:?}/$VIM_TEMP_DIR"
|
2016-10-12 11:49:12 +02:00
|
|
|
rm -rf $temp_restore_dir
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_vim {
|
2016-11-23 00:52:33 +01:00
|
|
|
# Don't remove vim because vim-common is needed for showing ssh host keys
|
|
|
|
# This may change with Debian Stretch
|
|
|
|
# apt-get -yq remove --purge vim
|
2016-10-12 11:49:12 +02:00
|
|
|
update-alternatives --set editor /usr/bin/nano
|
2018-03-01 12:45:51 +01:00
|
|
|
sed -i '/install_vim/d' "$COMPLETION_FILE"
|
2016-10-12 11:49:12 +02:00
|
|
|
|
|
|
|
# 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
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.muttrc" ]; then
|
|
|
|
if grep -q "set editor=" "/home/$USERNAME/.muttrc"; then
|
|
|
|
sed -i '/set editor=/d' "/home/$USERNAME/.muttrc"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_vim {
|
2016-10-23 20:38:14 +02:00
|
|
|
apt-get -yq install vim
|
2018-02-02 14:40:06 +01:00
|
|
|
update-alternatives --set editor /usr/bin/vim.tiny
|
2016-10-12 11:49:12 +02:00
|
|
|
|
|
|
|
# add a mutt entry to use Vim to compose emails
|
|
|
|
if [ -f /etc/Muttrc ]; then
|
|
|
|
if ! grep -q "set editor=" /etc/Muttrc; then
|
2016-10-12 12:01:15 +02:00
|
|
|
echo "set editor=\"$VIM_MUTT_EDITOR\"" >> /etc/Muttrc
|
2016-10-12 11:49:12 +02:00
|
|
|
else
|
2016-10-12 12:01:15 +02:00
|
|
|
sed -i "s|set editor=.*|set editor=\"$VIM_MUTT_EDITOR\"|g" /etc/Muttrc
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
for d in /home/*/ ; do
|
|
|
|
USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
|
|
|
|
if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
|
2018-03-01 12:45:51 +01:00
|
|
|
if [ -f "/home/$USERNAME/.muttrc" ]; then
|
|
|
|
if ! grep -q "set editor=" "/home/$USERNAME/.muttrc"; then
|
|
|
|
echo "set editor=\"$VIM_MUTT_EDITOR\"" >> "/home/$USERNAME/.muttrc"
|
2016-10-12 11:49:12 +02:00
|
|
|
else
|
2018-03-01 12:45:51 +01:00
|
|
|
sed -i "s|set editor=.*|set editor=\"$VIM_MUTT_EDITOR\"|g" "/home/$USERNAME/.muttrc"
|
2016-10-12 11:49:12 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
APP_INSTALLED=1
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_interactive_vim {
|
|
|
|
install_vim
|
|
|
|
}
|
|
|
|
|
|
|
|
# NOTE: deliberately no exit 0
|