#!/bin/sh
# Copyright (c) 2010-2019 Algo Communication Products Ltd.
# Network startup script

/opt/algo/bin/setup-resolvconf initial_setup

READCONF=/opt/algo/bin/readconf
DHCP_TIMEOUT=$($READCONF net.dhcp.timeout)
USE_VLAN=$($READCONF net.vlan.use)
PLATFORM=$($READCONF product.platform)
STATUS=/run/algo-vlan.status
USE_8021X=$($READCONF net.pnac.use)
TMPL_8021X=/opt/algo/info/wpa-supplicant.conf.tmpl
CONF_8021X=/tmp/wpa-supplicant.conf

# For back compatiblity
if [ "$USE_VLAN" = "0" ]; then
	USE_VLAN="none"
elif [ "$USE_VLAN" = "1" ]; then
	USE_VLAN="manual"
fi

/sbin/ifconfig eth0 up

# wait for link ready up to 10 seconds
timeout 10 sh -c ' \
	while true; do \
		LINK=$(ethtool eth0 | \
			grep "Link detected" | \
			cut -d " " -f 3); \
		if [ "$LINK" = "yes" ]; then \
			break; \
		fi; \
		echo link not ready; sleep 1; \
	done'

# Start VLAN Configuration
if [ "$USE_VLAN" = "auto" ]; then
	echo -n "VLAN Mode: Auto; " >> $STATUS

	LLDPD=/opt/algo/bin/algo-lldpd
	LLDPD_DESC="$(cat /opt/algo/info/firmware-version)"
	CONF_VENDOR=$($READCONF product.vendor)
	CONF_PROD=$($READCONF product.name)
	LLDPD_PLATFORM="$CONF_VENDOR"-"$CONF_PROD"
	DAEMON_ARGS="-ccccc -l -M 3 -S \"$LLDPD_DESC\" -P \"$LLDPD_PLATFORM\""

	# Start LLDPD
	$LLDPD $DAEMON_ARGS

	# Try to get an LLDP/CDP packet with VLAN configurations for 5 seconds
	# lldpcli update force sends CDPV2 and LLDP packets
	timeout 5 sh -c ' \
		while true; do \
			if /opt/algo/bin/lldpcli show neighbors details | \
			     grep -q " VLAN: \{8,\}[0-9]"; then \
				echo "VLAN detected"; \
				touch /run/vlan-detection.flag; \
				break; \
			fi; \
			/opt/algo/bin/lldpcli update >/dev/null
			sleep 1; \
		done'

	# Set VLAN_ID and VLAN_PCP variables to detected values
	# If VLAN is detected by CDP, PCP will not be set
	# so we default to configuration
	if [ -f /run/vlan-detection.flag ]; then
		VLAN_ID=$(/opt/algo/bin/lldpcli show neighbors details \
			| awk '/ VLAN: +[0-9]/ {print $2+0}')
		if /opt/algo/bin/lldpcli show neighbors details | \
			  grep -q ' PCP: \{8,\}[0-9]'; then \
			VLAN_PCP=$(/opt/algo/bin/lldpcli show neighbors \
			details | awk '/ PCP: +[0-9]/ {print $2+0}')
		else
			VLAN_PCP=$($READCONF net.vlan.priority)
		fi
		echo -n "VLAN Detected; " >> $STATUS
		echo -n "VLAN ID: $VLAN_ID; " >> $STATUS
		echo -n "VLAN Priority: $VLAN_PCP; " >> $STATUS
	else
		echo -n "VLAN not found; " >> $STATUS
	fi

	# Stop LLDPD
	pkill algo-lldpd
elif [ "$USE_VLAN" = "manual" ]; then
	# Use manual VLAN configuration
	echo -n "VLAN Mode: Manual; " >> $STATUS
	VLAN_ID=$($READCONF net.vlan.id)
	VLAN_PCP=$($READCONF net.vlan.priority)
	echo -n "VLAN ID: $VLAN_ID; " >> $STATUS
	echo -n "VLAN Priority: $VLAN_PCP; " >> $STATUS
else
	echo -n "VLAN Mode: None; " >> $STATUS
fi
# End VLAN detection

if [ "$PLATFORM" = "dailly" ]; then
	/opt/algo/bin/bootui showtxt 22 "Initializing network ..."
fi

