#!/bin/sh

# Remember to set up the network interface card with IRQ and base address
# in syslinux.cfg if nessesary.

#
# Grabbing the config.
#
. /etc/config

#
# Figure out about extra inside interfaces.
#
if [ -z "$INSIDE_EXTRA_IF" ]; then
	INSIDE_EXTRA_IF=`set | grep '^INSIDE_' | grep '_DEV=' | sed 's/^INSIDE_\([A-Z0-9]\+\)_DEV.*/\1/' | grep -v '^INSIDE_' | sort -u`
fi
# Print out the result nicely
INSIDE_EXTRA_IF=`echo $INSIDE_EXTRA_IF`
echo "Extra inside interfaces: ${INSIDE_EXTRA_IF:-(none)}"

ifconfig lo 127.0.0.1

#
# Inside:
#
ifconfig ${INSIDE_DEV} ${INSIDE_IP} netmask ${INSIDE_NETMASK} broadcast ${INSIDE_BROADCAST}
if [ -n "$INSIDE_EXTRA_IF" ]; then
	for i in $INSIDE_EXTRA_IF; do
		eval "ifconfig \${INSIDE_${i}_DEV} \${INSIDE_${i}_IP} netmask \${INSIDE_${i}_NETMASK} broadcast \${INSIDE_${i}_BROADCAST}"
	done
fi

#
# Brad and Jonas wants this.
#
set | grep '^INSIDE_' > /etc/inside.info

#
# setting up /etc/hosts
#
echo "${INSIDE_IP} ${HOSTNAME}.${DOMAIN}  ${HOSTNAME}" >> /etc/hosts
if [ -n "$INSIDE_EXTRA_IF" ]; then
	for i in $INSIDE_EXTRA_IF; do
		eval "echo \"\${INSIDE_${i}_IP} ${HOSTNAME}.\${DOMAIN}  \${HOSTNAME}\" >> /etc/hosts"
	done
fi
# setting up hostname
hostname ${HOSTNAME}
hostname -d ${DOMAIN}
echo "Hostname (fully qualified) set up to `hostname -f`"

#
# Tip from Jacco Kok.
#
if [ "$OUTSIDE_MAC" != "" ]; then
	/bin/ifconfig ${OUTSIDE_DEV} hw ether ${OUTSIDE_MAC}
fi

#
# Outside
#
if [ ${OUTSIDE_IP} = 'DHCP' ]; then
	echo "Booting udhcpc"
	echo "OUTSIDE_DEVICE=${OUTSIDE_DEV}"   > /etc/outside.info
	HARGS=
	[ "${HOSTNAME}" != "" ] && HARGS="-H ${HOSTNAME}"
	if /bin/udhcpc -n -s /etc/udhcpcrenew.sh ${HARGS} -i ${OUTSIDE_DEV}; then
		. /etc/outside.info
	else
		echo "duh!"     # Or some more useful error handling
	fi
else
    if [ ${OUTSIDE_IP} = 'EXTERNAL' ]; then
	/etc/ext-up.init
    else
	/bin/ifconfig ${OUTSIDE_DEV} ${OUTSIDE_IP} netmask ${OUTSIDE_NETMASK} broadcast ${OUTSIDE_BROADCAST}

	#
	# Default gateway
	#
	/bin/route add default gw ${DEFAULT_GATEWAY} metric 1

	echo "Setting up name server (etc/resolv.conf) "

	echo "domain ${DOMAIN}" >> /etc/resolv.conf
	echo "search ${DOMAIN}" >> /etc/resolv.conf

	echo "nameserver ${NAME_SERVER_IP1}" >> /etc/resolv.conf
	[ -n "${NAME_SERVER_IP2}" ] && echo "nameserver ${NAME_SERVER_IP2}" >> /etc/resolv.conf

	set | grep '^OUTSIDE_' > /etc/outside.info

	echo "Setting up firewall rules: "
	/etc/firewall.init
	echo

    fi	# if EXTERNAL
fi	# if DHCP

echo "1" > /proc/sys/net/ipv4/tcp_syncookies

if [ -f /proc/sys/net/ipv4/conf/all/rp_filter ] ; then
	echo "Enabling anti spoofing: "

	for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
		echo -n " $f "
		echo 1 > $f
	done
else
	echo "Anti spoofing is not available, the author of this floppy spoofed, mail him."
fi

#
# No umask so it ends up with 600 with both dhcp and PPPoE
# I'm lazy and place it here.
chmod 644 /etc/resolv.conf
#
# DHCP Daemon and DNS Cache.
#
if [ "${DHCP_DAEMON}" = "y" ]; then
	/etc/udhcpd.conf.sh
	udhcpd
	DNSMASQ="y"
fi
p=`pidof dnsmasq`
if [ "${DNSMASQ}" = "y" -a -z "$p" ]; then
	if [ -z "$DNSMASQ_DEVICES" ]; then
		DNSMASQ_DEVICES="${INSIDE_DEV}"
		if [ -n "$INSIDE_EXTRA_IF" ]; then
			for i in $INSIDE_EXTRA_IF; do
				eval "DNSMASQ_DEVICES=\"$DNSMASQ_DEVICES \${INSIDE_${i}_DEV}\""
			done
		fi
	fi
	dnsmasq -b `echo $DNSMASQ_DEVICES | sed 's/[[:space:]]*\([^[:space:]]\+\)/ -i \1/g'`
fi

# Should we keepalive your connection?
[ -n "$ISP_GATEWAY_IP" ] && ping -i 30 $ISP_GATEWAY_IP > /dev/null &
