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

[ -f /etc/delos-version ] && . /etc/delos-version

[ -f /usr/share/libubox/jshn.sh ] && . /usr/share/libubox/jshn.sh

dvl_is_developer_firmware() {
    echo "$FIRMWARE_VERSION" | grep -q '\.D'
}

dvl_is_internal_firmware() {
    echo "$FIRMWARE_VERSION" | grep -q '\.[ND]'
}

dvl_is_pts_firmware() {
    [ -e /etc/delos-pts-image-marker ]
}

dvl_check_and_set_wifi_cfg_disabled() {
    if [ -n "$(uci_get wireless wifi0)" ] ; then
        # function only matters if two radios are available
        if [ -n "$(uci_get wireless wifi1)" ] ; then
            # both cfg_disabled options must be set from WebUI or App or TR69
            # if both are unset -> switch both radios on
            # if both radios are configured the same -> switch both on
            # if the radios are configured different -> leave the options as they are
            if [ "$(uci_get wireless wifi0 cfg_disabled 0)" = "$(uci_get wireless wifi1 cfg_disabled 0)" ] ; then
                uci_set wireless wifi0 cfg_disabled 0
                uci_set wireless wifi1 cfg_disabled 0
            fi
        fi
    fi
}

dvl_time_to_mseconds() {
    local time="$1"
    # use "$(expr $x + 0)" to remove leading 0, because shell would "08" or "09" interpret as illegal octal number
    local hours=$(expr ${time%%:*} + 0)
    local minutes=$(expr ${time##*:} + 0)

    local mseconds=$(( ( $hours * 3600 + $minutes * 60 ) * 1000 ))

    echo "$mseconds"
}

dvl_make_default_ssid() {
    local vap="$1"

    local format="${_DVL_SSID_FORMAT:-sn-3}"
    local src="${format%-*}"
    local num="${format##*-}"

    local prefix
    case "$vap" in
        main)
            prefix="${_DVL_SSID_MAIN_PREFIX:-devolo-}"
            ;;
        guest)
            prefix="${_DVL_SSID_GUEST_PREFIX:-devolo-guest-}"
            ;;
        *)
            prefix=""
            ;;
    esac

    local base
    case "$src" in
        sn)
            base="$(uci_get_state delos baptization SerialNumber)"
            ;;
        wifimac)
            base="$(uci_get_state delos baptization WiFiMacAddress)"
            [ -z "$base" ] && base="$(uci_get_state delos baptization WiFiMacAddress0)"
            base=$(echo "${base//:/}" | tr 'A-Z' 'a-z')
            ;;
        *)
            base=""
            ;;
    esac

    echo "${prefix}${base:$((-$num))}"
}

dvl_check_oemvariant() {
    (
        local trailer="$1"

        local result=1
        local bap="$(uci_get_state delos baptization OemVariant)"
        local t keys k v

        json_load "$trailer"
        json_get_type t "oem_variants"

        if [ "$t" = "array" ]; then
            json_select "oem_variants"

            json_get_keys keys
            for k in $keys; do
                json_get_var v "$k"
                [ "$bap" = "$v" ] && result=0
            done

            json_select ..
        else
            [ -z "$bap" ] && result=0
        fi

        exit $result
    )
}

dvl_insert_customization_info_file() {
    local filename="$1"

    local variant="$(uci_get customization config active_variant)"
    local vendor_name="$(uci_get customization info vendor_name)"
    local vendor_url="$(uci_get customization info vendor_url)"
    local product_name="$(uci_get customization info product_name)"

    sed -i \
        -e "s~___ACTIVE_VARIANT___~$variant~"     \
        -e "s~___VENDOR_NAME___~$vendor_name~"    \
        -e "s~___VENDOR_URL___~$vendor_url~"      \
        -e "s~___PRODUCT_NAME___~$product_name~"  \
        "$filename"
}

dvl_insert_customization_info() {
    local srcdir="$1"
    local srcfile="$2"
    local dstdir="${3:-${srcdir}}"

    local tmpfile=$(mktemp /tmp/customization.XXXXXX)

    cp -f "$srcdir"/"$srcfile" "$tmpfile"
    dvl_insert_customization_info_file "$tmpfile"
    mv -f "$tmpfile" "$dstdir"/"$srcfile"
}
