#!/bin/sh

# This script applies the dot-config. It is a boot script, but actual
# functionality is moved to a separate binary, so the web interface
# could edit dot-config and run /wr/bin/apply_dot-config like we do here,
# without the need to rember wheter this is S20 or S10 during boot.

# First, read dot-config to get the new location, if any.
dotconfig=/wr/etc/dot-config
tmpconfig=/tmp/dot-config
tmpdir=/tmp
log_output=/dev/kmsg

set -o pipefail # The return value of a pipeline is the status of
                # the last command to exit with a non-zero status,
                # or zero if no command exited with a non-zero status
                # Needed for return value ($?) of xxx in "xxx | tee yyy"


rm -f "$tmpdir"/dot-config_source

if [ -f $dotconfig ]; then
    # source dot-config
    . $dotconfig
    ret_source=$?
    /wr/bin/wrs_checkcfg $dotconfig /wr/etc/Kconfig 2>&1| tee $log_output
    ret_check=$?
    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!!!" | tee $log_output >& 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
fi

# Create /etc/resolv.conf, so we can use it. /etc is ramdisk anyways
if [ ! -z "$CONFIG_DNS_SERVER" ]; then
    echo "nameserver $CONFIG_DNS_SERVER" > /etc/resolv.conf
    if [ ! -z "$CONFIG_DNS_DOMAIN" ]; then
        echo "domain $CONFIG_DNS_DOMAIN" >> /etc/resolv.conf
    fi
fi

# remove "$tmpdir"/dot-config_source_url" in case it is not first run
# of this script
rm -f "$tmpdir"/dot-config_source_url

# If we are expected to get a new dot-conf, do it
if [ "$CONFIG_DOTCONF_SOURCE_REMOTE" = "y" ] || [ "$CONFIG_DOTCONF_SOURCE_TRY_DHCP" = "y" ] \
	|| [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ]; then
    echo "remote" > "$tmpdir"/dot-config_source

    # get URL via DHCP
    if [ "$CONFIG_DOTCONF_SOURCE_TRY_DHCP" = "y" ] \
	    || [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ]; then
	if [ "$CONFIG_DOTCONF_SOURCE_TRY_DHCP" = "y" ]; then
	    echo "try_dhcp" > "$tmpdir"/dot-config_source
	fi
	if [ "$CONFIG_DOTCONF_SOURCE_FORCE_DHCP" = "y" ]; then
	    echo "force_dhcp" > "$tmpdir"/dot-config_source
	fi

	# up eth0 to allow dhcp requests
	ifconfig eth0 up
	# let udhcpc run /wr/bin/dhcp_get_filename.sh script to get "filename"
	# from DHCP (which contain url with dot-config).
	/sbin/udhcpc -i eth0 -s /wr/bin/dhcp_get_filename.sh -O bootfile -o \
		-n -q -f -t 5 &>/dev/null
	if [ -f "$tmpdir"/dot-config_source_url ]; then
	    # replace CONFIG_DOTCONF_URL with one gotten from dhcp
	    CONFIG_DOTCONF_URL=`cat "$tmpdir"/dot-config_source_url`
	    echo "Got dot-config's URL ("$CONFIG_DOTCONF_URL") via DHCP" | tee $log_output >& 2
	else
	    echo "dhcp_error" > "$tmpdir"/dot-config_status
	    echo "Unable to get dot-config's URL via DHCP, using old dot-config" | tee $log_output >& 2
	    # apply old dot-config
	    /wr/bin/apply_dot-config
	    exit
	fi
    fi

    # 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_DOTCONF_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-)

    # save URL of dot-config, to be used by SNMPd
    echo "$URL" > "$tmpdir"/dot-config_source_url
    rm -f $tmpconfig
    case $proto in
	http|ftp)
	    wget $URL -O $tmpconfig
	    ;;
	tftp)
	    tftp -g -r "$filename" -l $tmpconfig $host
	    ;;
	*)
	    echo "Invalid URL for dot-config: \"$URL\"" | tee $log_output >& 2
	    ;;
    esac
    if [ -f $tmpconfig ]; then
	# If it exists, it is not empty or too small and the checker is happy
	# then try to source it in another shell to verify syntax
	if [ $(cat $tmpconfig | wc -c) -gt 200 ] &&
	    /wr/bin/wrs_checkcfg $tmpconfig /wr/etc/Kconfig 2>&1| tee $log_output &&
	    /bin/sh -c ". $tmpconfig"; then
	    echo "Using newly-downloaded dot-config from $URL" | tee $log_output >& 2
	    # copy it in place to use the new file (unless it is identical)
	    cmp -s $tmpconfig $dotconfig || cp $tmpconfig $dotconfig
	    # info for SNMP that downloading was successful and checker is happy
	    echo "config_ok" > "$tmpdir"/dot-config_status
	else
	    echo "check_error" > "$tmpdir"/dot-config_status
	    echo "Errors found in downloaded dot-config \"$URL\", using old" | tee $log_output >& 2
	fi
    else
	echo "download_error" > "$tmpdir"/dot-config_status
	echo "Download error for dot-config \"$URL\", using old" | tee $log_output >& 2
    fi
elif [ "$CONFIG_DOTCONF_SOURCE_LOCAL" = "y" ]; then
    echo "local" > "$tmpdir"/dot-config_source
    echo "Using local dot-config" | tee $log_output >& 2
else
    echo "Unknown dot-config source. Using local dot-config" | tee $log_output >& 2
fi

# Finally, apply what we have, be it old or new
/wr/bin/apply_dot-config
