188 lines
4.1 KiB
Bash
Executable file
188 lines
4.1 KiB
Bash
Executable file
#!/bin/sh
|
|
set -e
|
|
|
|
USERNAME="fernando"
|
|
DOTFILES_URL="https://git.schauenburg.me/fernando/dotfiles.git"
|
|
|
|
if [ -t 1 ]; then
|
|
sgr0="$(printf '\033[0m')"
|
|
red="$(printf '\033[31m')"
|
|
yellow="$(printf '\033[33m')"
|
|
blue="$(printf '\033[34m')"
|
|
else
|
|
sgr0=''
|
|
red=''
|
|
yellow=''
|
|
blue=''
|
|
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_install() {
|
|
title "Install APT packages"
|
|
|
|
apt update && apt install -y \
|
|
apt-file \
|
|
ascii \
|
|
build-essential \
|
|
ca-certificates \
|
|
cmake \
|
|
cmake-doc \
|
|
curl \
|
|
exuberant-ctags \
|
|
g++ \
|
|
gcc \
|
|
git \
|
|
git-crypt \
|
|
gnupg \
|
|
htop \
|
|
jq \
|
|
make \
|
|
man-db \
|
|
nodejs \
|
|
pkg-config \
|
|
psmisc \
|
|
python3 \
|
|
python3-virtualenv \
|
|
ripgrep \
|
|
rsync \
|
|
shellcheck \
|
|
sshpass \
|
|
stow \
|
|
sudo \
|
|
tmux \
|
|
unzip \
|
|
zsh
|
|
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"
|
|
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_install
|
|
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 "$@"
|
|
|