#!/bin/sh

set -u

source /lib/functions/navico_env.sh
navico_env_init

show_usage() {
    cat >&2 <<EO_USAGE
Usage: ${0##*/} FUNCTION [ARGS]

	FUNCTION	ARGS
	hdmi-output	{enable|disabled-hdcp}
	ethernet	{get-config|set-config <IP>}
	prepare-update	{<path/to/updater>}
	codename
	dump-syslog <output-file>
	dump-dmesg  <output-file>
EO_USAGE
}

show_ethernet_usage() {
    cat >&2 <<EO_USAGE
Usage: ${0##*/} FUNCTION [ARGS]

        FUNCTION	ARGS
        get-config
        set-config	{<IP>}

EO_USAGE
}

IP_INTERFACE_ALIAS="eth0:lwe"
IP_INTERFACE="eth0"

ip_in_range() {
    printf '%s ' "$@" | awk '
        function ip2int(ip) {
            res=0;
            split(ip, a, ".");
            for (n=1 ; n<=4; n++) {
                v=a[n];

                if (v !~ /^[0-9]+$/ || v > 255) {
                    exit 1;
                }

                res=256*res + v;
            }

            return res;
        }

        {
            lo=ip2int($1);
            hi=ip2int($2);
            ip=ip2int($3);

            if ( ip < lo || ip > hi ) exit 2;
        }'
}

ethernet_set() {
    if ip_in_range 172.16.0.0 172.31.255.255 $1; then
        sudo -n /sbin/ip address flush label ${IP_INTERFACE_ALIAS} dev ${IP_INTERFACE}
        sudo -n /sbin/ip address add $1/16 label ${IP_INTERFACE_ALIAS} dev ${IP_INTERFACE}
        return
    fi
    return 2
}

ethernet_get() {
    local ip_addr=$(/sbin/ip address show label ${IP_INTERFACE_ALIAS} | awk -F" " '/inet/ {print substr($2, 0, index($2, "/")-1)}')
    if [ -n "${ip_addr}" ]; then
        echo "${ip_addr}"
        return
    fi
    return 1
}

ethernet () {
    local command=${1:-}

    case $command in
    set-config)
        shift && ethernet_set "$@"
        ;;
    get-config)
        ethernet_get
        ;;
    *)
        printf 'Unsupported argument "%s"\n' "$command"
        show_ethernet_usage
        return 1
        ;;
    esac
}

hdmi_output() {
    local state=${1:-}
    local hdmi_disable=/run/shm/hdmi-output-disable

    case $state in
    enable)
        if [ -e "$hdmi_disable" ]; then
            rm "$hdmi_disable"
            /lib/udev/udev_hdmi_action.sh --probe
        fi
        ;;
    disabled-*)
        echo "$state" > "$hdmi_disable"
        /lib/udev/udev_hdmi_action.sh --probe
        ;;
    *)
        printf 'Unsupported argument "%s"\n' "$state"
        return 1
        ;;
    esac
}

ALL_ARGS="$@"

case ${1:-} in
    hdmi-output)
        shift && hdmi_output "$@"
        ;;
    ethernet)
        shift && ethernet "$@"
        ;;
    prepare-update)
        shift && sudo -n /usr/libexec/nos-hal/prepare-update "$@"
        ;;
    codename)
        echo "${CODENAME:-}"
        ;;
    dump-syslog)
        shift && { logread || cat /var/log/messages || cat /tmp/messages; } 2>/dev/null | tail -c 32768 > "$1"
        ;;
    dump-dmesg)
        shift && /bin/dmesg > "$1"
        ;;
    ""|-h|--help)
        show_usage
        exit 0
        ;;
    *)
        show_usage
        false
        ;;
esac

RESULT=$?
PARENT=$(tr '\0' '\t' </proc/$PPID/cmdline | cut -f1)
logger -t "${0##*/}" -p info -- "$PARENT[$PPID] $ALL_ARGS -> $RESULT"

exit $RESULT
