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

. /usr/lib/lh-migration.sh

LH_PATH="$.SystemConfig.Services"

function migrate_password() {
    logger "Migrating password"
    local PWD="$(lh_filter $LH_PATH.System.SystemPassword)"
    [ -n "$PWD" ] && echo -e "${PWD}\n${PWD}" | passwd
}

function migrate_led() {
    local uci_path="system"
    local AlwaysOff="$(lh_filter $LH_PATH.LedsAndButtons.Leds.AlwaysOff)"
    logger "Migrating leds (AlwaysOff=$AlwaysOff)"
    if [ "$AlwaysOff" = "true" ]; then
        local PLC_LED_SCHEME="off"
        local DISABLE_LEDS="1"
    elif [ "$AlwaysOff" = "false" ]; then
        local PLC_LED_SCHEME="on"
        local DISABLE_LEDS="0"
    else
        return
    fi
    config_load plc
    uci set plc.plc.led_scheme=$PLC_LED_SCHEME
    uci_commit plc

    uci set $uci_path.led_wlan.disabled='0'
    uci set $uci_path.led_wlan.disableLeds=$DISABLE_LEDS
    uci set $uci_path.led_plcw.disabled='0'
    uci set $uci_path.led_plcr.disabled='0'
}

function migrate_timezone() {
    local uci_path="system.@system[0]"
    local TIMEZONE ZONENAME
    local GMTOffset=$(lh_filter $LH_PATH.NTPClient.GMTOffset)
    logger "Migrating timezone $GMTOffset"
    case "$GMTOffset" in
        "-12:00")
            TIMEZONE='UTC+12'
            ZONENAME='UTC-12'
        ;;
        "-11:00")
            TIMEZONE='UTC+11'
            ZONENAME='UTC-11'
        ;;
        "-10:00")
            TIMEZONE='UTC+10'
            ZONENAME='UTC-10'
        ;;
        "-09:00")
            TIMEZONE='UTC+9'
            ZONENAME='UTC-9'
        ;;
        "-08:00")
            TIMEZONE='UTC+8'
            ZONENAME='UTC-8'
        ;;
        "-07:00")
            TIMEZONE='UTC+7'
            ZONENAME='UTC-7'
        ;;
        "-06:00")
            TIMEZONE='UTC+6'
            ZONENAME='UTC-6'
        ;;
        "-05:00")
            TIMEZONE='UTC+5'
            ZONENAME='UTC-5'
        ;;
        "-04:00")
            TIMEZONE='UTC+4'
            ZONENAME='UTC-4'
        ;;
        "-03:30")
            TIMEZONE='UTC+3:30'
            ZONENAME='UTC-3:30'
        ;;
        "-03:00")
            TIMEZONE='UTC+3'
            ZONENAME='UTC-3'
        ;;
        "-02:00")
            TIMEZONE='UTC+2'
            ZONENAME='UTC-2'
        ;;
        "-01:00")
            TIMEZONE='UTC+1'
            ZONENAME='UTC-1'
        ;;
        "+00:00")
            TIMEZONE='GMT0BST,M3.5.0/1,M10.5.0'
            ZONENAME='Europe/London'
        ;;
        "+01:00")
            TIMEZONE='CET-1CEST,M3.5.0,M10.5.0/3'
            ZONENAME='Europe/Berlin'
        ;;
        "+02:00")
            TIMEZONE='EET-2EEST,M3.5.0/3,M10.5.0/4'
            ZONENAME='Europe/Sofia'
        ;;
        "+03:00")
            TIMEZONE='MSK-3'
            ZONENAME='Europe/Moscow'
        ;;
        "+03:30")
            TIMEZONE='UTC-3:30'
            ZONENAME='UTC+3:30'
        ;;
        "+04:00")
            TIMEZONE='SAMT-4'
            ZONENAME='Europe/Samara'
        ;;
        "+05:00")
            TIMEZONE='UTC-5'
            ZONENAME='UTC+5'
        ;;
        "+05:30")
            TIMEZONE='UTC-5:30'
            ZONENAME='UTC+5:30'
        ;;
        "+05:45")
            TIMEZONE='UTC-5:45'
            ZONENAME='UTC+5:45'
        ;;
        "+06:00")
            TIMEZONE='UTC-6'
            ZONENAME='UTC+6'
        ;;
        "+06:30")
            TIMEZONE='UTC-6:30'
            ZONENAME='UTC+6:30'
        ;;
        "+07:00")
            TIMEZONE='UTC-7'
            ZONENAME='UTC+7'
        ;;
        "+08:00")
            TIMEZONE='UTC-8'
            ZONENAME='UTC+8'
        ;;
        "+09:00")
            TIMEZONE='UTC-9'
            ZONENAME='UTC+9'
        ;;
        "+09:30")
            TIMEZONE='UTC-9:30'
            ZONENAME='UTC+9:30'
        ;;
        "+10:00")
            TIMEZONE='UTC-10'
            ZONENAME='UTC+10'
        ;;
        "+10:30")
            TIMEZONE='UTC-10:30'
            ZONENAME='UTC+10:30'
        ;;
        "+11:00")
            TIMEZONE='UTC-11'
            ZONENAME='UTC+11'
        ;;
        "+12:00")
            TIMEZONE='UTC-12'
            ZONENAME='UTC+12'
        ;;
        "+13:00")
            TIMEZONE='UTC-13'
            ZONENAME='UTC+13'
        ;;
        "+14:00")
            TIMEZONE='UTC-14'
            ZONENAME='UTC+14'
        ;;
    esac
    [ -n "$ZONENAME" ] && uci set $uci_path.zonename="$ZONENAME"
    [ -n "$TIMEZONE" ] && uci set $uci_path.timezone="$TIMEZONE"
}

function migrate_timeserver() {
    local uci_path="system.ntp"
    logger "Migrating timeserver"
    local SERVER1=$(lh_filter $LH_PATH.NTPClient.NTPServer)
    local SERVER2=$(lh_filter $LH_PATH.NTPClient.AlternateNTPServer)
    if [ -n "$SERVER1" ]; then
        uci del_list $uci_path.server="europe.pool.ntp.org"
        uci add_list $uci_path.server="$SERVER1"
    fi
    [ -n "$SERVER2" ] && uci add_list $uci_path.server="$SERVER2"
}

function migrate_system() {
    logger "Migrating system"
    config_load system
    migrate_password
    migrate_led
    migrate_timezone
    migrate_timeserver
    uci_commit system
}

migrate_system