#!/bin/sh set -eu # See `parse_args()` for enabling dry run. dry_run="no" is_dry_run() { test "${dry_run}" = "yes" } setup_colors() { if [ -t 1 ]; then sgr0="$(printf '\033[0m')" bold="$(printf '\033[1m')" red="$(printf '\033[31m')" green="$(printf '\033[32m')" yellow="$(printf '\033[33m')" blue="$(printf '\033[34m')" else sgr0='' bold='' red='' green='' yellow='' blue='' fi } setup_commands() { dry='' is_dry_run && dry="echo " APT="${dry}apt" APT_FILE="${dry}apt-file" CD="${dry}cd" CHSH="${dry}chsh" LN="${dry}ln" MKDIR="${dry}mkdir" RM="${dry}rm" RMDIR="${dry}rmdir" SED="${dry}sed" SU="${dry}su" UPDATE_GRUB="${dry}update-grub" USERADD="${dry}useradd" USERMOD="${dry}usermod" } usage() { echo "Usage: $(basename "$0") [-h] [-n]" echo "" echo " -h print this help and exit." echo " -n dry run, don't make changes." } error() { printf "${red}ERROR:${sgr0} %s\n" "$1" >&2 exit 1 } info() { printf "${green}->${sgr0} %s\n" "$1" } title() { echo "${blue}${bold}=> ${1}${sgr0}" } skipped() { echo "${yellow}SKIPPED:${sgr0} ${1}" } apt_install() { title "Install APT packages" info "Updating package database" ${APT} update info "Installing APT packages" ${APT} install -y \ apt-file \ ascii \ build-essential \ ca-certificates \ cmake \ cmake-doc \ curl \ exuberant-ctags \ g++ \ gcc \ git \ git-crypt \ gnupg \ help2man \ htop \ jq \ make \ man-db \ nodejs \ pandoc \ pkg-config \ psmisc \ python3 \ python3-virtualenv \ ripgrep \ rsync \ shellcheck \ sshpass \ stow \ sudo \ tmux \ unzip \ zsh info "Updating apt-file database" ${APT_FILE} update } 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" for dir in $(seq -f '/usr/local/man/man%.0f' 9); do ${MKDIR} -vp "${dir}" done } user_setup() { title "Setup user: $1" if user_exists "$1"; then info "User $1 exists. Updating..." user_update "$1" else info "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.` \ 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() { info "Adding $1 to group staff" ${USERMOD} -aG staff "$1" info "Changing login shell to zsh" ${CHSH} -s /bin/zsh "$1" info "Removing bash leftover files" for f in .bash_history .bash_logout .bashrc .profile; do ${RM} -vf "/home/${1}/${f}" done } # Allow `sudo` without password for user $1. user_allow_sudo_nopasswd() { info "Enabling sudo without password for ${1}" contents="$1 ALL=(ALL:ALL) NOPASSWD:ALL" sudoers_file="/etc/sudoers.d/${1}_nopasswd" if is_dry_run; then echo "echo \"$contents\" >$sudoers_file" else echo "$contents" >"$sudoers_file" fi } # Deploy dotfiles for user $1 from URL $2. deploy_dotfiles() { title "Deploy dotfiles" dotfiles_dir="/home/$1/.dotfiles" if [ -d "${dotfiles_dir}" ]; then skipped "${dotfiles_dir} exists" else info "Cloning dotfiles" ${SU} "$1" -c "git clone $2 ${dotfiles_dir}" ( info "Installing dotfiles" ${CD} "${dotfiles_dir}" ${SU} "$1" -c "./install.sh -y" ) fi } parse_args() { while getopts 'hn' opt; do case "$opt" in h) # help usage exit 0 ;; n) # dry run dry_run="yes" ;; *) # invalid argument usage exit 1 ;; esac done } require_root() { [ "$(id -u)" -eq 0 ] || error "This script must be run as root!" } user="fernando" dotfiles_url="https://git.schauenburg.me/fernando/dotfiles.git" main() { setup_colors parse_args "$@" is_dry_run || require_root setup_commands apt_install grub_disable_timeout ensure_usr_bin_fd ensure_usr_local_man_manN user_setup "$user" deploy_dotfiles "$user" "$dotfiles_url" } main "$@"