if [ -n "$VLAN_ID" ]; then
	NETIF=eth0.$VLAN_ID
	/sbin/vconfig add eth0 $VLAN_ID
	for SKB in 0 1 2 3 4 5 6 7; do
		/sbin/vconfig set_egress_map $NETIF $SKB $VLAN_PCP
	done
	echo use_vlan: $USE_VLAN
	echo vlan_id: $VLAN_ID
	echo vlan_pcp: $VLAN_PCP

	/sbin/ifconfig $NETIF up
	# wait for link ready up to 10 seconds
	for i in $(seq 1 10); do
		LINK=$(ethtool $NETIF | grep "Link detected" | cut -d " " -f 3)
		if [ "$LINK" = "yes" ]; then
			break
		fi
		echo "link not ready"
		sleep 1
	done
	ethtool $NETIF | grep "Link detected"
else
	NETIF=eth0
fi

echo network interface $NETIF
mkdir -p /run/algo-netman
echo "$NETIF" > /run/algo-netman/ifname


# Run 802.1x authentication
if [ "$USE_8021X" = "1" ]; then
	ID_8021X=$($READCONF "net.pnac.id")
	PWD_8021X=$($READCONF "net.pnac.pwd")

	sed -e "s/#WPA_ID#/$ID_8021X/; s/#WPA_PWD#/$PWD_8021X/" \
		$TMPL_8021X > $CONF_8021X

	wpa_supplicant -B -c $CONF_8021X -D wired -i eth0
fi

USE_DHCP=$($READCONF net.dhcp.use)

FAILSAFE_IP=192.168.1.111
FAILSAFE_NETMASK=255.255.255.0
if [ "$USE_DHCP" = "1" ]; then
	mkdir -p /run/algo-dhcp
	rm -rf /run/algo-dhcp/*

	if [ "$($READCONF net.dhcp.c.dns)" = "1" ]; then
		echo "$($READCONF net.dns1) $($READCONF net.dns2)" > \
			/run/algo-dhcp/c-dns
	fi

	if [ "$($READCONF net.dhcp.c.gateway)" = "1" ]; then
		echo "$($READCONF net.gateway)" > \
			/run/algo-dhcp/c-gateway
	fi

	if [ "$($READCONF net.dhcp.c.ntp)" = "1" ]; then
		echo "$($READCONF net.time1) $($READCONF net.time2) \
			$($READCONF net.time3) $($READCONF net.time4)" > \
			/run/algo-dhcp/c-ntp
	fi

	dhclient -v -pf /run/algo-dhcp/client.pid \
		-lf /run/algo-dhcp/client.lease \
		-cf /opt/algo/cache/dhclient.conf \
		-sf /opt/algo/bin/dhcpproc \
		$NETIF
	if [ ! -f /run/algo-dhcp/valid.flag ]; then
		echo $FAILSAFE_IP > /var/run/valid_ip.flag
		/sbin/ifconfig $NETIF $FAILSAFE_IP netmask $FAILSAFE_NETMASK
		/sbin/route add -net 0.0.0.0 gw $FAILSAFE_IP
	else
		if [ ! -f /run/algo-dhcp/c-ntp ] && \
			[ -s /run/algo-dhcp/option42.status ]; then
			cp /opt/algo/info/ntp.conf.tmpl \
				/var/lib/ntp/ntp.conf.dhcp
			for SERVER in $(cat /run/algo-dhcp/option42.status); do
				echo "server $SERVER iburst" >> \
					/var/lib/ntp/ntp.conf.dhcp
			done
		fi
	fi
else
	IPADDR=$($READCONF net.ip)
	[ -n "$IPADDR" ] || IPADDR=$FAILSAFE_IP
	NETMASK=$($READCONF net.mask)
	[ -n "$NETMASK" ] || NETMASK=$FAILSAFE_NETMASK
	GATEWAY=$($READCONF net.gateway)
	[ -n "$GATEWAY" ] || GATEWAY=$IPADDR
	/sbin/ifconfig $NETIF $IPADDR netmask $NETMASK
	/sbin/route add -net 0.0.0.0 gw $GATEWAY
	DNS=$($READCONF net.dns1)
	[ -n "$DNS" ] && echo "nameserver $DNS" >> /etc/resolv.conf
	DNS=$($READCONF net.dns2)
	[ -n "$DNS" ] && echo "nameserver $DNS" >> /etc/resolv.conf
	echo $IPADDR > /var/run/valid_ip.flag
fi

ETH_PHY=$($READCONF net.eth.phy)

if [ -n "$ETH_PHY" ]; then
	ethtool -s eth0 $ETH_PHY
fi

ip route add 224.0.0.0/4 dev $NETIF

if [ "$PLATFORM" = "dailly" ]; then
	IPADDR=$(ifconfig $NETIF | sed -n 's/.*inet *addr:\([0-9\.]*\).*/\1/p')
	NETMASK=$(ifconfig $NETIF | sed -n 's/.*Mask:\([0-9\.]*\).*/\1/p')
	/opt/algo/bin/bootui showtxt 24 "IP: $IPADDR, Mask: $NETMASK"
fi
