# Headings
SECTION() { printf "\n[1m[4m$1[0m\n"; }
highlight() { printf "[1m[3m$1[0m\n"; }
subhighlight() { printf "[3m$1[0m\n"; }

source "$HOME/.zshenv"

SECTION "System"

highlight "Sudoers defaults"
echo 'Defaults	editor=/usr/bin/nvim
Defaults        env_keep="EDITOR"' |
	sudo tee /etc/sudoers.d/editor
echo 'Defaults	timestamp_timeout=120
Defaults	passwd_timeout=30' |
	sudo tee /etc/sudoers.d/timeout
subhighlight "password-free reboot"
echo "$USER ALL = NOPASSWD: /usr/bin/halt, /usr/bin/shutdown, /usr/bin/reboot, /usr/bin/poweroff, /bin/reboot, /bin/shutdown. /usr/bin/umount" |
	sudo tee /etc/sudoers.d/shutdown

highlight "Set default user in tty1,2,3"
sudo mkdir -p /etc/systemd/system/getty@tty{1,2,3}.service.d
echo "[Service]
ExecStart=
ExecStart=-/usr/bin/agetty --skip-login --login-options $USER %I" |
	sudo tee /etc/systemd/system/getty@tty1.service.d/override.conf /etc/systemd/system/getty@tty2.service.d/override.conf /etc/systemd/system/getty@tty3.service.d/override.conf

highlight "Sysyadm setup"
test -d "/etc/yadm" || sy clone

SECTION "Software"

link() {
	test -e "$2" -o -h "$2" || $($HOME/.local/bin/scripts/checkaccess -w "$2" || echo sudo) ln -sv "$@"
}

mkdir -p .config/audacity
find $HOME -maxdepth 1 -type l -exec file {} + |
	grep '.local/share' | grep broken |
	sed 's/.* //' | xargs -r mkdir -p

link python /usr/bin/python3
link fdfind /usr/bin/fd
link batcat /usr/bin/bat

highlight "SSH Permissions"
chmod og= "$HOME/.ssh" -R

highlight "Cron"
echo 'cron.*				/var/log/cron.log' | sudo tee /etc/rsyslog.d/60-cron.conf
hasService=$(which service 2>/dev/null)
test "$hasService" && sudo service rsyslog restart || sudo systemctl restart rsyslog
test "$hasService" && sudo service cron reload || sudo systemctl reload cronie

highlight "Zsh"
test -n "$ZSH" || export ZSH="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/oh-my-zsh"
mkdir -p "$(dirname $ZSH)"
oldzsh="$XDG_STATE_HOME/zsh/oh-my-zsh" && test ! -d "$ZSH" -a -d "$oldzsh" && mv -vn $oldzsh $ZSH
cloneshallow() {
	url=https://github.com/$2/$1.git
	dir="${3:-$ZSH/plugins/$1}"
	mkdir -p "$(dirname $dir)"
	if test -d "$dir"
	then git -C "$dir" remote set-url origin $url
	else git clone --depth=1 $url "$dir"
	fi
}
cloneshallow oh-my-zsh                  robbyrussell       "$ZSH"
cloneshallow powerlevel10k              romkatv            "$ZSH/themes/powerlevel10k"
cloneshallow fast-syntax-highlighting   zdharma-continuum
cloneshallow zsh-autosuggestions        zsh-users
cloneshallow zsh-vim-mode               softmoth
case "$(grep "^$USER" /etc/passwd | sed 's/.*://')" in (*zsh);; (*) chsh -s $(which zsh);; esac

highlight "Arch"
if test -f /etc/pacman.conf; then
	subhighlight "Pacman"
	sudo sed -i 's/#Color/Color/' /etc/pacman.conf
	sudo sed -i "$(grep -n "\[multilib\]" /etc/pacman.conf | cut -d':' -f1),+1 s/# *//" /etc/pacman.conf
	subhighlight "Reflector"
	echo "--save /etc/pacman.d/mirrorlist
	--protocol https
	--country Germany
	--latest 50
	--score 10
	--sort rate" | sudo tee /etc/xdg/reflector/reflector.conf
	sudo systemctl start reflector &
	sudo systemctl enable reflector.timer
fi

test "$DISPLAY" || exit 0
highlight "Desktop Applications"

subhighlight "Application symlinks"
link .bash_aliases "$HOME/.bashrc"
link "$HOME/.ssh/config" "$XDG_CONFIG_HOME/ssh"

link "$XDG_STATE_HOME/gradle/gradle.properties" "$XDG_CONFIG_HOME/gradle.properties"
link "$XDG_DATA_HOME/applications/signal-tray.desktop" "$XDG_CONFIG_HOME/autostart/"
link "/usr/share/applications/stretchly.desktop" "$XDG_CONFIG_HOME/autostart/"
mx="$HOME/.mixxx"
if ! test -d "$mx"
then mkdir "$mx" && ln -sv -t "$mx" ${XDG_DATA_HOME:-$HOME/.local/share}/mixxx/* ${XDG_CONFIG_HOME:-$HOME/.config}/mixxx/*
fi

subhighlight "Block internet at night & on weekdays in the morning to force focus"
walli 22 --save

subhighlight "Setup kakoune plugin loader"
cloneshallow plug.kak robertmeta "${XDG_CONFIG_HOME:-$HOME/.config}/kak/plugins/plug.kak"
