#!/bin/bash

# --------------------------------------------------------------
# -- get old IP from the modem
# --------------------------------------------------------------
/etc/phion/bin/boxconf_rollout.net1 /opt/phion/config/active/boxnet.conf /tmp/boxnetoldconfig
source /tmp/boxnetoldconfig

if [[ ${box_AADSL:-"n"} == "n" ]]; then
	exit 0
fi

# --------------------------------------------------------------
# -- Globals
# --------------------------------------------------------------
PPPOE="$(which pppoe)" || exit 1
IP="$(which ip)" || exit 1

PROGNAME=$(basename $0)
LOGPREFIX="${PROGNAME} [${$}]"
IFLIST=()
IFINUSE=""
IFSAVED=""
NOCHANGE=0
SAVE=0
SAVFILE="/phion0/preserve/boxnet/xDSL/vlanprobe"

DEBUG=0

# --------------------------------------------------------------
# -- Libs & Logging
# --------------------------------------------------------------
PMODPATH="/opt/phion/modules/box/boxnet"
[ -f ${PMODPATH}/moddefs.conf ] && source ${PMODPATH}/moddefs.conf  


MYLOGFLE="${logfile_adsl:-"box_Network_adsl"}"
if [ -f /etc/phion/phion.sh ]; then
	source /etc/phion/phion.sh
fi
if [ -f /etc/phion/functions ]; then
	source /etc/phion/functions
	logerror="LogError $MYLOGFLE"
else
	logerror="echo"
fi

if [[ ! -f ${PMODPATH}/bin/functions.dynnet ]]; then
	$logerror "Fatal" "${LOGPREFIX}: INIT CHECK, operation mode=${MYTASK}, missing functions.dynnet library"	
fi

function logmsg
{
	$logerror ${1} ${2}
	if [[ $DEBUG -eq 1 ]]; then
		DATE=`date +'%Y %m %d %H:%M:%S'`
		echo "${DATE} ${1} - ${2}"
	fi
}

# --------------------------------------------------------------
# -- cleanup
# --------------------------------------------------------------
function cleanup
{
	rm -f /tmp/boxnetoldconfig &> /dev/null
}

# --------------------------------------------------------------
# -- change interface
# --------------------------------------------------------------
function changeif
{
	NEWIF=${1}

	logmsg "Info" "${LOGPREFIX}: changing interface \"${IFLIST[0]}\" from ${IFINUSE} to ${NEWIF}"

	if [[ -n ${IFINUSE} ]]; then
		ifconfig ${IFLIST[0]} down
		ip link set ${IFLIST[0]} name ${IFINUSE}
		if [[ $? -ne 0 ]]; then
			logmsg "Error" "${LOGPREFIX}: can not rename interface \"${IFLIST[0]}\" to \"${IFINUSE}\""
			return 1
		fi
		ifconfig ${IFINUSE} up
	fi

	ifconfig ${NEWIF} down
	ip link set ${NEWIF} name ${IFLIST[0]}
	if [[ $? -ne 0 ]]; then
		logmsg "Error" "${LOGPREFIX}: can not rename interface \"${NEWIF}\" to \"${IFLIST[0]}\""
		return 1
	fi
	ifconfig ${IFLIST[0]} up

	return 0
}

