#!/bin/sh
#
# Start the network....
#
me="[$(printf $0 | xargs basename)]"
# Load ipmodule variables
source /opt/gira/share/devicestack/ipmodule-vars

MAX_LOG_SIZE=1000000	# 1M

if [ -e "${NETLOG}" ]; then
	LOGSIZE=`ls -la ${NETLOG} | awk '{ print $5}'`
	if [ "${LOGSIZE}" -gt "${MAX_LOG_SIZE}" ]
	then
		rm -f "${NETLOG}"
	fi
fi

touch ${NETLOG}

# Cleanup caches, free memory.
# In good hope that this is a workaround for network issues.
# It sometimes happens that after changing ip settings you get these messages:
#	ifconfig: page allocation failure: order:4, mode:0xd0
#	eth0: Unable to allocate DMA memory (error -12)
#	ifconfig: SIOCSIFFLAGS: Cannot allocate memory
#	route: SIOCADDRT: Network is down
#	ifconfig: page allocation failure: order:4, mode:0xd0
#	eth0: Unable to allocate DMA memory (error -12)
#	ifconfig: SIOCSIFFLAGS: Cannot allocate memory
#	route: SIOCADDRT: Network is down
#	route: SIOCADDRT: Network is unreachable
# Network is down and unreachable thereafter.
logger "[S16network]Freeing pagecache, dentries and inodes!"
sync && printf "3\\n" > /proc/sys/vm/drop_caches

start() {
	printf "Starting network \\n"
	printf "Starting network ...\\n" >> ${NETLOG}

	# prepare
	touch ${RESOLV_CONF}
	rm -f ${SERVER_ID}
	if [ ! -f ${NETWORK_CFG_FILE} ]; then
		printf "%s not found copying template.\\n" "${NETWORK_CFG_FILE}"
		cp ${NETWORK_CFG_FILE_TEMPLATE} ${NETWORK_CFG_FILE}
	fi

	# source config file
	source ${NETWORK_CFG_FILE}

	# set new hostname
	HOSTNAME=`${DEVICESTACK} --host-name`
	logger "Setting hostname to ${HOSTNAME}"
	hostname ${HOSTNAME}

	# init eth0
	ifconfig eth0 up >> ${NETLOG}

	# Reconfigure multicast routes
	route del -net 224.0.0.0 netmask 240.0.0.0 2> /dev/null
	route add -net 224.0.0.0 netmask 240.0.0.0 eth0

	# clean up processes
	killall -q udhcpc
	killall -q zcip

	# check dhcp mode
	if [ "${MODE}" = "DHCP" ]; then
		${IPC_SEND} netcfg-requesting-dhcp-address
		udhcpc -x hostname:"${HOSTNAME}" -b -s ${DHCP_SCRIPT} >> ${NETLOG}
		printf "Starting Network done.\\n" >> ${NETLOG}
		exit 0
	fi

	# configure static network
	ifconfig eth0 ${IP} netmask ${NETMASK} >> ${NETLOG}
	route add -net 224.0.0.0 netmask 240.0.0.0 eth0
	route del default 2> /dev/null
	if [ "${GATEWAY}" != "" ] && [  "${GATEWAY}" != "0.0.0.0" ];	then
		route add default gw ${GATEWAY}
	fi

	# set nameserver 1
	if [ "${NAMESERVER1}" != "" ]; then
		printf "nameserver %s\\n" "${NAMESERVER1}" >> ${RESOLV_CONF}
	fi

	# set nameserver 2
	if [ "${NAMESERVER2}" != "" ]; then
		printf "nameserver %s\\n" "${NAMESERVER2}" >> ${RESOLV_CONF}
	fi

	${IPC_SEND} netcfg-static-address-configured

	printf "Starting network done. \\n"
	printf "Starting Network done.\\n" >> ${NETLOG}
}

stop() {
	printf "Stopping network ...\\n"
	printf "Stopping network ...\\n" >> ${NETLOG}

	${IPC_SEND} netcfg-unconfigured

	# Cleaning up routes
	route del -net 224.0.0.0 netmask 240.0.0.0 2> /dev/null
	route del default 2> /dev/null

	# Killing processes
	killall -q udhcpc

	# Removing unused files
	rm -rf ${RESOLV_CONF}

	# shut down interface
	ifconfig eth0 down >> ${NETLOG}

	printf "Stopping Network done.\\n"
	printf "Stopping Network done.\\n" >> ${NETLOG}
}

restart() {
	stop
	start
}

case "$1" in
	start)
  		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	*)
		printf "Usage: %s {start|stop|restart}\\n" "$0"
		exit 1
esac

exit 0

