#!/bin/sh
#
# Copyright (C) 2018 devolo AG
#

. /usr/lib/lh-migration.sh

function migrate_ap() {
    local lh_path=$1
    local uci_index=$2
    local uci_path="wireless.@wifi-iface[$uci_index]"

    if [ -z "$(lh_filter $lh_path.Role)" ]; then
        uci set $uci_path.disabled=1
        logger "Missing AP $lh_path, disabling ours"
        return 0
    fi

    logger "Migrating AP $lh_path"
    logger "LH data: $(lh_filter $lh_path)"

    uci set $uci_path.ssid="$(lh_filter $lh_path.SSID)"
    local security="$(lh_filter $lh_path.Security)"
    case "$security" in
        none)
            logger "Setting no encryption"
            uci set $uci_path.encryption="none"
            ;;
        WPA)
            local mode="$(lh_filter $lh_path.WPAMode)"
            if [ "$mode" = "both" ]; then
                logger "Setting WPA/WPA2 encryption"
                uci set $uci_path.encryption="psk-mixed+tkip+ccmp"
            elif [ "$mode" = "WPA2" ]; then
                logger "Setting WPA2 encryption"
                uci set $uci_path.encryption="psk2"
            fi
            uci set $uci_path.key="$(lh_filter $lh_path.WPAKey)"
            ;;
    esac
    local VAL=0
    [ "$(lh_filter $lh_path.Active)" != "true" ] && VAL=1
    uci set $uci_path.disabled=$VAL
    [ "$(lh_filter $lh_path.HideSSID)" = "true" ] && uci set $uci_path.hidden=1
    [ "$(lh_filter $lh_path.Role)" = "guest" ] && uci set $uci_path.dvl_guest=1
}

function migrate_radio() {
    local index=$1
    local lh_path=$2
    local uci_path="wireless.wifi$index"

    # Our defaults are radios on
    [ "$(lh_filter $lh_path.WLANRadio)" = "false" ] && {
        uci set $uci_path.disabled=1
        uci set $uci_path.cfg_disabled=1
    }

    # Our defaults are auto channel
    local channel=$(lh_filter $lh_path.Channel)
    # In a special case of genius, LH uses 35 to mean "auto" for 5 GHz radios
    [ ! "$channel" = "0" ] && [ ! "$channel" = "35" ] && uci set $uci_path.channel=$channel
}

function migrate_radio_mode() {
    local lh_path="$.SystemConfig.Services.Wireless"
    local uci_path="wireless"
    local RADIO_MODE="$(lh_filter $lh_path.RadioMode)"
    logger "Migrating RadioMode $RADIO_MODE"
    case $RADIO_MODE in
        all)
            [ "$(lh_filter $lh_path.Radios[0].WLANRadio)" = "false" ] && uci set $uci_path.wifi0.disabled=1
            [ "$(lh_filter $lh_path.Radios[1].WLANRadio)" = "false" ] && uci set $uci_path.wifi1.disabled=1
        ;;
        radio0)
            [ "$(lh_filter $lh_path.Radios[0].WLANRadio)" = "false" ] && uci set $uci_path.wifi0.disabled=1
            uci set $uci_path.wifi1.disabled=1
        ;;
        radio1)
            uci set $uci_path.wifi0.disabled=1
            [ "$(lh_filter $lh_path.Radios[1].WLANRadio)" = "false" ] && uci set $uci_path.wifi1.disabled=1
        ;;
    esac
}

# No pro features, so strictly 2 APs per radio
function migrate_single_radio() {
    logger "Migrating single radio"
    config_load wireless
    migrate_radio 0 "$.SystemConfig.Services.Wireless.Radio"
    migrate_ap "$.SystemConfig.Services.Wireless.Radio.APs[0]" 0
    migrate_ap "$.SystemConfig.Services.Wireless.Radio.APs[1]" 1
    uci_commit wireless
}

function migrate_dual_radio() {
    logger "Migrating dual radio"
    config_load wireless
    migrate_radio_mode
    migrate_radio 0 "$.SystemConfig.Services.Wireless.Radios[0]"
    migrate_radio 1 "$.SystemConfig.Services.Wireless.Radios[1]"
    migrate_ap "$.SystemConfig.Services.Wireless.Radios[0].APs[0]" 0
    migrate_ap "$.SystemConfig.Services.Wireless.Radios[1].APs[0]" 1
    migrate_ap "$.SystemConfig.Services.Wireless.Radios[0].APs[1]" 2
    migrate_ap "$.SystemConfig.Services.Wireless.Radios[1].APs[1]" 3
    uci_commit wireless
}

[ "$1" = "single" ] && migrate_single_radio
[ "$1" = "dual" ] && migrate_dual_radio

