#!/bin/bash
#
# Copyright (C) 2011-2018 severalnines.com
#

function ensure_wget_installed
{
    command -v wget &>/dev/null
    [[ $? -eq 0 ]] && return
    echo "=> Installing wget ..."
    if command -v apt-get &>/dev/null; then
        apt-get install -y wget
    else
        yum install -yt wget
    fi
}

function ensure_tools_installed
{
    echo "=> Install/upgrade s9s-tools for error-reporting."
    # make sure we have wget
    if [[ -z "$OFFLINE" ]] && ! command -v wget >/dev/null 2>&1; then
        ensure_wget_installed
    fi
    if [[ -z "$OFFLINE" ]] && ! command -v s9s >/dev/null 2>&1; then
        local tmp_script
        tmp_script=$(mktemp)
        # Download via HTTPS and verify success before executing
        if wget -qO "$tmp_script" https://repo.severalnines.com/s9s-tools/install-s9s-tools.sh; then
            bash "$tmp_script"
            rm -f "$tmp_script"
        else
            echo "Error: Failed to download s9s-tools installation script." >&2
            rm -f "$tmp_script"
            return 1
        fi
    fi
    # and lets print out the version to verify
    s9s --version
}

function create_local_s9s_user
{
    export S9S_SYSTEM_CONFIG=/dev/null
    export S9S_USER_CONFIG=$HOME/.s9s/s9s-error-reporter.conf

    unset cmon_user

    if [ -r $S9S_USER_CONFIG ]; then
        source $S9S_USER_CONFIG &>/dev/null
    fi

    # verify if the specified user works
    if [ -n "$cmon_user" ]; then
        s9s user --whoami
        if (( $? == 3 )); then
            # access denied, try to create a new user
            cmon_user=""
        fi
    fi

    # check if s9s.conf doesn't have a user configured, then create one
    if [ -z "$cmon_user" ]; then
        if [ -z "$SUDO_USER" ]; then
            cmon_user="s9s-error-reporter-root"
        else
            cmon_user="s9s-error-reporter-$SUDO_USER"
        fi

        # remove the non-working config
        rm -f $S9S_USER_CONFIG 2>/dev/null

        # new s9s CLI requires the username defined alone without --cmon-user
        # only the new one has BUILD info in --version:
        if s9s --version | grep BUILD >/dev/null; then
            s9s user --create --generate-key --group=admins --controller="https://localhost:9500" "$cmon_user"
        else
            s9s user --create --generate-key --group=admins --controller="https://localhost:9500" --cmon-user="$cmon_user"
        fi

        cat - > $S9S_USER_CONFIG <<CONFIG_END
[global]
cmon_user=$cmon_user
controller=https://localhost:9500
CONFIG_END

        if [ -z "$SUDO_USER" ]; then
            chown -R root ~/.s9s
        else
            chown -R "$SUDO_USER" ~/.s9s
        fi
    fi

    s9s user --whoami

    unset S9S_SYSTEM_CONFIG
    unset S9S_USER_CONFIG
}

function create_report
{
    create_local_s9s_user

    export S9S_SYSTEM_CONFIG=/dev/null
    export S9S_USER_CONFIG=$HOME/.s9s/s9s-error-reporter.conf

    if [ -n "$DESTINATION" ]; then
        s9s cluster --create-report --output-dir=$DESTINATION --cluster-id=$CLUSTER_ID --log
    else
        s9s cluster --create-report --cluster-id=$CLUSTER_ID --log
    fi

    unset S9S_SYSTEM_CONFIG
    unset S9S_USER_CONFIG
}

WWWSTORE=0

args=`getopt i:d: $*`
set -- $args
for i
do
    case "$i" in
        -i)
            CLUSTER_ID="$2"; shift;
            shift;;
        -d)
            DESTINATION="$2"; shift;
            shift;;
        --)
            shift; break;;
    esac
done

if [ -z "$CLUSTER_ID" ]; then
    echo "s9s_error_report -i <clusterid> [-d destination-dir]"
    exit 1
fi

# We need root access to be able to iterate files inside /etc/cmon.d
if (( EUID != 0 )); then
    echo "must be executed as 'root' or with 'sudo'  ( you are logged in as $USER )."
    exit 1
fi

# start if not running yet
systemctl --no-pager status cmon || systemctl start cmon || true

ensure_tools_installed
create_report

