#!/bin/sh

. /lib/functions.sh
. /usr/share/libubox/jshn.sh

POWERLINE=""
[ -d /lib/firmware/plc ] && POWERLINE=plc
[ -d /lib/firmware/ghn ] && POWERLINE=ghn
if [ -z "$POWERLINE" ]; then
    logger "rpcd (network.powerline): No powerline technology found, please deselect this script in the .config for this device"
    echo '{}'
    exit 1
fi

MT=$(uci_get_state delos baptization MT)

# HPAV specific functions

PATCHDIR=/lib/firmware/$POWERLINE/$(cat /tmp/sysinfo/board_name)

find_default_mode_plc() {
    local mode=""
    [ -f "$PATCHDIR"/default_mode_name.txt ] && mode=$(cat "$PATCHDIR"/default_mode_name.txt)
    echo "${mode:-default}"
}

get_mode_plc() {
    uci_get plc.plc.compat_mode
}

get_notches_plc() {
    uci_get plc.plc.user_notches
}

set_and_apply_mode_plc() {
    local mode="$1"

    if [ -z "$mode" ] || [ -f "$PATCHDIR"/patches_mt"$MT"_"$mode".txt ]; then
        uci_set plc plc compat_mode "$mode"
        uci_commit plc
        reload_config
        echo "ok"
    else
        echo "error"
    fi
}

set_and_apply_notches_plc() {
    local notches="$1"

    uci_set plc plc user_notches "$notches"

    uci_commit plc
    reload_config

    echo "ok"
}

make_mode_list_plc() {
    json_add_string "" "$(find_default_mode_$POWERLINE)"

    local pat
    for pat in "$PATCHDIR"/patches_mt"$MT"_*.txt; do
        local mode=$(basename "$pat" .txt | sed -e 's/^patches_mt[0-9]*_//')
        if [ -n "$mode" ] && [ "$mode" != "*" ] && [ "$mode" != "default" ]; then
            json_add_string "" "$mode"
        fi
    done
}

# G.hn specific functions

get_mode_ghn() {
    uci_get ghn.ghn.compat_mode
}

get_notches_ghn() {
    uci_get ghn.ghn.user_notches
}

set_and_apply_mode_ghn() {
    if [ -n "$mode" ]; then
        uci_set ghn ghn compat_mode "$mode"
        uci_commit ghn
        reload_config
        echo "ok"
    else
        echo "error"
    fi
}

set_and_apply_notches_ghn() {
    local notches="$1"

    uci_set ghn ghn user_notches "$notches"

    uci_commit ghn
    reload_config

    echo "ok"
}

find_default_mode_ghn() {
    # our default mode handling is done in the G.hn init script
    echo "-"
}

make_mode_list_ghn() {
    IF=$(uci_get ghn ghn interface)
    DlanMacAddress=$(uci_get_state delos baptization DlanMacAddress | tr a-f A-F)
    PW=$(uci_get_state delos-private baptization PwdGhnConfig)
    COMPAT_MODES_AVAILABLE=$(configlayer -i $IF -m $DlanMacAddress -w $PW -o GET -p DEVOLO.POWERMASK.PROFILES_AVAILABLE | grep DEVOLO.POWERMASK.PROFILES_AVAILABLE | cut -d= -f2- | sed 's/,/ /g')
    for COMPAT_MODE in ${COMPAT_MODES_AVAILABLE}; do
	    json_add_string "" "${COMPAT_MODE}"
    done
}

# common code

case "$1" in
    list)
        echo '{"get_compat_mode":{}, "set_compat_mode":{"mode":"String"}, "get_user_notches":{}, "set_user_notches":{"notches":"String"}}'
    ;;

    call)
        case "$2" in
            get_compat_mode)
                mode=$(get_mode_$POWERLINE)
                [ -z "$mode" ] && mode=$(find_default_mode_$POWERLINE)

                json_init
                json_add_string "current_mode" "$mode"
                json_add_array "available_modes"
                make_mode_list_$POWERLINE
                json_close_array
                json_dump
            ;;

            set_compat_mode)
                read input
                json_load "$input"
                json_get_var mode mode
                [ "$mode" = "$(find_default_mode_$POWERLINE)" ] && mode=""

                result=$(set_and_apply_mode_$POWERLINE "$mode")
                echo '{"result":"'"$result"'"}'
            ;;

            get_user_notches)
                notches=$(get_notches_$POWERLINE)
                echo '{"notches":"'"$notches"'"}'
            ;;

            set_user_notches)
                read input
                json_load "$input"
                json_get_var notches notches

                result=$(set_and_apply_notches_$POWERLINE "$notches")
                echo '{"result":"'"$result"'"}'
            ;;
        esac
    ;;
esac