# --------------------------------------------------------------
# -- check connection
# --------------------------------------------------------------
function checkcon
{
	IF=${1}

	logmsg "Info" "${LOGPREFIX}: check connection of ${IF}"
	$PPPOE -I ${IF} -A &> /dev/null
	if [[ $? -ne 0 ]]; then
		return 1
	fi
	
	return 0
}
# --------------------------------------------------------------
# -- find interface in use as pppoe
# --------------------------------------------------------------
function findifinuse
{
	logmsg "Info" "${LOGPREFIX}: search for the actual used interface for \"pppoe\""

	for ((i=1; i<${#IFLIST[*]}; i++)); do
		ifconfig ${IFLIST[$i]} &> /dev/null
		if [[ $? -ne 0 ]]; then
			IFINUSE="${IFLIST[$i]}"
			break
		fi
	done

	if [[ -z ${IFINUSE} ]]; then
		logmsg "Error" "${LOGPREFIX}: no interface in use as ${IFLIST[0]}"
		ifconfig ${IFLIST[0]} &> /dev/null
		if [[ $? -eq 0 ]]; then
			logmsg "Error" "${LOGPREFIX}: Interface \"${IFLIST[0]}\" in use by unknown interface"
			cleanup
			exit 1
		fi
	fi
}

# --------------------------------------------------------------
# -- collect all interfaces
# --------------------------------------------------------------
function findifs
{
	logmsg "Info" "${LOGPREFIX}: collect all needed interface for VLAN probing"
	for ((CONIDX=1; CONIDX<=$box_adsl_N; CONIDX++)); do
		eval NAME=\${box_adsl_NAME_${CONIDX}}
		eval ACTIVE=\${box_adsl_ACT_${CONIDX}:-"n"}
		eval VLPROBE=\${box_adsl_VLANPROBEON_${CONIDX}:-"0"}
		if [[ -n ${NAME} ]] && [[ ${ACTIVE} == "y" ]] && [[ $VLPROBE -eq 1 ]]; then
			IFLIST=("pppoe${CONIDX}")

			eval IFDSL=\${box_adsl_MODEMDEV_${CONIDX}}
			IFBASE=${IFDSL%.*}

			IFLIST+=("${IFBASE}")
			if [[ ${IFBASE} != ${IFDSL} ]]; then
				IFLIST+=("${IFDSL}")
			fi
			break;
		fi
	done

	if [[ ${#IFLIST[*]} -le 1 ]]; then
		rm -f ${SAVFILE} &> /dev/null
		cleanup
		exit 0
	fi

	for ((VLIDX=1; VLIDX<=$box_vlans_N; VLIDX++)); do
		eval IFVL=\${box_vlans_DEV_${VLIDX}}
		eval VID=\${box_vlans_VID_${VLIDX}}
		IFVLVID="${IFVL}.${VID}"
		if [[ ${IFVL} == ${IFBASE} ]]; then
			if [[ ${IFVLVID} == ${IFDSL} ]]; then
				continue;
			fi

			IFLIST+=("${IFVLVID}")
		fi
	done
}

# --------------------------------------------------------------
# -- Main
# --------------------------------------------------------------
while getopts ns opt; do
	case $opt in
		n)	NOCHANGE=1;;
		s)	SAVE=1;;
		*)	;;
	esac
done

[ -f ${SAVFILE} ] && {
	source ${SAVFILE}
}

findifs

findifinuse

if [[ $DEBUG -eq 1 ]]; then
	echo "IFLIST = ${IFLIST[*]}"
	echo "IFINUSE = ${IFINUSE}"
	echo "IFSAVED = ${IFSAVED}"
fi

for ((IDX=0; IDX<${#IFLIST[*]}; IDX++)); do
	if [[ -z ${IFINUSE} ]] && [[ $IDX -eq 0 ]]; then
		continue
	elif [[ ${IFLIST[$IDX]} == ${IFINUSE} ]]; then
		continue
	fi

	checkcon ${IFLIST[$IDX]}
	if [[ $? -eq 0 ]]; then
		if [[ $IDX -ne 0 ]]; then
			if [[ $NOCHANGE -eq 0 ]]; then
				changeif ${IFLIST[$IDX]}
			else
				logmsg "Info" "${LOGPREFIX}: ${IFLIST[0]} Interface should be changed to ${IFLIST[$IDX]}"
			fi
			if [[ $SAVE -eq 1 ]]; then
				logmsg "Info" "${LOGPREFIX}: saving Interface into ${SAVFILE}"
				echo "IFSAVED=${IFLIST[$IDX]}" > ${SAVFILE}
			fi
		else
			if [[ $SAVE -eq 1 ]] && [[ ${IFINUSE} != ${IFSAVED} ]]; then
				logmsg "Info" "${LOGPREFIX}: saving Interface into ${SAVFILE}"
				echo "IFSAVED=${IFINUSE}" > ${SAVFILE}
			fi
		fi
		break
	fi
done

cleanup
exit 0


