dotfiles/bootstrap
Ahmad Ansori Palembani a843e5236a
refactor(script/bootstrap): Allow pre-bootstrap to be ran
automatically by `bootstrap`

This will simplify the process of installing the dotfiles while still
giving users control
2024-07-20 09:00:06 +07:00

117 lines
3.3 KiB
Bash
Executable file

#!/bin/sh
[ $ZI_DOTFILES ] && {
. $ZI_DOTFILES/common
} || {
[ -f ./common ] || { >&2 echo "Bootstrap should be ran directly from dotfiles directory ('./bootstrap'), or run './pre-bootstrap' before running bootstrap!"; exit 1; }
. ./common
}
[ $ZI_DOTFILES ] || {
ZI_DOTFILES=$PWD
echo "'./pre-bootstrap' was never ran before. This script will store '$PWD' in \$ZI_DOTFILES as helper variable for automation scripts."
prompt "Run it now? [y/N] " || { >&2 echo "You need to run './pre-bootstrap' before running bootstrap!"; exit 1; }
echo "======include/dotfiles======"
./pre-bootstrap -
echo "============================"
prompt "Set '$PWD' as dotfiles directory? [y/N] " || exit 1
./pre-bootstrap - > $ZI_DOTFILES/.config/zsh/include/dotfiles
}
[ -d $ZI_DOTFILES ] || { >&2 echo "Invalid dotfiles path, please re-run pre-bootstrap!"; exit 1; }
. $ZI_DOTFILES/bootstrap-pkgs
# === Function and Variable ===
is_package_exists() {
[ $(command -v "$1" | wc -l) -gt 0 ] && echo 1 || echo 0
}
DISTRO="$(cat /etc/*-release | grep ^ID | head -n1 | cut -d '=' -f2 | cut -d '"' -f2)"
[ "$SUDO" = "" ] && { [ $(is_package_exists doas) = 1 ] && SUDO="doas" || SUDO="sudo"; }
[ "$PACMAN" = "" ] && {
case $DISTRO in
"arch" ) PACMAN="pacman" ;;
"ubuntu" ) PACMAN="apt" ;;
esac
}
AUR="${AUR:-paru}"
update_package_db() {
MSG_FAILED="Failed to update package database, skipping..."
case "$PACMAN" in
"pacman" )
$SUDO $PACMAN -Sy || echo $MSG_FAILED
;;
"apt" )
$SUDO $PACMAN update || echo $MSG_FAILED
;;
esac
}
install_package() {
[ "$1" = "" ] && { echo "No package candidate is specified, skipping..."; return; }
MSG_FAILED="Failed to install some packages, skipping..."
case "$PACMAN" in
"pacman" )
$SUDO $PACMAN -S --noconfirm "$@" || echo $MSG_FAILED
;;
"apt" )
$SUDO $PACMAN install "$@" || echo $MSG_FAILED
;;
esac
}
install_aur_package() {
# >> Paranoia checks
[ $DISTRO = "arch" ] || { echo "AUR only available in Arch, skipping..."; return; }
[ $(is_package_exists $AUR) = 1 ] || { echo "AUR helper '$AUR' is not installed, skipping..."; return; }
[ "$1" = "" ] && { echo "No AUR package candidate is specified, skipping..."; return; }
# <<
$AUR -S --noconfirm "$@"
}
# === Actual automation ===
echo "Linking x* configs..."
ln -si $ZI_DOTFILES/.config/xinitrc $HOME/.xinitrc
ln -si $ZI_DOTFILES/.config/xprofile $HOME/.xprofile
echo "Updating package database..."
update_package_db
echo "Installing common packages..."
install_package "${packages[@]}"
echo "Installing distro-specific packages..."
case $DISTRO in
"arch" )
install_package "${packages_arch[@]}"
echo "Installing AUR packages..."
install_aur_package "${packages_arch_aur[@]}"
;;
"ubuntu" ) install_package "${packages_ubuntu[@]}" ;;
esac
echo "Changing default shell to zsh..."
$SUDO chsh -s $(which zsh)
echo "Configuring zsh to use XDG Base Directory..."
$SUDO $ZI_DOTFILES/zsh-xdg-setup
echo "Installing zsh config..."
ln -si $ZI_DOTFILES/.config/zsh $HOME/.config/zsh
echo "Configuring doas..."
echo """permit persist :wheel
permit nopass root as root""" > /tmp/doas.conf.tmp
echo "=======/etc/doas.conf======="
cat /tmp/doas.conf.tmp
echo "============================"
$SUDO cp -i /tmp/doas.conf.tmp /etc/doas.conf
echo "============================"
echo "Bootstrap completed, please restart your shell!"