#!/bin/bash
set -u

LIB_PATH="$(dirname "${BASH_SOURCE[0]}")/../lib/lwr/ipman"
source ${LIB_PATH}/ipman.env

function help() {
    echo "
Usage:
    ipman get-primary
    ipman show [<interface_name>]
    ipman status [<interface_name> [<static|dhcp|linklocal>]]
    ipman set <interface_name> <subcommand> [options]

Availabe interfaces: 
$(netman list | sed 's/^/    /')

Commands:
    get-primary
        Returns the primary network interface name.

    show [<interface_name>]
        Returns the dynamic config for the given network interface in json format. (or all of them, if not given)

    status [<interface_name> [<static|dhcp|linklocal>]]
        Returns the status of the given network interface. (or all of them, if not given)

    wait-for-init
        Blocking call, waits for ipman init to be done regardless of the outcome (success or failure). The subscriptions for events
        can only be done after init ended.

    set <interface_name> <property> [<new-value>...]
        properties:
            enable <0|1>
                Set to UP (1) or DOWN (0) the given interface. 

            static-enable <0|1>
                Enable (1) or disable (0) static IP configuration for the given interface.

            dhcp-enable <0|1>
                Enable (1) or disable (0) DHCP configuration for the given interface.

            dhcp-linklocal-fallback <0|1>
                Enable (1) or disable (0) DHCP link-local fallback for the given interface.

            linklocal-enable <0|1>
                Enable (1) or disable (0) link-local IP configuration for the given interface.

            static <ip>/<netmask-CIDR> <gateway>
            static-ip <ip>/<netmask-CIDR>
            static-gateway <gateway>
                Configure a static IP address for the given interface.
                    <ip>/<netmask-CIDR> - IP address and netmask in CIDR notation (e.g., 192.168.1.10/24).
                    <gateway> - Default gateway IP address.

            config <interface-config-json>
                Applying an entire interface config.
                    <interface-config-json> - Interface dynamic-config what is an escaped string following the JSON notation.


Return values: 
    Success (return code: 0): Result on stdout
    Fail (return code: non 0): Error message on stderr
"
}

function fail() {
    echo -e "${@}" 1>&2
    help
    exit 1
}

# Calls the given commands with arguments, print result to stdout, if succed
# If any error occures, it prints error message to stderr and help to stdout, then terminates the process
function eval_lib_function() {
    local ret
    ret=$("${@}")
    if [[ $? -ne 0 ]]; then
        fail "${ret}"
    fi
    echo "${ret}"
}



function main() {
    local dynamic_config="${IPMAN_CONFIG_PATH}"
    subcommand="${1:-}"
    shift

    #get out dynamic config file if added (mainly for test purposes), and store the other arguments
    positional_args=()
    while [[ $# -gt 0 ]]; do
        case ${1} in
            -c|--config-file)
                dynamic_config=${2}
                if [ ! -f "${dynamic_config}" ]; then
                    fail "Given dynamic-config: ${dynamic_config} not exists!"
                fi
                shift
                ;;
            --*|-*)
                fail "Unknown option: ${1}"
                ;;
            *)
                if [[ -n "${1}" ]]; then
                    positional_args+=("${1}")
                fi
        esac
        shift
    done

    case $subcommand in
        show)
            eval_lib_function ${LIB_PATH}/ipman-show "${dynamic_config}" "${positional_args[@]}"
            ;;
        get-primary)
            eval_lib_function ${LIB_PATH}/ipman-get-primary "${dynamic_config}"
            ;;
        set)
            eval_lib_function ${LIB_PATH}/ipman-set "${dynamic_config}" "${positional_args[@]}"
            ;;
        status)
            eval_lib_function ${LIB_PATH}/ipman-status "${dynamic_config}" "${positional_args[@]}"
            ;;
        wait-for-init)
            eval_lib_function ${LIB_PATH}/ipman-wait-for-init
            ;;
        -h|--help)
            help
            ;;
        *)
            fail "ipman got unknown subcommand: $subcommand"
            ;;
    esac
}

main "$@"
