#!/bin/sh

# This script applies the current dot-config to make
# the choices users wanted.  You can change the dot-config on flash,
# and call this script to apply changes (please note that some changes
# require restarting running processes).  The script is called at
# every boot by /etc/init.d/dot-config

# When called with "local_config" parameter, files with information about
# dotconfig source are removed (used by SNMP)

# We create a temporary file in /tmp, to avoid wearing flash if not
# needed. Then we replace the real file if different.

T=$(mktemp /tmp/config-XXXXXX)

copy_conf() {
    # busybox cmp exits 1 or 2 according to GNU man page
    for dest in $*; do
	cmp -s $T $1 || cp $T $1
    done
}

#check if config is local
if [ "$1" == "local_config" ]; then
	# remove source information in case previous config was received from
	# network
	rm /tmp/dot-config_*
	echo "local" > /tmp/dot-config_source
	#assume that local config is always ok
	echo "config_ok" > /tmp/dot-config_status
fi

# Check and complain, but we need to edit some files even if unconfigured.
if [ -f /wr/etc/dot-config ]; then
    . /wr/etc/dot-config
    configured=true
else
    echo "No /wr/etc/dot-config to use" >& 2
    configured=false
fi

##### Actual configuration actions start here.

# Root password. The default is an empty password (if old dot-config active)
if [ ! -z "$CONFIG_ROOT_PWD_IS_ENCRYPTED" ]; then
    #problem: the password includes '$', so the shell assignment failed.
    passwd=$(busybox awk '-F"' '/PWD_CYPHER/ {print $2}' /wr/etc/dot-config)
else
    # Note: CONFIG_ROOT_PWD_CLEAR may be an empty string
    passwd=$(/wr/bin/mkpasswd --method=md5 "$CONFIG_ROOT_PWD_CLEAR")
fi
sed -i "s,^root:[^:]*:,root:$passwd:," /etc/shadow

# A non-existent wr_date.conf means no NTP. So "rm" if unconfigured
if [ ! -z "$CONFIG_NTP_SERVER" ]; then
    echo "ntpserver $CONFIG_NTP_SERVER" > $T
    copy_conf /wr/etc/wr_date.conf
else
    rm -f /wr/etc/wr_date.conf
fi

# /etc/resolv.conf can be empty, so start empty
> $T
if [ ! -z "$CONFIG_DNS_SERVER" ]; then
    echo "nameserver $CONFIG_DNS_SERVER" >> $T
    if [ ! -z "$CONFIG_DNS_DOMAIN" ]; then
        echo "domain $CONFIG_DNS_DOMAIN" >> $T
    fi
fi
copy_conf /etc/resolv.conf /usr/etc/resolv.conf

# rsyslog.conf is created from a template file, and busybox sed has -i
cp /usr/etc/rsyslog.conf.in $T
if [ "$CONFIG_REMOTE_SYSLOG_UDP" = "y" ]; then
    sed -i 's/@@remote-host/@remote-host/' $T
fi
if [ ! -z "$CONFIG_REMOTE_SYSLOG_SERVER" ]; then
    sed -i '/remote-host/ s/^##//' $T
    sed -i "s/remote-host/$CONFIG_REMOTE_SYSLOG_SERVER/" $T
fi
copy_conf /etc/rsyslog.conf /usr/etc/rsyslog.conf

# Fix SNMP values: for all not-empty configs remove comment and replace value
cp /wr/etc/snmpd.conf.in $T
set | tr -d \' | grep CONFIG_SNMP | sed 's/=/ /' | while read varname value; do
    if [ -z "$value" ]; then continue; fi
    sed -i "/$varname/ s/^#//" $T
    sed -i "/$varname/ s/$varname/$value/" $T
done
copy_conf /wr/etc/snmpd.conf

# Select a ppsi configuration file
if [ "$CONFIG_PTP_PORT_PARAMS" = "y" ]; then
    /wr/bin/assembly_ppsi_conf.sh
elif [ "$CONFIG_PTP_CUSTOM" = "y" ]; then
    cp "$CONFIG_PTP_CUSTOM_FILENAME" /wr/etc/ppsi.conf
elif [ "$CONFIG_PTP_REMOTE_CONF" = "y" ]; then

    # Warning: code below copied from /etc/init.d/dot-config.

    tmpconfig=/tmp/ppsi-config

    # replace IPADDR and MACADDR, to have a device-specific name
    macaddr=$(cat /sys/class/net/eth0/address)
    ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d '  -f 1)
    if [ -z "$ipaddr" ]; then
	# if no IP address available from barebox try to get IP from old dot-config
	# /etc/init.d/network will run later again
	/etc/init.d/network
	ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d '  -f 1)
    fi
    URL=$(echo $CONFIG_PTP_CONF_URL | \
	sed -e s/MACADDR/$macaddr/ -e s/IPADDR/$ipaddr/)
    # split the parts, as we need to handle tftp by hand
    proto=$(echo $URL | cut -d: -f 1)
    host=$(echo $URL | cut -d/ -f 3)
    filename=$(echo $URL | cut -d/ -f 4-)

    rm -f $tmpconfig
    case $proto in
	http|ftp)
	    wget $URL -O $tmpconfig
	    ;;
	tftp)
	    tftp -g -r "$filename" -l $tmpconfig $host
	    ;;
	*)
	    echo "Invalid URL for ppsi.conf: \"$URL\"" >& 2
	    ;;
    esac
    if [ -f $tmpconfig ]; then
	    # copy it in place to use the new file (unless it is identical)
	    cmp -s $tmpconfig /wr/etc/ppsi.conf || \
		cp $tmpconfig /wr/etc/ppsi.conf
    fi
else
    # no valid PTP option keep ppsi.conf with old postfix
    echo "No valid PTP option in dot-config!"
    if [ -f /wr/etc/ppsi.conf ]; then
	mv -f /wr/etc/ppsi.conf /wr/etc/ppsi.conf.old
    fi
fi
