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

# read baptization config and store it in uci state

. /lib/functions.sh
. /lib/functions/system.sh
. /lib/delos-functions.sh

clear_delos_baptization() {
    uci_toggle_state delos baptization "" baptization
}

# create/clear section
clear_delos_baptization
# transfer all baptization data to uci config file (linux-host legacy devices)
baptization.config 2>/dev/null | while IFS== read key val; do
    if [ "$key" = "DlanMacAddress" -o \
         "$key" = "MacAddress0" -o \
         "$key" = "MacAddress1" ]; then
        val=$(macaddr_canonicalize "$val")
    fi
    uci_toggle_state delos baptization "$key" "$val"
done
# compatibility -> baptization.config to devolo WiFi pro 1xxxy devices
bpt_copy() {
    uci_toggle_state delos baptization $2 $(uci_get_state delos baptization $1)
}
bpt_copy_default() {
    [ -n "$(uci_get_state delos baptization $2)" ] || bpt_copy $1 $2
}
bpt_copy_if_exist() {
    [ -n "$(uci_get_state delos baptization $1)" ] && bpt_copy $1 $2
}
# keep compatibility with devolo WiFi pro 1xxxy and linux-host updated devices
# delos WiFi pro 1xxxy has 'ssh' baptized and linux-host devices have 'Telnetd' baptized
# first copy 'Telnetd' to 'Ssh' then copy 'ssh' only if it exist otherwise 'Ssh' would be deleted
bpt_copy Telnetd Ssh
bpt_copy_if_exist ssh Ssh
bpt_copy shell Shell
bpt_copy DlanDevicePassword SecurityId

# We don't want to copy intermediate linux host baptization from production
if [ "no" == "$(uci_get_state delos baptization ProductionCompleted)" -o "cal_image" == "$(uci_get_state delos baptization OemVariant)" ]; then
    clear_delos_baptization
fi

# override with boot env when existing
( . "./30_uboot-envtools" ) 2>/dev/null
# transfer all baptization data from uboot config to uci config file
# only access delos baptization, ignore other keys
baptization="SecurityId Shell Ssh MT OemVariant SerialNumber
             DeviceType ProductionDate ProductionPTSVersion WiFiMacAddress0
             WiFiMacAddress1 MacAddress0 MacAddress1 DlanMacAddress
             DlanSecurityID DlanFactoryNPW ProductionCompleted FwBootSource
             PwdFwknopd PwdGhnConfig PwdGhnUnlockSector0
             CalibrationMode PS WiFiCountryCode Tr069 ConfigSyncDomain "
for key in $baptization
do
    val=$(fw_printenv -n $key 2>&1)
    case $? in
    0)
        uci_toggle_state delos baptization "$key" "$val"
        ;;
    *)
        #ignore
        ;;
    esac
done
# compatibility -> old to new settings of delos-5
bpt_copy_default SecurityId DlanSecurityID
# backward compatibility, new boot environment overrides all!
bpt_copy DlanSecurityID SecurityId
bpt_copy DlanSecurityID DlanDevicePassword
# leave out ssh,shell, was never used by delos
# derived values:
DlanSecurityID=$(uci_get_state delos baptization DlanSecurityID)
hpavkey 2>/dev/null && uci_toggle_state delos baptization DlanDAK $(hpavkey -D $DlanSecurityID)

# move security ids not to be accessible by ubus to separate config state file
uci_toggle_state delos-private baptization "" baptization
keys="DlanSecurityID SecurityId DlanDevicePassword DlanFactoryNPW DlanDAK
      PwdGhnConfig PwdGhnUnlockSector0 PwdFwknopd"
for key in $keys; do
    uci_toggle_state delos-private baptization $key "$(uci_get_state delos baptization $key)"
    uci_revert_state delos baptization $key
done


# create /etc/delos-image when not supplied by rootfs image (ipq806x target)
devicetype=$(uci_get_state delos baptization DeviceType)
if [ -n "$devicetype" ] && [ ! -e /etc/delos-image ] && [ ! -e /rom/etc/delos-image ]; then
    echo "DEVICE_TYPE=$devicetype" >/etc/delos-image
fi

uci_get delos delos device_type >/dev/null || (
    . /etc/delos-image 2>/dev/null && [ -n "$DEVICE_TYPE" ] && uci_set delos delos device_type "$DEVICE_TYPE"
    uci_commit delos
)

if [ "$(uci_get_state delos baptization Shell)" = "1" ] || dvl_is_internal_firmware || dvl_is_pts_firmware; then
    # console login shell is /tmp/bin/ash... see /etc/inittab
    mkdir -p /tmp/bin
    ln -s /bin/ash /tmp/bin/ash
fi

value="normal"
case "$(uci_get delos delos device_type)" in
    1200wifiac|zuse|turing|hamilton|hamming|valencia)
        bogomips=$(cat /proc/cpuinfo | grep BogoMIPS | sed -e 's/^.*: //' | sed -e 's/\..*$//')
        [ -n "$bogomips" ] && [ "$bogomips" -lt 100 ] && value="slow"
    ;;
esac
uci_toggle_state delos bootloader "" bootloader
uci_toggle_state delos bootloader type "$value"

# keep this file for the next boot
exit 1