#!/bin/sh

###############################################
# CONFIGURATION
###############################################

INSTALL_DIR="/opt/oxibackup"

if ! . "$INSTALL_DIR/etc/oxibackupd/service.conf" >/dev/null 2>&1
then
    >&2 echo "Could not load service configuration file"
    exit 1
fi

if ! . "$INSTALL_DIR/etc/oxibackupd/config/oxibackupd.conf" >/dev/null 2>&1
then
    >&2 echo "Could not load Oxibox backup agent configuration file"
    exit 1
fi

CONFIG_FILE="$INSTALL_DIR/etc/oxibackupd/config/oxibackupd.conf"
SERVICE_FULL_NAME="Oxibox backup agent"

# Find out if we must use 'echo' or 'echo -e' to display tabs and line breaks
if echo "\n" | grep -q '\\n' > /dev/null 2>&1
then
    ECHO_ESCAPE_CMD="echo -e"
else
    ECHO_ESCAPE_CMD="echo"
fi

###############################################
# FUNCTIONS
###############################################

show_help () {
    echo "$SERVICE_FULL_NAME service configuration"
    echo "Available commands:"
    $ECHO_ESCAPE_CMD "\tset\tSet value for:"
    $ECHO_ESCAPE_CMD "\t\t\t- 'gc': int in range ]0,100], defines when the program should trigger garbage collection (default 20)"
    $ECHO_ESCAPE_CMD "\t\t\t- 'cpus': int in range [0,nbCPUs], defines maximum number of threads, set 0 (default) to use the maximum based on available CPUs"
    $ECHO_ESCAPE_CMD "\tget\tGet current value for 'all' or for one of possible values defined in 'set'"
    $ECHO_ESCAPE_CMD "\thelp\tShow this help message"
}

# Edit config file
# $1: variable name in file
# $2: new value
edit_config_value () {
    if ! command -v sed >/dev/null 2>&1
    then
        >&2 echo "'sed' is not available"
        exit 1
    fi
    if sed -i "s/^$1=.*$/$1=$2/" "$CONFIG_FILE" 2>/dev/null
    then
        echo "Configuration successfully applied, restart service for changes to take effect"
    else
        >&2 echo "An error occurred while applying the new value in the configuration"
        exit 1
    fi
}

# Check value and edit config file
# $1: value name given as argument
# $2: new value
set_config () {
    case "$1" in
        "gc")
            if [ "$2" -gt 0 ] 2>/dev/null && [ "$2" -le 100 ] 2>/dev/null
            then
                edit_config_value "GC" "$2"
            else
                >&2 echo "'$2' is not a valid value for GC"
                exit 1
            fi
        ;;
        "cpus")
            if [ "$2" -ge 0 ] 2>/dev/null
            then
                edit_config_value "CPUS" "$2"
            else
                >&2 echo "'$2' is not a valid value for CPUS"
                exit 1
            fi
        ;;
        *)
            >&2 echo "'$1' is not known to be configurable"
            exit 1
        ;;
    esac
}

# Get one or all values from config file
# $1: value name given as argument
# $2: new value
get_config () {
    case "$1" in
        "all")
            echo "Current values in configuration:"
            get_config "gc"
            get_config "cpus"
        ;;
        "gc")
            echo "GC: $GC"
        ;;
        "cpus")
            echo "CPUs: $CPUS"
        ;;
        *)
            >&2 echo "'$1' is not a valid option"
            exit 1
        ;;
    esac
}

###############################################
# SERVICE CONFIGURATION
###############################################

if [ $# -lt 1 ] || [ $# -gt 3 ]
then
    >&2 echo "Wrong number of arguments"
    show_help
    exit 1
fi

case "$1" in
    "set")
        if [ $# -ne 3 ]
        then
            >&2 echo "Wrong number of arguments"
            show_help
            exit 1
        fi
        set_config "$2" "$3"
    ;;
    "get")
        if [ $# -ne 2 ]
        then
            >&2 echo "Wrong number of arguments"
            show_help
            exit 1
        fi
        get_config "$2"
    ;;
    "help")
        show_help
    ;;
    *)
        >&2 echo "Unknown command $1"
        show_help
        exit 1
    ;;
esac

exit 0
