#!/bin/bash

set -e

###############################
# OXIBOX BACKUP AGENT
# SYNOLOGY INSTALLER 1.0.0
###############################

###############################
# VARS AND FUNCTIONS
###############################

OXIBOX_LOG_DIR="/var/log/oxibox"
OXIBOX_STATUS_FILE="$OXIBOX_LOG_DIR/oxibox-status"

# Print to stderr
echo_error() {
    echo >&2 "$@"
}

# Write given status to status file
write_status () {
    echo "$1" > "$OXIBOX_STATUS_FILE"
}

# Check if user is root
is_root() {
    if [[ $EUID -ne 0 ]]; then
        echo_error "This script must be run as root"
        exit 1
    fi 
}

# Print list of available arguments
print_help() {
    echo "Install/remove Oxibox backup agent on a Synology NAS."
    echo ""
    echo "Usage:"
    echo "    $(basename "$0") <install|remove> [flags]"
    echo ""
    echo "Flags"
    echo "    -h, --help        Display this help message."
}

# Install Oxibox backup agent on NAS
install() {
    local bundle_script_path='/usr/sbin/download_oxibackup_bundle.sh'

    echo "Downloading files..."

    if ! curl -s 'https://deploy.oxibox.com/synology/dsm7/bundles/download.php' -o "$bundle_script_path"; then
        echo_error "Error: failed to download bundle downloader"
        return 1
    fi

    if ! chmod 700 "$bundle_script_path"; then
        echo_error "Error: chmod error on bundle downloader"
        return 1
    fi

    if ! "$bundle_script_path"; then
        echo_error "Error: could not download bundle files"
        return 1
    fi

    echo "Installing Oxibox backup agent..."

    local exit_code=0
    '/usr/sbin/oxibackup_agent_dist.sh' install || exit_code=$?

    if [[ $exit_code -eq 42 ]]; then
        echo "Oxibox backup agent is already installed"
        return 0
    elif [[ $exit_code -ne 0 ]];then
        echo_error "Error: failed to install Oxibox backup agent"
        return 1
    fi

    if ! /opt/oxibackup/bin/oxibox-registration --type 'nas-synology' -y; then
        write_status "REGISTRATION_ERROR"
        return 1
    fi

    write_status "INSTALLED"

    echo "Oxibox backup agent was successfully installed"
    return 0
}

# Remove Oxibox backup agent from NAS
remove() {
    local agent_dist_script_path='/usr/sbin/oxibackup_agent_dist.sh'

    if [ ! -f "$agent_dist_script_path" ]; then
        echo "Oxibox backup agent is not installed or files are missing"
        return 1
    fi

    echo "Removing Oxibox backup agent..."
    if ! "$agent_dist_script_path" remove; then
        echo_error "Error: failed to remove Oxibox backup agent"
        return 1
    fi

    echo "Oxibox backup agent was successfully removed"
    return 0
}

###############################
# ARGUMENTS
###############################

POSITIONAL=()
while [[ $# -gt 0 ]]; do
    case "$1" in
    "-h" | "--help")
        print_help
        exit 0
        ;;
    -*) # Unknown flag
        echo_error "Unknown flag. Use --help to get a list of valid options. Exiting."
        exit 1
        ;;
    *)
        POSITIONAL+=("$1")
        shift
        ;;
    esac
done
set -- "${POSITIONAL[@]}"

###############################
# MAIN
###############################

case "${1:-}" in
    "install")
        is_root
        install
        exit $?
    ;;
    "remove")
        is_root
        remove
        exit $?
    ;;
    *)
        if [ -z "${1:-}" ]; then
            print_help
        else
            echo_error "Unknown command. Use --help to get a list of valid options. Exiting."
        fi
        exit 1
    ;;
esac
