#!/bin/sh

# Files
GUEST_NETWORK_PREV="/tmpHost/network_state.prev"
HOST_NETWORK_PREV="${MEDIACASTER_DIR}${GUEST_NETWORK_PREV}"

GUEST_NETWORK_CUR="/tmpHost/network_state.cur"
HOST_NETWORK_CUR="${MEDIACASTER_DIR}${GUEST_NETWORK_CUR}"

# admin-helper not available in host environment
GUEST_NETWORK_CONFIG_FILE="/etc/oe-admin.conf"
HOST_NETWORK_CONFIG_FILE="${MEDIACASTER_DIR}${GUEST_NETWORK_CONFIG_FILE}"

# Executables
IFCONFIG="${MEDIACASTER_DIR}/sbin/ifconfig"
DIFF="${MEDIACASTER_DIR}/usr/bin/diff"
CHROOT="${MEDIACASTER_DIR}/usr/sbin/chroot"
GREP="${MEDIACASTER_DIR}/bin/grep"
AWK="${MEDIACASTER_DIR}/usr/bin/awk"

if [ -x "/sbin/curl" ]; then
	# R7000 has curl in non-standard location
	HOSTCURL="/sbin/curl"
elif [ -x "/usr/bin/curl" ]; then
	HOSTCURL="/usr/bin/curl"
fi

# Network module constants
NETWORK_STATE_UP="UP"
NETWORK_STATE_DOWN="DOWN"
NETWORK_CONNECTIVITY_CHECK_UP_PERIOD="120"
NETWORK_CONNECTIVITY_CHECK_DOWN_PERIOD="10"
NETWORK_CONNECTIVITY_URL="http://www.msftncsi.com/ncsi.txt"

# Network module variables
NETWORK_STATE=${NETWORK_STATE_DOWN}
NETWORK_CHECK_UPTIME="-${NETWORK_CONNECTIVITY_CHECK_DOWN_PERIOD}"

# Interfaces
HOST_WAN_IFACE=`${GREP} ^WANInterface ${HOST_NETWORK_CONFIG_FILE} | ${AWK} '{ print $2 }'`
HOST_LAN_IFACE=`${GREP} ^LANInterface ${HOST_NETWORK_CONFIG_FILE} | ${AWK} '{ print $2 }'`

# Determine the current network state (UP or DOWN)
daemon_network_check_connectivity () {
	# Default previous state and uptime
	local CURRENT_UPTIME=`daemon_utils_uptime`
	local DELTA_UPTIME=$((CURRENT_UPTIME - NETWORK_CHECK_UPTIME))

	local CHECK=0

	if [ "${NETWORK_STATE}" == "${NETWORK_STATE_UP}" ] && [ ${DELTA_UPTIME} -ge ${NETWORK_CONNECTIVITY_CHECK_UP_PERIOD} ]; then
	 	CHECK=1
	elif [ "${NETWORK_STATE}" == "${NETWORK_STATE_DOWN}" ] && [ ${DELTA_UPTIME} -ge ${NETWORK_CONNECTIVITY_CHECK_DOWN_PERIOD} ]; then
		CHECK=1
	fi

	if [ "${CHECK}" == "1" ]; then
		${HOSTCURL} --insecure --head --connect-timeout 10 --max-time 20 ${NETWORK_CONNECTIVITY_URL} >/dev/null 2>&1
		if [ "$?" = "0" ]; then
			NETWORK_STATE="$NETWORK_STATE_UP"
		else
			NETWORK_STATE="$NETWORK_STATE_DOWN"
		fi

		# Save the network connectivity check uptime
		NETWORK_CHECK_UPTIME=${CURRENT_UPTIME}
	fi

	echo "Network is $NETWORK_STATE"

	if [ "$NETWORK_STATE" == "$NETWORK_STATE_UP" ]; then
		return 0
	fi

	return 1
}

daemon_network_calculate_state () {
	# See whether internet is up or down
	daemon_network_check_connectivity

	# Check the interface configurations
	${IFCONFIG} ${HOST_WAN_IFACE} 2>/dev/null | grep -v packets | grep -v bytes |grep -v collisions | grep -v Interrupt | grep -v inet6
	${IFCONFIG} ${HOST_LAN_IFACE} 2>/dev/null | grep -v packets | grep -v bytes |grep -v collisions | grep -v Interrupt | grep -v inet6
}

# Create a temporary network state file with the current network state
daemon_network_get_state () {
	local STATE_FILE=${HOST_NETWORK_CUR}
	if [ "prev" == "$1" ]; then
		STATE_FILE=${HOST_NETWORK_PREV}
	fi
	daemon_network_calculate_state > ${STATE_FILE}
}

# Test if the current network state is different from the previous network state
daemon_network_compare_state () {
	# Get the current network state
	daemon_network_get_state "cur"

	# Compare with the previous state
	${DIFF} -q ${HOST_NETWORK_PREV} ${HOST_NETWORK_CUR} >/dev/null 2>&1
	local RETVAL=$?

	# Make the current state into the previous state
	mv -f ${HOST_NETWORK_CUR} ${HOST_NETWORK_PREV}

	# Return 0 if the network state has not changed, 1 if it has
	return ${RETVAL}
}

# Handle changes in network state
daemon_network_state_changed () {
	daemon_network_check_connectivity

	# If the network is up, restart services
	if [ "$?" == "0" ] ; then
		daemon_log_info "Network changed from DOWN to UP"
		${CHROOT} "$MEDIACASTER_DIR" /manager network_up
	else
		daemon_log_info "Network changed from UP to DOWN"
	fi
}
