#!/usr/bin/env bash
#
# Copyright (C) 2017 ScyllaDB
#

set -eu -o pipefail

is_systemd() {
    grep -q '^systemd$' /proc/1/comm
}

interactive_ask_service() {
    echo $1
    echo $2
    while true; do
        if [[ "$3" == "yes" ]]; then
        prompt="[YES/no]"
        elif [[ "$3" == "no" ]]; then
        prompt="[yes/NO]"
        else
        prompt="[yes/no]"
        fi
        result=""
        while [[ x == x"${result}" ]]; do
            read -p ${prompt} ans
            if [[ x == x"${ans}" ]]; then
                result="$3"
            else
                result="${ans}"
            fi
        done
        case $(echo ${result} | tr '[:upper:]' '[:lower:]') in
            "y" | "yes")
                return 1
                ;;
            "n" | "no")
                return 0
                ;;
        esac
    done
}

print_help() {
  echo "Usage: $(basename $0) [-y][--no-scylla-setup][--no-enable-service][--no-check-for-updates]

Options:
  -y, --assume-yes          assume that the answer to any question which would be asked is yes
  --no-scylla-setup         skip setting up and enabling local Scylla instance as a storage backend for Scylla Manager
  --no-enable-service       skip enabling service
  --no-check-for-updates    skip enabling periodic check for updates
  -h, --help                print this help

Interactive mode is enabled when no flags are provided.
"
}

if [[ $# != 0 ]]; then
    INTERACTIVE=0
else
    INTERACTIVE=1
fi

SETUP_SCYLLA=1
ENABLE_SERVICE=1
CHECK_FOR_UPDATES=1

while [[ $# -gt 0 ]]
do

case $1 in
    -y|--assume-yes)
    shift
    ;;
    --no-scylla-setup)
    SETUP_SCYLLA=0
    shift
    ;;
    --no-enable-service)
    ENABLE_SERVICE=0
    shift
    ;;
    --no-check-for-updates)
    CHECK_FOR_UPDATES=0
    shift
    ;;
    -h|--help)
    print_help
    exit 0
    ;;

    *)
    echo "unknown command $1"
    exit 1
    ;;
esac
done

if [[ "$(id -u)" != "0" ]]; then
    echo "Requires root permission."
    exit 1
fi

if [[ ${INTERACTIVE} == 1 ]]; then
    interactive_ask_service "Do you want to setup and enable local Scylla instance as a storage backend for the Scylla Manager installation?" "Yes - setup and automatically start Scylla server when the node boots. No - skip this step." "yes" &&:
    SETUP_SCYLLA=$?
    interactive_ask_service "Do you want the Scylla Manager service to automatically start when the node boots?" "Yes - automatically start Scylla Manager when the node boots. No - skip this step." "yes" &&:
    ENABLE_SERVICE=$?
    interactive_ask_service "Do you want to enable Scylla Manger to check if there is a newer version of Scylla Manager available?" "Yes - start the Scylla Manager housekeeping service to check for a newer version. This check runs periodically. No - skips this step." "yes" &&:
    CHECK_FOR_UPDATES=$?
fi

if command -v scylla_setup > /dev/null && [[ ${SETUP_SCYLLA} == 1 ]]; then
    scylla_dev_mode_setup --developer-mode 1
    scylla_cpuset_setup --smp 1

    f=""
    if [[ -f /etc/default/scylla-server ]]; then
        f=/etc/default/scylla-server
    elif [[ -f /etc/sysconfig/scylla-server ]]; then
        f=/etc/sysconfig/scylla-server
    fi
    if [[ "$f" != "" ]]; then
      sed -i -e 's/^SCYLLA_ARGS=.*/SCYLLA_ARGS="--memory 250M --log-to-syslog 0 --log-to-stdout 1 --default-log-level info --network-stack posix"/g' ${f}
    fi
fi

if [[ ${ENABLE_SERVICE} == 1 ]]; then
    if is_systemd; then
        systemctl enable scylla-manager.service
    fi
fi

if [[ ${CHECK_FOR_UPDATES} == 1 ]]; then
    if is_systemd; then
        systemctl enable scylla-manager-check-for-updates.timer
        systemctl start scylla-manager-check-for-updates.timer
    fi
fi

systemctl daemon-reload

echo "Scylla Manager setup finished."
