#!/bin/bash

RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
NC=`tput sgr0`
REG_PUB="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4UTXOYXrKA6dR7KizO2AvqqHKQGJE/FZF2oKTiofWEYDf+UWylksH4WjFmVczDUHN653Ve/QOIyRfI6IUuVa2hJ+l02xFV7rdl7L5zSZwKiSJr+SefouzWIFwS3VS3gbLOqk864a1NkUR97yKYjxsZiT9fISf771HqEKhsXOzZDOFbxt5u+YAaAJIJlU0EMKkDRBBtAVxmLFHme0uSpZ8DlYMFARGe1s0I++1eby0NVtzP3TarouvkPN1cFmS7UhQCsHzcmDMcNyrtHGBnlgjihd4m2bppmY75xTTR/PQTKDWqwklyYZhiDCKjZYzxWTk493SwKfZfaT9FOU0r4FT reg@homelet"
MAINT_PUB="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCx9Bfw8wkceadnounwXVHInI0FyNEj3z64bqXA8cwbgkqkXTVWnI3I6vUzKY8dSfL8PXydCaVnGxogP88Y294k4rjIf8NGubwNe5B2oyLNuscBhd1QWzEmvr4ej32I1Ot3oulJsbqt7oSKUr6pQ4fD44WXjGNaQx3WhbsSJb28k4rNRs4bY+HlScsaKlfVRpE+kuI64BNPl4+IVfkJzs+E7NuDp3DnHl4pwbWjsj856/coKe0v0XtMOXZP7pVn/TLRGbNA+w/HVLLRud5taTZXxV5jYHOeftLFupSZL5VdGHWrC6/GeWgtwlvcsfmt6erc4p6MQqKxT3SV/CNIS2j1 maint@cardshop"

green() { 
    echo "${GREEN}$1${NC}" 
}
red() { 
    echo "${RED}$1${NC}"
}
yellow() {
    echo "${YELLOW}$1${NC}"
}
blue() {
    echo "${BLUE}$1${NC}"
}
step() {
    yellow "=> $1"
}

die() {
    red "$@"
    exit 1
}

# blank the screen
reset

blue "=== Cardshop WriterHost setup ==="

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    red "Must be ran as root. exiting."
    exit 1
fi

if [ -z "${REVERSE_SSH_PORT}" ]; then
    red "you must set the REVERSE_SSH_PORT environ variable to the appropriate port for this writer host."
    echo " See http://wiki.kiwix.org/wiki/Cardshop-maintenance"
    exit 1
fi

pathadd() {
    if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
        PATH="${PATH:+"$PATH:"}$1"
        echo "export PATH=${PATH}" > /root/.bash_profile
        source /root/.bash_profile
    fi
}

step "Ugrading base Ubuntu packages"
apt update -y && apt --fix-broken install && apt upgrade -y && apt --fix-broken install && apt autoremove -y 

step "Installing additional packages"
apt install -y vim openssh-server

step "Add maintenance SSH keys"
mkdir -p /root/.ssh
chmod 700 /root/.ssh
echo "${REG_PUB}" >> /root/.ssh/authorized_keys
echo "${MAINT_PUB}" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys

step "Add reverse-SSH connection"
#echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config
#echo "    UserKnownHostsFile=/dev/null" >> /etc/ssh/ssh_config
systemctl enable ssh
echo "REVERSE_SSH_PORT=${REVERSE_SSH_PORT}" > /etc/default/reverse-ssh
curl -L -o /etc/systemd/system/reverse-ssh.service https://raw.githubusercontent.com/kiwix/cardshop/master/whost/reverse-ssh.service
systemctl daemon-reload
systemctl enable reverse-ssh.service
systemctl restart reverse-ssh.service
systemctl status --no-pager -l reverse-ssh.service

step "Install docker-CE from official repo"
# https://docs.docker.com/install/linux/docker-ce/ubuntu/#set-up-the-repository
apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update -y
apt-get install -y docker-ce
mkdir -p /data

step "Install basic python dependencies"
add-apt-repository universe
apt update -y
apt install -y python3-pip
/usr/local/bin/pip3 install -U pip || die "unable to update pip"
/usr/local/bin/pip3 install virtualenv  || die "unable to install virtualenv"
virtualenv -p /usr/bin/python3 /root/whostenv || die "unable to create venv"
source /root/whostenv/bin/activate || die "unable to source venv"

step "Download code"
pip install -U whost

step "Adding whost folder to PATH"
WHOST_BINS=`python -c 'import sys ; print([p for p in sys.path if p.endswith("site-packages")][-1])'`
pathadd "${WHOST_BINS}"
echo $PATH

step "Add whost-config to login"
inprofile=`cat /root/.bash_profile |grep "^whost-config$" |wc -l`
if [ "${inprofile}" = "0" ];
then
    echo "source /root/whostenv/bin/activate" >> /root/.bash_profile
    echo "whost-config" >> /root/.bash_profile
fi

step "Pulling worker"
docker pull kiwix/cardshop-worker

step "Restarting"
read -p "About to reboot. Please type any key once ready." yn
