#!/bin/sh
[ "$ACTION" = ifup ] || exit 0
[ -f /var/lock/bypass_ready.lock ] || exit 0
if uci -q get bypass.@global[0].global_server >/dev/null;then
	default_device=$(ip route | grep default | awk -F 'dev ' '{print $2}' | awk '{print $1}')
	[ "$default_device" == "$DEVICE" ] && {
		LOCK_FILE_DIR=/var/lock
		[ ! -d ${LOCK_FILE_DIR} ] && mkdir -p ${LOCK_FILE_DIR}
		LOCK_FILE="${LOCK_FILE_DIR}/bypass_ifup.lock"
		if [ -s ${LOCK_FILE} ]; then
			SPID=$(cat ${LOCK_FILE})
			if [ -e /proc/${SPID}/status ]; then
				exit 1
			fi
			cat /dev/null > ${LOCK_FILE}
		fi
		echo $$ > ${LOCK_FILE}
		
		/etc/init.d/bypass restart
		echo "bypass: restart when $INTERFACE ifup" > /dev/kmsg

		rm -rf ${LOCK_FILE}
	}

fi
