#!/usr/bin/env bash
set -e
sudo -v

# the horrors
function banner() {
    local pos=$1
    local w=50
    local text=" $2 "
    local pad=$((w - ${#text}))
    local l=$((pad/2)) r=$((pad - pad/2))
    local left=$(printf '%*s' "$l" ''); left=${left// /─}
    local right=$(printf '%*s' "$r" ''); right=${right// /─}

    if [[ "$pos" == "top" ]]; then
        printf "┌%s%s%s┐\n\n" "$left" "$text" "$right"
    else
        printf "\n└%s%s%s┘\n\n" "$left" "$text" "$right"
    fi
}

cat << 'EOF'
       __              __
 ___ _/ /____  __ _   / /____ _    _____
/ _ `/ __/ _ \/  ' \_/ __/ _ \ |/|/ / _ \
\_,_/\__/\___/_/_/_(_)__/\___/__,__/_//_/
        ..... brought to you by futile


EOF

# check if on fedora
if ! grep -q '^ID=fedora$' /etc/os-release 2>/dev/null; then
    echo "errrm... why aren't you on fedora"
    exit 1
fi

# get preferences
banner top "getting preferences"
while [[ -z "$hostname" ]]; do
    read -rp "choose hostname: " hostname
done
while [[ -z "$pubkey" ]]; do
    read -rp "give ssh key: " pubkey
done
echo -e "using hostname \"$hostname\""
banner bottom "got preferences"

# write ssh key
banner top "writing ssh key"
sudo mkdir -p ~/.ssh
sudo chmod 700 ~/.ssh
sudo touch ~/.ssh/authorized_keys
sudo chmod 600 ~/.ssh/authorized_keys
sudo chown -R $(id -un) ~/.ssh
echo "$pubkey" >> ~/.ssh/authorized_keys
banner bottom "wrote ssh key"

# change hostname
banner top "changing hostname"
sudo hostnamectl hostname $hostname
sudo hostnamectl
banner bottom "changed hostname"

# configure dnf
banner top "configuring dnf"
sudo tee /etc/dnf/dnf.conf << 'EOF'
[main]
defaultyes = True
max_parallel_downloads = 10
EOF
banner bottom "configured dnf"

# update system
banner top "updating system"
sudo dnf update -y
banner bottom "updated system"

# install misc
banner top "installing misc packages"
sudo dnf install -y micro btop fastfetch policycoreutils-python-utils
curl -fsSL https://vykar.borgbase.com/install.sh | sh
banner bottom "installed misc packages"

# install (& configure) docker
banner top "installing docker"
sudo dnf config-manager addrepo --from-repofile https://download.docker.com/linux/fedora/docker-ce.repo --overwrite
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $(id -un)
sudo systemctl enable --now docker
sudo docker run hello-world
banner bottom "installed docker"

# install (& configure) fish
banner top "installing fish"
sudo dnf install -y fish
sudo chsh -s $(which fish) $(id -un)
fish -c "set -U fish_greeting"
fish -c "set -Ux EDITOR $(which micro)"
banner bottom "installed fish"

# install (& configure) cron
banner top "installing cron"
sudo dnf install -y cronie
sudo systemctl enable --now crond
banner bottom "installed cron"

# configure sshd
banner top "configuring sshd"
sudo tee /etc/ssh/sshd_config << 'EOF'
Include /etc/ssh/sshd_config.d/*.conf
Port 67
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
EOF
sudo semanage port -a -t ssh_port_t -p tcp 67
sudo systemctl restart sshd
banner bottom "configuring sshd"

# finish
echo -e "done! please log out and log back in with \e[1;5mssh -p 67 $(id -un)@$(curl -s https://checkip.amazonaws.com)\e[0m\n"
