#!/bin/sh
#########################################################################################
# 1.2
# Set mode to "normal" in /etc/network/mode as it is the mode we are setting when we 
# copy the box dependent network configuration file (interfaces-knx or interfaces-emss)
# over the system resource (/etc/network/interfaces ). 
#########################################################################################

source /usr/local/bin/update_common

#########################################################################################
# Local variables 
#########################################################################################

S11INITSPECIFIC_VERSION=1.2

CONSOLE=/dev/console

platform=$KNXBOARD

DIR_ETC="etc"
DIR_ETC_GPIO="$DIR_ETC/gpio"
DIR_ETC_INIT_D="$DIR_ETC/init.d"
DIR_ETC_NETWORK="$DIR_ETC/network"

SSHD_CONFIG_FILE="sshd_config"
INTERFACES_FILE="interfaces"

#########################################################################################
# Functions
#########################################################################################
#########################################################################################
Specific_SSHConfig() {

	filepath="/$DIR_ETC"
	destinationfile=$SSHD_CONFIG_FILE

	if [ $platform == $EMSS_BOARD ]; then
		sourcefile="sshd_config-emss"
		badfile="sshd_config-knx"
	else
		sourcefile="sshd_config-knx"
		badfile="sshd_config-emss"
	fi

	# Do something only if a specific file is present
	if [ -e $filepath/$sourcefile ]; then
		cp "$filepath/$sourcefile" "$filepath/$destinationfile" 2>&1 >> $CONSOLE
		rm "$filepath/$sourcefile"  2>&1 >> $CONSOLE
	fi

	# Clean not used file if it exists
	if [ -e $filepath/$badfile ]; then
		rm "$filepath/$badfile"  2>&1 >> $CONSOLE
	fi
}

#########################################################################################
Specific_Network() {

	filepath="/$DIR_ETC_NETWORK"
	destinationfile=$INTERFACES_FILE

	if [ $platform == $EMSS_BOARD ]; then
		sourcefile="interfaces-emss"
		badfile="interfaces-knx"
	else
		sourcefile="interfaces-knx"
		badfile="interfaces-emss"
	fi		

	# Do something only if a specific file is present
	if [ -e $filepath/$sourcefile ]; then
		cp "$filepath/$sourcefile" "$filepath/$destinationfile" 2>&1 >> $CONSOLE
		rm "$filepath/$sourcefile"  2>&1 >> $CONSOLE

		echo "normal" > /etc/network/mode
	fi

	# Clean not used file if it exists
	if [ -e $filepath/$badfile ]; then
		rm "$filepath/$badfile"  2>&1 >> $CONSOLE
	fi
}

#########################################################################################
# Script beginning
#########################################################################################

platform=$(Get_BoardType)

TimeElapsed=`awk '{printf("[%11f]", $1)}' /proc/uptime)`
echo "$TimeElapsed : ----------------------------- $(basename "$0") for $platform box : ($1) - v$S11INITSPECIFIC_VERSION" > $CONSOLE

case "$1" in

  start)
	Specific_SSHConfig
	Specific_Network
	;;

  stop)
	;;

  restart|reload)
	"$0" stop
	"$0" start
	;;

  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
	;;

esac

exit $?
 
