Files
home/update.sh
Alexandre Tuleu 0a156bd711 Updates one command-liner
Apparently PAM does not like nested scripts.
2022-11-04 10:43:08 +01:00

272 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
set -e
function print_help() {
echo "usage .home/update.sh [-g/--gnome] [-h/--help] [-v/--berbose] [-d/--dry]
Installs and updates shared configuration on a system. Options are:
-h/--help : print this help
-g/--gnome : system is intended to run in a GNOME environement, and
will setup gnome-terminal accordingly. Do not
activate on headless system.
-v/--verbose : prints command before running them
-n/--dry : dry run, do not execute commands. Implies --verbose.
"
}
function exit_error() {
print_help
exit 1
}
GNOME=false
VERBOSE=false
DRY=false
function set_gnome_on() {
GNOME=true
}
function set_verbose_on() {
VERBOSE=true
}
function set_dry_on() {
DRY=true
VERBOSE=true
}
while getopts "vnhg-:" optchar
do
case "${optchar}" in
-)
case "${OPTARG}" in
gnome)
set_gnome_on
;;
verbose)
set_verbose_on
;;
dry)
set_dry_on
;;
*)
echo "invalid argument --${OPTARG}"
exit_error
;;
esac;;
h)
print_help
exit 0
;;
g)
set_gnome_on
;;
n)
set_dry_on
;;
v)
set_verbose_on
;;
*)
exit_error
;;
esac
done
files=".config/fish \
.config/git \
.config/omf \
.config/starship.toml \
.local/share/omf \
.emacs \
.tmux.conf.local"
function this_script_root_dir() {
dirname $(realpath ${BASH_SOURCE[0]})
}
function run() {
if $VERBOSE
then
echo "[$@]"
fi
if ! $DRY
then
$@
fi
}
function run_git() {
if $VERBOSE
then
echo "[git $@] in $(this_script_root_dir)"
fi
if ! $DRY
then
pushd $(this_script_root_dir) 1>/dev/null
if $VERBOSE
then
git $@
else
git $@ 1>/dev/null
fi
popd 1>/dev/null
fi
}
function load_gnome_terminal_profiles() {
echo "--- Installing GNOME terminal profile"
if which dconf 1>/dev/null
then
run dconf load /org/gnome/terminal/legacy/profiles:/ < $(this_script_root_dir)/gnome-terminal-profiles.dconf
fi
}
function use_fish() {
echo "--- Ensuring fish is current shell"
if ! which fish 1>/dev/null
then
echo "+++ Installing fish"
run sudo apt-get install -y fish
else
echo "+++ Fish is installed"
fi
fish=$(which fish)
current_shell=$(grep $USER /etc/passwd | cut -d ":" -f 7)
if [ $current_shell != $fish ]
then
echo "+++ Changing shell to $fish (you will be prompted for passwd)"
run chsh -s $fish
else
echo "+++ Default shell is $fish"
fi
}
function use_direnv() {
echo "--- Ensuring direnv is installed"
if ! which direnv 1>/dev/null
then
echo "++ Installing direnv"
run sudo apt-get install -y direnv
else
echo "+++ direnv is installed"
fi
}
function use_starship() {
echo "--- Ensuring starship is installed"
if ! which starship 1>/dev/null
then
echo "+++ Installing starship"
run curl -sS https://starship.rs/install.sh | sh
else
echo "+++ Starship is installed"
fi
}
function install_ubuntu_mono_nerd() {
echo "--- Ensuring UbuntuMono Nerd font is present"
if ! fc-list | grep "UbuntuMono Nerd" 1>/dev/null
then
pushd /tmp
run wget https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/UbuntuMono.zip
run unzip UbuntuMono.zip
run mkdir -p ~/.local/share/fonts
run cp *.ttf ~/.local/share/fonts/
run fc-cache -f
popd
else
echo "+++ UbuntuMono Nerd font is installed"
fi
if [ ! -e /usr/share/terminfo/x/xterm-direct ]
then
run sudo apt-get install -y ncurses-term
else
echo "+++ xterm-direct is in terminfo database"
fi
}
function install_deps() {
use_fish
use_direnv
use_starship
if $GNOME
then
install_ubuntu_mono_nerd
fi
}
function install_files() {
echo "--- Ensuring symlinks to git file are present"
for f in $files
do
run rm -Rf $HOME/$f
dir=$(dirname $f)
if [ ! "$dir" = "." ]
then
run mkdir -p $HOME/$dir
fi
run ln -sf $(this_script_root_dir)/$f $HOME/$f
done
run ln -sf $(this_script_root_dir)/.tmux/.tmux.conf $HOME/.tmux.conf
}
function update_oh_my_fish() {
echo "--- Updating Oh My Fish"
if $VERBOSE
then
echo "[/usr/bin/fish -c 'omf install']"
fi
if ! $DRY
then
if $VERBOSE
then
/usr/bin/fish -c 'omf install'
else
/usr/bin/fish -c 'omf install' 1>/dev/null
fi
fi
}
function update_home() {
update_repos
install_deps
install_files
update_oh_my_fish
if $GNOME
then
load_gnome_terminal_profiles
fi
if ! $DRY
then
echo "--- git.tuleu.science:atuleu/home.git updated and set"
fi
}
function update_repos() {
echo "--- Updating git.tuleu.science:atuleu/home.git"
run_git pull
run_git submodule init
run_git submodule update
}
if [ -z $BASH_SOURCE ] && [ ! -d "$HOME/.home" ]
then
echo "--- cloning git.tuleu.science:atuleu/home.git in $HOME/.home"
pushd $HOME 1>/dev/null
run_git clone https://git.tuleu.science/atuleu/home.git .home
popd 1>/dev/null
else
update_home
fi