Files
dort.sh/dort.sh
2026-01-23 14:56:32 +01:00

252 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
## Environment variables
CONFIG_FILE=./dort.yaml
DOTS_DIR=$(pwd)
## Fail if running as sudo
if [[ "$EUID" -eq 0 ]]; then
echo "Don't run this script directly as root (you will be prompted for your sudo password once because installing things from the AUR is weird)"
exit 1
fi
## Keep sudo alive
echo "This script keeps sudo active in the background for yay/paru, be aware of that, don't randomly trust me and check the source code"
sudo -v || exit 1
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done 2>/dev/null &
## Updating Reops
sudo pacman -Syy
###############
## Functions ##
###############
install_packages() {
local pkg_str=$*
case $package_manager in
"pacman")
sudo pacman -S --noconfirm --needed $pkg_str || exit 1
;;
"paru")
paru -S --noconfirm --needed $pkg_str || exit 1
;;
"yay")
yay -S --noconfirm --needed $pkg_str || exit 1
;;
esac
}
check_and_install_dependencies() {
local deps=(yq gum stow git)
local all_installed=true
for program in "${deps[@]}"; do
if ! command -v "$program" &>/dev/null; then
all_installed=false
break
fi
done
if [[ "$all_installed" == false ]]; then
while true; do
read -r -p "Required dependencies (${deps[*]}) for dort are not installed, do you want to install them now? [y/n] " yn
case $yn in
[Yy]*)
break
;;
[Nn]*)
echo "Exiting dort..."
exit 1
;;
esac
done
fi
install_packages "${deps[@]}"
}
setup_aur_helper() {
AUR=$(yq -r '.dort.aur' "$CONFIG_FILE")
case $AUR in
"paru")
if ! command -v paru &>/dev/null; then
install_packages base-devel git
local temp_dir=$(mktemp -d)
git clone https://aur.archlinux.org/paru-bin.git "$temp_dir"/paru
cd "$temp_dir"/paru
makepkg -si --noconfirm
fi
cd "$DOTS_DIR"
package_manager=paru
;;
"yay")
if ! command -v yay &>/dev/null; then
install_packages base-devel git
local temp_dir=$(mktemp -d)
git clone https://aur.archlinux.org/yay-bin.git "$temp_dir"/yay
cd "$temp_dir"/yay
makepkg -si --noconfirm
fi
cd "$DOTS_DIR"
package_manager=yay
;;
*)
package_manager=pacman
;;
esac
}
load_categories_from_config() {
local dort_cats="$(yq -r '.configs | keys[]' "$CONFIG_FILE")"
readarray -d $'\n' -t categories <<<"$dort_cats"
}
load_current_profiles() {
local profiles_arr=()
local dort_cats="$(yq -r ".configs.$1 | keys[]" "$CONFIG_FILE")"
readarray -d $'\n' -t profiles_arr <<<"$dort_cats"
for profile in "${profiles_arr[@]}"; do
if [ "$profile" == "category" ]; then
continue
fi
local display_name="$(yq -r ".configs.$1.$profile.display" "$CONFIG_FILE")"
current_profiles[$display_name]="$profile"
done
}
select_profiles_in_category() {
mapfile -t selected < <(gum choose --header "$1" "$([[ "$2" == "false" ]] && printf "%s" '--no-limit')" "${!current_profiles[@]}")
for p in "${selected[@]}"; do
if [[ "$p" != "" && "$p" != "%" ]]; then
selected_profiles+=(".configs.$category.${current_profiles["$p"]}")
fi
done
unset selected
}
apply_profiles() {
local packages_to_install=()
local stow_dirs=()
local set_environs=()
local systemd_enable=()
local systemd_user_enable=()
local systemd_wants=()
local systemd_user_wants=()
local systemd_disable=()
local systemd_user_disable=()
local run_lines=()
for p in "${selected_profiles[@]}"; do
mapfile -t profile_pkgs < <(yq -r "$p.packages | values []" "$CONFIG_FILE")
mapfile -t profile_stows < <(yq -r "$p.stow | values []" "$CONFIG_FILE")
mapfile -t profile_env < <(yq -r "$p.environment | values []" "$CONFIG_FILE")
mapfile -t profile_sysd_enable < <(yq -r "$p.systemd.enable | values []" "$CONFIG_FILE")
mapfile -t profile_sysd_user_enable < <(yq -r "$p.systemd.user.enable | values []" "$CONFIG_FILE")
mapfile -t profile_sysd_wants < <(yq -r "$p.systemd.wants | values []" "$CONFIG_FILE")
mapfile -t profile_sysd_user_wants < <(yq -r "$p.systemd.user.wants | values []" "$CONFIG_FILE")
mapfile -t profile_sysd_disable < <(yq -r "$p.systemd.disable | values []" "$CONFIG_FILE")
mapfile -t profile_sysd_user_disable < <(yq -r "$p.systemd.user.disable | values []" "$CONFIG_FILE")
mapfile -t profile_run_lines < <(yq -r "$p.run | values[]" "$CONFIG_FILE")
packages_to_install+=("${profile_pkgs[@]}")
stow_dirs+=("${profile_stows[@]}")
set_environs+=("${profile_env[@]}")
systemd_enable+=("${profile_sysd_enable[@]}")
systemd_user_enable+=("${profile_sysd_user_enable[@]}")
systemd_wants+=("${profile_sysd_wants[@]}")
systemd_user_wants+=("${profile_sysd_user_wants[@]}")
systemd_disable+=("${profile_sysd_disable[@]}")
systemd_user_disable+=("${profile_sysd_user_disable[@]}")
run_lines+=("${profile_run_lines[@]}")
unset profile_pkgs
unset profile_stows
unset profile_env
unset profile_sysd_enable
unset profile_sysd_user_enable
unset profile_sysd_wants
unset profile_sysd_user_wants
unset profile_sysd_disable
unset profile_sysd_user_disable
unset profile_run_lines
done
install_packages "${packages_to_install[@]}"
stow "${stow_dirs[@]}" --adopt
git add .
git reset --hard HEAD
git submodule update --recursive --init
mkdir -p "$HOME"/.config/environment.d
echo "$(
IFS=$'\n'
echo "${set_environs[*]}"
)" >"$HOME"/.config/environment.d/90-dort.conf
if ((${#systemd_enable[@]} != 0)); then
sudo systemctl enable "${systemd_enable[@]}"
fi
if ((${#systemd_user_enable[@]} != 0)); then
systemctl enable --user "${systemd_user_enable[@]}"
fi
if ((${#systemd_disable[@]} != 0)); then
sudo systemctl disable "${systemd_disable[@]}"
fi
if ((${#systemd_user_disable[@]} != 0)); then
systemctl disable --user "${systemd_user_disable[@]}"
fi
if ((${#systemd_wants[@]} != 0)); then
sudo systemctl add-wants "${systemd_wants[@]}"
fi
if ((${#systemd_user_wants[@]} != 0)); then
systemctl add-wants --user "${systemd_user_wants[@]}"
fi
for run_cmd in "${run_lines[@]}"; do
eval "$run_cmd"
done
}
main() {
local package_manager="pacman"
local categories=()
local selected_profiles=()
check_and_install_dependencies
setup_aur_helper
load_categories_from_config
for category in "${categories[@]}"; do
declare -A current_profiles
load_current_profiles "$category"
select_profiles_in_category "Header text" "false"
unset current_profiles
done
apply_profiles
}
main