#!/bin/sh

set -e

if [ "$(id -u)" -ne 0 ]
then
        echo "This script must be run as root." >&2
        exit 1
fi

OS_ID=$({
        source /etc/os-release
        echo $ID
})

if [ "$OS_ID" != "alpine" ]
then
	echo "This script is only compatible with Alpine Linux." >&2
	exit 1
fi

apk update
apk upgrade
apk update
apk add curl
apk add doas
apk add htop
apk add less
apk add nano
apk add openssh
apk add zlib

mkdir -p /etc/skel
mkdir -p /etc/doas.d
mkdir -p /etc/ssh/sshd_config.d

cat  <<- 'EOF' > /etc/skel/.ashrc
alias ls=ls --color=auto
ps_user_color=034
[ "$(id -u)" -eq 0 ] && ps_user_color=208
PS1='\[\e[38;5;238m\][\[\e[38;5;25m\]\H\[\e[38;5;238m\]] \[\e[38;5;'$ps_user_color'm\]\u \[\e[38;5;169m\]\w \[\e[38;5;238m\]\$ \[\e[m\]'
EOF

cat <<- 'EOF' > /etc/skel/.profile
export ENV="$HOME/.ashrc"
EOF

cat <<- 'EOF' > /etc/ssh/sshd_config.d/lockdown.conf
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
KbdInteractiveAuthentication no
AllowAgentForwarding no
EOF

printf "What username to create? "
read username
printf "Please provide your SSH public key (usually ~/.ssh/id_*.pub): "
read pubkey
adduser "$username" -D
adduser "$username" wheel
echo "$username:*np*" | chpasswd -e
mkdir -p "/home/$username/.ssh"
echo "$pubkey" > "/home/$username/.ssh/authorized_keys"
chown -R "$username:$username" "/home/$username/.ssh"
rc-service sshd restart
passwd -dl root
echo "OK, the system is ready to use."
