dotfiles/debian.sh

253 lines
6.6 KiB
Bash
Executable file

#!/bin/sh
set -e
USERNAME="fernando"
DOTFILES_URL="https://git.schauenburg.me/fernando/dotfiles.git"
CUSTOM_APT_KEY_FILE="/etc/apt/keyrings/gitea-fernando.asc"
CUSTOM_APT_URL="https://git.schauenburg.me/api/packages/fernando/debian"
CUSTOM_APT_KEY="-----BEGIN PGP PUBLIC KEY BLOCK-----
xsBNBGX4KGoBCADiysyeZjwKmgHQakDpeutLq6glS1dvijb3Bdf9RGfOHwoQUPix
uPHv3b0mxfHHa9yRfySCSHNSZmCvMDztMpDKjfUKzNwj0ufd+b8vqZ9IIhD323/2
cCGpL76ARhGHaQrcfCyjU1bFnNmTRykjXvEPWiNLu9REYd18rPcwGOrITJYfevpj
NWdb+scpcMBIoob9NxkvN8V7tRvTsKnHfu6BqU22NobN2kRnqoqQ+IwDhchgrD8s
XDZCOiiL/cd3GAHyNsJ1Xu6M87Q/2YT1OHU26I2nWQuoxAHpO/wswD5lOhphUMUc
vMvzJNmwmGcHkhZdFZ/YdU0p/BNRQh+65bCNABEBAAHNEShEZWJpYW4gUmVnaXN0
cnkpwsBiBBMBCAAWBQJl+ChqCRANOnSUcFcewAIbAwIZAQAA3MUIAIF5jt1LUjQz
Cs1ucVEYOTsXVkbHQUyzDX7ZxWYa3052Xd/ipEa4fuedIB2Fu8jlUdEcoWx76CDl
08Xr0Jhi+9GYEjOnjtD1/waNt4KdqyQGxbZ9MtFicAweIAyghmrYHnoB+pWZ5G12
0CwQs1P+6yija4apeVFTx/RB/ByGXVx7IjQ7Fz5tAIc4EdRqK8VPGq8BYrCT9cct
mUFtMD9HaXvtcM9gAn6MVgyqLjql0VDg37knZGFZNWKfvNGHXyx6PsbcQtAIXE8N
ujiigmvEGY7tUqOYDB0Fn1nkL9loZfgWzzgluKoCZiRbdMk8DLVbvuoaCyDD1qXt
sw0tK5HujBLOwE0EZfgoagEIAL0tSBJlAuOF6VvkkHSI6tS9VZIE6TjnrYcnBv0q
hn0N6dA4wmwaioObVYqOdW4iIx/kuoS5kR+XwWTJ/wFf+RGMA1Rq13/HZKj1KgcG
TTGLn6tGAYU3NQ8sSSqV84IcKjvhvogIfUR9vZwmXE7FZTPLWfyVVAypFARoFLit
apVKj8wP+LdfC7UY9xsTXtDrD12iuPCNjeIZf7JIgGqQbi/PZV8kTL48/c94CK0d
vP/EXpEsTcChusy/7esh+71C44IECOw+BhJdWTF6SS3pGqUvwyExidh1XzHJOo6o
5xQRqLQin62wishsoM24BwA4LQtge36g6dTtsZ3FjrZLviEAEQEAAcLAXwQYAQgA
EwUCZfgoagkQDTp0lHBXHsACGwwAAIQ/CADFFk3+HLvN7fM1ylUfS6wa0OQfPtVf
9xF3FHoKu1ZTFVGDh53VuHqAEk0c7tBVrpfHHWWgb/bLZPfcIz96OKRtqmD2eIKl
JfHtHJLfv4Of9xa1YvBujgtCwc2h21DbMlNDQkrRZD44H1yIQOl+JR2XwjJyZ70/
l2gZy6iLTHT866UlPgYpCkxYSuf2ZH1BQ9E7BUIu4Jv2jeQzOzOiMekJ8HBdAypH
aN1GalvZk/HY3MUqPHklGF03OAn5YT2mimB6tgMpSoxwaaaIfXmUE4seqhA1wmpc
GwM1x9G/T3ZMkaV4wgEZ80RoEFRMtq2l2su/yzrYoeBkNI1h6oU9lkvd
=aOC5
-----END PGP PUBLIC KEY BLOCK-----
"
if [ -t 1 ]; then
sgr0="$(printf '\033[0m')"
red="$(printf '\033[31m')"
# green="$(printf '\033[32m')"
yellow="$(printf '\033[33m')"
blue="$(printf '\033[34m')"
# magenta="$(printf '\033[35m')"
# cyan="$(printf '\033[36m')"
else
sgr0=''
red=''
# green=''
yellow=''
blue=''
# magenta=''
# cyan=''
fi
usage() {
echo "Usage: $(basename "$0") [-h]"
echo ""
echo " -h print this help and exit"
}
error() {
printf "${red}ERROR:${sgr0} %s\n" "$1" >&2
exit 1
}
title() {
echo "${blue}=> ${1}${sgr0}"
}
skipped() {
echo "${yellow}SKIPPED:${sgr0} ${1}"
}
apt_custom_source() {
title "Set up custom APT source"
echo "${CUSTOM_APT_KEY}" >"${CUSTOM_APT_KEY_FILE}"
sed \
-i.original \
-e "1i deb [signed-by=${CUSTOM_APT_KEY_FILE}] ${CUSTOM_APT_URL} stable main\n\n" \
/etc/apt/sources.list
}
apt_install() {
title "Install APT packages"
apt update && apt install -y \
apt-file \
ascii \
broot \
build-essential \
ca-certificates \
cmake \
cmake-doc \
curl \
exuberant-ctags \
eza \
fd-find \
g++ \
gcc \
git \
git-crypt \
git-delta \
gnupg \
htop \
jq \
make \
man-db \
neovim \
nodejs \
pkg-config \
psmisc \
python3 \
python3-virtualenv \
ripgrep \
rsync \
shellcheck \
sshpass \
stow \
sudo \
tmux \
unzip \
zsh
apt-file update
}
stow_custom_packages() {
title "Stow custom packages under /usr/local/stow"
(
cd /usr/local/stow
for package in ./*; do
stow -v "${package##*/}"
done
)
}
grub_disable_timeout() {
title "Disable GRUB timeout"
sed -i.original -e 's/^GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /etc/default/grub
update-grub
}
ensure_usr_bin_fd() {
title "Make 'fd' available with the correct name"
fd_executable='/usr/local/bin/fdfind'
if [ -x "${fd_executable}" ]; then
ln -svf "${fd_executable}" /usr/local/bin/fd
else
skipped "${fd_executable} does not exist"
fi
}
ensure_usr_local_man_manN() {
title "Make sure we have directories for all man page sections"
mkdir -vp $(seq -f '/usr/local/man/man%.0f' 9)
}
user_setup() {
title "Setup user: $1"
if user_exists "$1"; then
echo "User $1 exists. Updating..."
user_update "$1"
else
echo "Creating user $1..."
user_new "$1"
fi
user_allow_sudo_nopasswd "$1"
}
user_exists() {
id -u "$1" >/dev/null 2>&1
}
user_new() {
empty_skel="$(mktemp -d)"
useradd \
-m ` # Create home directory.` \
-k "$empty_skel" ` # Copy files from this directory into the new home.` \
-U ` # Create a groups with the same name as the user.` \
-G staff ` # Other groups the new user will be a member of.` \
-s /bin/zsh ` # The new user's login shell. ` \
"$1" ` # The new user's name.` \
>/dev/null 2>&1 ` # Silently.` \
rmdir "$empty_skel"
}
# Add user $1 to the `staff` group...
# ...and change shell to `zsh` and get rid of bash files.
user_update() {
usermod -aG staff "$1"
chsh -s /bin/zsh "$1"
rm -vf "$(printf "/home/$1/%s " .bash_history .bash_logout .bashrc .profile)"
}
# Allow `sudo` without password for user $1.
user_allow_sudo_nopasswd() {
echo "$1 ALL=(ALL:ALL) NOPASSWD:ALL" >"/etc/sudoers.d/${1}_nopasswd"
}
deploy_dotfiles() {
title "Deploy dotfiles"
dotfiles_dir="/home/$USERNAME/.dotfiles"
if [ -d "${dotfiles_dir}" ]; then
skipped "${dotfiles_dir} exists"
else
su "$USERNAME" -c "git clone $DOTFILES_URL ${dotfiles_dir}"
(
cd "${dotfiles_dir}"
su "$USERNAME" -c "./install.sh -y"
)
fi
}
execute() {
apt_custom_source
apt_install
stow_custom_packages
grub_disable_timeout
ensure_usr_bin_fd
ensure_usr_local_man_manN
user_setup "$USERNAME"
deploy_dotfiles
}
main() {
while getopts 'hn' opt; do
case "$opt" in
h) # help
usage
exit 0
;;
*) # invalid argument
usage
exit 1
;;
esac
done
[ "$(id -u)" -eq 0 ] || error "This script must be run as root!"
execute
}
main "$@"