#!/bin/sh

set -e

# check if stderr is a terminal...
if [ -t 2 ]; then
    # see if it supports colors...
    ncolors=$(tput colors)
    if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
        bold="$(tput bold)"
        underline="$(tput smul)"
        standout="$(tput smso)"
        normal="$(tput sgr0)"
        black="$(tput setaf 0)"
        red="$(tput setaf 1)"
        green="$(tput setaf 2)"
        yellow="$(tput setaf 3)"
        blue="$(tput setaf 4)"
        magenta="$(tput setaf 5)"
        cyan="$(tput setaf 6)"
        white="$(tput setaf 7)"
    fi
fi

if [ $EUID -ne 0 ]; then
    echo "${red}${bold}This script must be run as root or with sudo!${normal}" 1>&2
    exit 1
fi

# Get the public signing key.
echo "${bold}Getting and adding kamp-simon public signing key from http://mirror.kamp.de/kamp-simon/GPG-KEY-deb.${normal}"
#wget --quiet -O - http://mirror.kamp.de/kamp-simon/GPG-KEY-deb | apt-key add -
if hash curl 2>/dev/null; then
    curl http://mirror.kamp.de/kamp-simon/GPG-KEY-deb | apt-key add -
    RETVAL="${?}"
elif hash wget 2>/dev/null; then
    wget --quiet -O - http://mirror.kamp.de/kamp-simon/GPG-KEY-deb | apt-key add -
    RETVAL="${?}"
else
    echo "${red}${bold}Neither curl nor wget available! Exiting!${normal}"
    exit 1
fi
echo "${green}${bold}Added kamp-simon public signing key.${normal}"

# Add repository list.
echo "${bold}Adding kamp-simon repository as /etc/apt/sources.list.d/kamp-simon.list.${normal}"
if hash curl 2>/dev/null; then
    curl http://mirror.kamp.de/kamp-simon/kamp-simon.list | tee /etc/apt/sources.list.d/kamp-simon.list
elif hash wget 2>/dev/null; then
    wget --quiet -O - http://mirror.kamp.de/kamp-simon/kamp-simon.list | tee /etc/apt/sources.list.d/kamp-simon.list
else
    echo "${red}${bold}Neither curl nor wget available!${normal}"
    exit 1
fi
echo "${green}${bold}Added kamp-simon repository.${normal}"

# Remove old repository list.
if [ -e /etc/apt/sources.list.d/lusi.list ]; then
    echo "${bold}Removing old repository list from /etc/apt/sources.list.d/lusi.list.${normal}"
    rm -rf /etc/apt/sources.list.d/lusi.list
    echo "${green}${bold}Removed old repository list.${normal}"
fi

# Update repositories.
echo "${bold}Updating repositories.${normal}"
apt-get update
echo "${green}${bold}Updated repositories.${normal}"

# These definitions first check if their variable hasn't already been set.

# The server to send to. (default: https://backend.kamp-simon)
#[ -z "${SERVER}" ] && SERVER="https://backend.kamp-simon:8443"
[ -n "${1}" ] && SERVER="${1}"
# Allow insecure curl SSL connections? (default: false)
# IF SET TO true ALL SERVER CERTIFICATES WILL BE ALLOWED!
[ -z "${INSECURE}" ] && INSECURE=false
# Send data after installation? (default: false)
[ -z "${SEND_NOW}" ] && SEND_NOW=false
# Keep the cronjobs? (default: true)
[ -z "${INSTALL_CRON}" ] && INSTALL_CRON=true
# Keep the DPKG hooks? (default: true)
[ -z "${INSTALL_DPKG_HOOKS}" ] && INSTALL_DPKG_HOOKS=true
# Send Pre Shared Key? (default: empty string)
[ -z "${PSK}" ] && PSK=""

# Set configuration options.
# Uncomment to set them to the above variables, comment to use defaults from package.
echo "${bold}Seeding configuration options.${normal}"
[ -n "${SERVER}" ] && echo "kamp-simon-shell-client kamp-simon-shell-client/server string ${SERVER}" | debconf-set-selections
echo "kamp-simon-shell-client kamp-simon-shell-client/insecure_curl boolean ${INSECURE}" | debconf-set-selections
echo "kamp-simon-shell-client kamp-simon-shell-client/send_now boolean ${SEND_NOW}" | debconf-set-selections
echo "kamp-simon-shell-client kamp-simon-shell-client/install_cron boolean ${INSTALL_CRON}" | debconf-set-selections
echo "kamp-simon-shell-client kamp-simon-shell-client/install_dpkg_hooks boolean ${INSTALL_DPKG_HOOKS}" | debconf-set-selections
echo "kamp-simon-shell-client kamp-simon-shell-client/psk string ${PSK}" | debconf-set-selections
echo "${green}${bold}Seeded configuration options.${normal}"

# Install kamp-simon-shell-client.
echo "${bold}Installing kamp-simon-shell-client.${normal}"
apt-get install --yes kamp-simon-shell-client
echo "${green}${bold}"
echo "***************************************************"
echo "* kamp-simon-shell-client successfully installed. *"
echo "***************************************************"
echo "${normal}"

echo "${bold}Signing up.${normal}"
echo "${yellow}${bold}Errors with Response-Code 409 can be ignored.${normal}"
kamp-simon-shell-client --sign-up
echo "${green}${bold}Signed up.${normal}"

echo "${bold}Sending all data.${normal}"
kamp-simon-shell-client --all
echo "${green}${bold}Send all data.${normal}"

if dpkg -s lusi-shell-client &> /dev/null; then
    echo "${bold}Purging old lusi-shell-client.${normal}"
    apt-get purge --yes lusi-shell-client
    echo "${green}${bold}Purged old lusi-shell-client.${normal}"
fi
