Makes install script more beautiful and modular

This commit is contained in:
2022-11-04 10:10:55 +01:00
parent 043dcb2cf5
commit 2a2bc51953

View File

@@ -2,6 +2,80 @@
set -e 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 \ files=".config/fish \
.config/git \ .config/git \
@@ -12,16 +86,44 @@ files=".config/fish \
.tmux.conf.local" .tmux.conf.local"
function this_script_root_dir() { function this_script_root_dir() {
dirname $(realpath ${BASH_SOURCE[0]}) dirname $(realpath ${BASH_SOURCE[0]})
} }
function run() { function run() {
# echo "$@" 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() { function load_gnome_terminal_profiles() {
echo "--- Installing GNOME terminal profile"
if which dconf 1>/dev/null if which dconf 1>/dev/null
then then
run dconf load /org/gnome/terminal/legacy/profiles:/ < $(this_script_root_dir)/gnome-terminal-profiles.dconf run dconf load /org/gnome/terminal/legacy/profiles:/ < $(this_script_root_dir)/gnome-terminal-profiles.dconf
@@ -29,64 +131,68 @@ function load_gnome_terminal_profiles() {
} }
function use_fish() { function use_fish() {
echo "--- Ensuring fish is current shell"
if ! which fish 1>/dev/null if ! which fish 1>/dev/null
then then
echo "Installing fish" echo "+++ Installing fish"
run sudo apt-get install -y fish run sudo apt-get install -y fish
else else
echo "Fish is installed" echo "+++ Fish is installed"
fi fi
fish=$(which fish) fish=$(which fish)
current_shell=$(grep $USER /etc/passwd | cut -d ":" -f 7) current_shell=$(grep $USER /etc/passwd | cut -d ":" -f 7)
if [ $current_shell != $fish ] if [ $current_shell != $fish ]
then then
echo "Changing shell to $fish (you will be prompted for passwd)" echo "+++ Changing shell to $fish (you will be prompted for passwd)"
run chsh -s $fish run chsh -s $fish
else else
echo "Default shell is $fish" echo "+++ Default shell is $fish"
fi fi
} }
function use_direnv() { function use_direnv() {
echo "--- Ensuring direnv is installed"
if ! which direnv 1>/dev/null if ! which direnv 1>/dev/null
then then
echo "Installing direnv" echo "++ Installing direnv"
run sudo apt-get install -y direnv run sudo apt-get install -y direnv
else else
echo "direnv is installed" echo "+++ direnv is installed"
fi fi
} }
function use_starship() { function use_starship() {
echo "--- Ensuring starship is installed"
if ! which starship 1>/dev/null if ! which starship 1>/dev/null
then then
echo "Installing starship" echo "+++ Installing starship"
run curl -sS https://starship.rs/install.sh | sh run curl -sS https://starship.rs/install.sh | sh
else else
echo "Starship is installed" echo "+++ Starship is installed"
fi fi
} }
function install_ubuntu_mono_nerd() { function install_ubuntu_mono_nerd() {
echo "--- Ensuring UbuntuMono Nerd font is present"
if ! fc-list | grep "UbuntuMono Nerd" 1>/dev/null if ! fc-list | grep "UbuntuMono Nerd" 1>/dev/null
then then
pushd /tmp pushd /tmp
run wget https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/UbuntuMono.zip run wget https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/UbuntuMono.zip
run unzip UbuntuMono.zip run unzip UbuntuMono.zip
run mkdir -p ~/.local/share/fonts run mkdir -p ~/.local/share/fonts
cp *.ttf ~/.local/share/fonts/ run cp *.ttf ~/.local/share/fonts/
run fc-cache -f run fc-cache -f
popd popd
else else
echo "UbuntuMono Nerd font is installed" echo "+++ UbuntuMono Nerd font is installed"
fi fi
if [ ! -e /usr/share/terminfo/x/xterm-direct ] if [ ! -e /usr/share/terminfo/x/xterm-direct ]
then then
run sudo apt-get install -y ncurses-term run sudo apt-get install -y ncurses-term
else else
echo "xterm-direct is in terminfo database" echo "+++ xterm-direct is in terminfo database"
fi fi
} }
@@ -94,12 +200,14 @@ function install_deps() {
use_fish use_fish
use_direnv use_direnv
use_starship use_starship
install_ubuntu_mono_nerd if $GNOME
then
install_ubuntu_mono_nerd
fi
} }
function install_files() {
function install_home() { echo "--- Ensuring symlinks to git file are present"
install_deps
for f in $files for f in $files
do do
run rm -Rf $HOME/$f run rm -Rf $HOME/$f
@@ -111,16 +219,45 @@ function install_home() {
run ln -sf $(this_script_root_dir)/$f $HOME/$f run ln -sf $(this_script_root_dir)/$f $HOME/$f
done done
run ln -sf $(this_script_root_dir)/.tmux/.tmux.conf $HOME/.tmux.conf run ln -sf $(this_script_root_dir)/.tmux/.tmux.conf $HOME/.tmux.conf
load_gnome_terminal_profiles
/usr/bin/fish -c 'omf install'
} }
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
}
pushd $(this_script_root_dir) 1>/dev/null function update_home() {
echo "Updating git.tuleu.science:atuleu/home.git" update_repos
git pull 1>/dev/null install_deps
git submodule init 1>/dev/null install_files
git submodule update 1>/dev/null update_oh_my_fish
popd 1>/dev/null if $GNOME
install_home then
echo "git.tuleu.science:atuleu/home.git updated and set" 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
}
update_home