dotfiles/setup_system.sh

322 lines
8.1 KiB
Bash
Raw Normal View History

2022-05-27 21:07:07 +02:00
#!/bin/bash
#
# Default variables
#
2022-11-10 15:43:45 +01:00
2022-05-27 21:07:07 +02:00
UPGRADE_PACKAGES=false
SCRIPT_NAME="$(basename -- "$0")"
SCRIPT_DIR="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit 1
pwd -P
)"
2022-11-10 15:43:45 +01:00
#
# Argument parsing and help
#
2022-05-27 21:07:07 +02:00
function usage() {
echo "Usage: $SCRIPT_NAME [OPTIONS]"
echo
echo "Options:"
echo " -h Display help."
echo " -u Upgrade existing packages."
exit 0
}
while getopts ":uh" arg; do
case $arg in
h) usage ;;
u) UPGRADE_PACKAGES=true ;;
:)
echo "$0: Must supply an argument to -$OPTARG." >&2
exit 1
;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
exit 2
;;
esac
done
2022-11-10 15:43:45 +01:00
#
# Distribution-agnostic functions
#
function _acpib() {
acpi -b | grep -Fv ' 0%'
}
function is_laptop() {
[[ $(_acpib | wc -l) -gt 0 ]] && return 0 || return 1
}
2022-05-27 21:07:07 +02:00
# Returns a comma-separated list of packages for one or more targets.
# The packages file may contain multiple rows with the same target name.
function get_packages() {
local packages pkg_file="$SCRIPT_DIR/.installer/packages"
for target in "$@"; do
while read -r row; do
for package in ${row//,/ }; do
echo "$package"
done
done <<<"$(grep "^${target:?}:" "$pkg_file" | cut -d':' -f2)"
done
}
2022-06-07 12:11:19 +02:00
function install_sddm_aerial_theme() {
local theme_dir="/usr/share/sddm/themes/aerial"
[ -d "$theme_dir" ] && return
echo "Installing SDDM Aerial theme."
git clone https://github.com/3ximus/aerial-sddm-theme.git /tmp/aerial
sudo mv /tmp/aerial "$theme_dir"
sudo chown -R root:root "$theme_dir"
}
2022-06-05 00:19:56 +02:00
function configure_sddm() {
local sddm_config_dir="/etc/sddm.conf.d"
local theme_dir="/usr/share/sddm/themes/aerial"
local theme_user_config="$theme_dir/theme.conf.user"
echo "Setting up '$sddm_config_dir'."
if ! [ -d "$sddm_config_dir" ]; then
sudo mkdir "$sddm_config_dir"
sudo chown root:root -R "$sddm_config_dir"
fi
{
echo "[Theme]"
echo "Current=aerial"
} | sudo tee "$sddm_config_dir/theme" >/dev/null
{
echo "[X11]"
echo "DisplayCommand=/usr/share/sddm/scripts/Xsetup"
} | sudo tee "$sddm_config_dir/xsetup" >/dev/null
echo "Setting up '$theme_dir'."
[ -f "$theme_user_config" ] && sudo mv "$theme_user_config" "$theme_user_config.disabled"
}
function configure_ufw() {
echo "Setting up UFW."
sudo systemctl enable ufw.service || return 1
sudo systemctl start ufw.service || return 2
sudo ufw enable || return 3
sudo ufw allow ssh || return 4
}
2022-11-10 15:43:45 +01:00
function install_picom() {
local tmp=/tmp/picom
echo "Installing Picom."
rm -Rf "$tmp"
git clone https://github.com/yshui/picom.git "$tmp" || return 1
cd "$tmp" || return 2
git submodule update --init --recursive || return 3
meson --buildtype=release . build || return 4
ninja -C build || return 5
sudo ninja -C build install || return 6
cd || return 7
rm -Rf "$tmp"
}
2022-11-10 16:27:35 +01:00
function setup_homebrew() {
local dir="$HOME/homebrew"
echo "Setting up Homebrew."
if [ ! -d "$dir" ]; then
echo "Homebrew is already installed."
mkdir "$dir" || return 1
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C "$dir" || return 2
fi
echo "Updating Homebrew."
eval "$("$dir/bin/brew" shellenv)"
brew update --force
chmod -R go-w "$(brew --prefix)/share/zsh"
}
function setup_brew_formulas() {
local targets=("brew-formulas-any") packages
brew update || return 1
for package in $(get_packages "${targets[@]}"); do
if brew list --full-name | grep -Eq "(^| )$package($| )" >/dev/null; then
if $UPGRADE_PACKAGES; then
echo "Package '$package' will be upgraded."
brew upgrade "$package" || return 2
else
echo "Package '$package' already exists."
continue
fi
else
echo "Package '$package' will be installed."
brew install "$package" || return 2
fi
done
}
2022-11-10 15:45:51 +01:00
function setup_krew_plugins() {
local targets=("krew-any") packages
if ! command -v kubectl-krew >/dev/null; then
echo "WARN: Krew not installed: kubectl-krew"
return 1
fi
for package in $(get_packages "${targets[@]}"); do
if kubectl-krew list | grep -q "^$package$" >/dev/null; then
if $UPGRADE_PACKAGES; then
echo "Package '$package' will be upgraded."
else
echo "Package '$package' already exists."
continue
fi
else
echo "Package '$package' will be installed."
fi
kubectl-krew install "$package" || return 2
done
}
2022-11-10 15:43:45 +01:00
#
# Setup for Arch-like systems
#
function prepare_arch() {
which pamac >/dev/null && return 0
sudo pacman -S pamac --noconfirm || return 1
}
function setup_arch_with_pamac() {
local targets=("pacman-any") packages
is_laptop && targets+=("pacman-laptop")
packages="$(get_packages "${targets[@]}")"
# shellcheck disable=SC2086
sudo pamac install $packages --no-confirm || return 1
}
function setup_arch_with_yay() {
local targets=("aur-any") packages
is_laptop && targets+=("aur-laptop")
for package in $(get_packages "${targets[@]}"); do
if pacman -Qs "^$package$" >/dev/null; then
if $UPGRADE_PACKAGES; then
echo "Package '$package' will be upgraded."
else
echo "Package '$package' already exists."
continue
fi
else
echo "Package '$package' will be installed."
fi
sudo pamac build "$package" --no-confirm || return 1
done
}
function setup_arch_with_pip(){
local targets=("pip-any") packages
packages="$(get_packages "${targets[@]}")"
# shellcheck disable=SC2086
sudo pip3 install $packages --no-input || return 1
}
2022-05-27 21:07:07 +02:00
function setup_arch() {
prepare_arch || return 1
setup_arch_with_pamac || return 2
setup_arch_with_yay || return 3
2022-06-30 20:44:01 +02:00
setup_arch_with_pip || return 4
configure_sddm || return 5
configure_ufw || return 6
2022-11-10 16:27:35 +01:00
setup_homebrew || return 7
setup_brew_formulas || return 8
setup_krew_plugins || return 9
2022-05-27 21:07:07 +02:00
}
2022-11-10 15:43:45 +01:00
#
# Setup for Debian-like systems
#
2022-06-07 12:11:19 +02:00
function setup_debian_repo_vscodium() {
echo "Setting up repository for VSCodium."
wget -qO - https://gitlab.com/paulcarroty/vscodium-deb-rpm-repo/raw/master/pub.gpg |
gpg --dearmor |
sudo dd of=/usr/share/keyrings/vscodium-archive-keyring.gpg
echo 'deb [ signed-by=/usr/share/keyrings/vscodium-archive-keyring.gpg ] https://download.vscodium.com/debs vscodium main' |
sudo tee /etc/apt/sources.list.d/vscodium.list
}
function setup_debian_with_apt() {
local targets packages
2022-06-07 12:11:19 +02:00
targets=("apt-base")
2022-06-07 12:11:19 +02:00
packages="$(get_packages "${targets[@]}")"
sudo apt-get update
# shellcheck disable=SC2086
sudo apt-get install $packages -y || return 1
targets=("apt-any")
is_laptop && targets+=("apt-laptop")
packages="$(get_packages "${targets[@]}")"
sudo apt-get update
# shellcheck disable=SC2086
sudo apt-get install $packages -y || return 2
2022-06-07 12:11:19 +02:00
}
2022-06-08 10:18:53 +02:00
function setup_debian_with_pip(){
2022-06-07 12:11:19 +02:00
local targets=("pip-any") packages
packages="$(get_packages "${targets[@]}")"
# shellcheck disable=SC2086
sudo pip3 install $packages --no-input || return 1
}
2022-06-08 10:18:53 +02:00
function setup_debian_with_git() {
2022-06-07 12:11:19 +02:00
install_picom || return 1
install_sddm_aerial_theme || return 2
}
function setup_debian() {
setup_debian_repo_vscodium || return 1
2022-06-07 12:11:19 +02:00
setup_debian_with_apt || return 2
2022-06-08 10:18:53 +02:00
setup_debian_with_pip || return 3
setup_debian_with_git || return 4
2022-06-07 12:11:19 +02:00
configure_sddm || return 5
configure_ufw || return 6
2022-11-10 16:27:35 +01:00
setup_homebrew || return 7
setup_brew_formulas || return 8
setup_krew_plugins || return 9
2022-06-07 12:11:19 +02:00
}
ID_LIKE="$(grep ID_LIKE= </etc/os-release | cut -d= -f2)"
echo "Setting up ${ID_LIKE^:?}-like OS."
2022-05-27 21:07:07 +02:00
if [[ "$ID_LIKE" == "arch" ]]; then
setup_arch
2022-06-07 12:11:19 +02:00
elif [[ "$ID_LIKE" == "debian" ]]; then
setup_debian
2022-05-27 21:07:07 +02:00
else
echo "ERROR: Unsupported system: ID_LIKE=$ID_LIKE"
exit 2
fi
echo "Setup finished."