#!/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)

log_output=/dev/kmsg

# We create a temporary file in /tmp, store all files in ramdisk

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

# check if config is local, used by webinterface
if [ "$1" = "local_config" ]; then
    # remove source information in case previous config was received from
    # network
    rm "$tmpdir"/dot-config_*
    echo "local" > "$tmpdir"/dot-config_source

    # Check and complain
    if [ -f /wr/etc/dot-config ]; then
	# source dot-config
	. /wr/etc/dot-config
	ret_source=$?
	/wr/bin/wrs_checkcfg /wr/etc/dot-config /wr/etc/Kconfig
	ret_check=$?
	configured=true
	if [ $ret_source != 0 ] || [ $ret_check != 0 ]; then
	    # errors in local dot-config
	    echo "check_error" > "$tmpdir"/dot-config_status
	    echo "Errors found in local dot-config!!!" >& 2
	else
	    echo "config_ok" > "$tmpdir"/dot-config_status
	fi
    else
	echo "No /wr/etc/dot-config to use" | tee $log_output >& 2
	echo "no_config" > "$tmpdir"/dot-config_source
	echo "config_error" > "$tmpdir"/dot-config_status
	configured=false
    fi
elif [ -f /wr/etc/dot-config ]; then
    # if config was not touched by webinterface simply source it, the check was
    # done by /etc/init.d/dot-config
    . /wr/etc/dot-config
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
    mv $T /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
mv $T /etc/resolv.conf

# rsyslog.conf is created from a template file, and busybox sed has -i
cp /wr/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

if [ ! -z "$CONFIG_LOCAL_SYSLOG_FILE" ]; then
    sed -i '/$outchannel/ s/^##//' $T
    sed -i '/omfile/ s/^##//' $T
    sed -i "s|/tmp/syslog|$CONFIG_LOCAL_SYSLOG_FILE|" $T
    sed -i "s|/tmp/syslog|$CONFIG_LOCAL_SYSLOG_FILE|" $T
fi

mv $T /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
# call assembly_snmpd_conf.sh to fill more advanced SNMP parameters
/wr/bin/assembly_snmpd_conf.sh $T
mv $T /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" /etc/ppsi.conf
elif [ "$CONFIG_PTP_REMOTE_CONF" = "y" ]; then

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

    tmpconfig="$tmpdir"/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
    host_name=`hostname`
    URL=$(echo $CONFIG_PTP_CONF_URL | \
	sed -e s/MACADDR/$macaddr/g -e s/IPADDR/$ipaddr/g -e s/HOSTNAME/$host_name/g)
    # 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\"" | tee $log_output >& 2
	    ;;
    esac
    if [ -f $tmpconfig ]; then
	# copy it in place to use the new file
	mv $tmpconfig /etc/ppsi.conf
    fi
else
    # no valid PTP option keep ppsi.conf with old postfix
    echo "No valid PTP option in dot-config!" | tee $log_output >& 2
fi

# Configure processes to be monitored by monit
# It creates a symbolic link with extension .enabled for all processes to monitor 
rm -f /etc/monit.d/*.enabled
if [ "$CONFIG_MONIT_DISABLE" != "y" ]; then
    # Evaluate all processes that must be monitored 
	monit_configs=`ls /etc/monit.d/*.conf`
	if [ -n "$monit_configs" ]; then 
		for config in $monit_configs ;  do
			process=`basename $config .conf`
			monitor=1
			case $process in
			  lldpd)    if [ "$CONFIG_LLDPD_DISABLE" = "y" ] ; then
			  				monitor=0
			 			fi 
			 			;;
			  nslcd)    if [ "$CONFIG_LDAP_ENABLE" != "y"   ] ; then 
			  				monitor=0;	
			  			fi 
			  			;;
			  lighttpd) if [ "$CONFIG_HTTPD_DISABLE" = "y" ] ; then
			  				monitor=0
			  			fi 
			  			;;
			  radiusvlan) if [ "$CONFIG_RVLAN_ENABLE" != "y" ] ; then
							monitor=0
			  			fi 
			  			;;
			  *) ;; 
			esac
			if [[ $monitor = 1 ]] ; then 
				ln -s $config  $config.enabled 
			fi
		done
	fi
fi
#if monit is running, force it to re-read its configuration
#monit_pid=/var/run/monit.pid
#if [ -f $monit_pid ] ; then
#	kill -SIGHUP `cat $monit_pid`
#fi
 
# System clock monitor: program the cron tab
/etc/init.d/system_clock_monitor -s

