#!/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

ifconfig lo 127.0.0.1

#
# Inside:
#
ifconfig ${INSIDE_DEV} ${INSIDE_IP} netmask ${INSIDE_NETMASK} broadcast ${INSIDE_BROADCAST}

#
# Brad wanted these next 5 lines.
#
echo "INSIDE_DEVICE=${INSIDE_DEV}"          > /etc/inside.info
echo "INSIDE_IP=${INSIDE_IP}"               >> /etc/inside.info
echo "INSIDE_NETWORK=${INSIDE_NETWORK}"     >> /etc/inside.info
echo "INSIDE_NETMASK=${INSIDE_NETMASK}"     >> /etc/inside.info
echo "INSIDE_BROADCAST=${INSIDE_BROADCAST}" >> /etc/inside.info

#
# setting up /etc/hosts
#
echo "${INSIDE_IP} ${HOSTNAME}.${DOMAIN}  ${HOSTNAME}" >> /etc/hosts    
# 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
        echo "nameserver ${NAME_SERVER_IP2}" >> /etc/resolv.conf

        echo "OUTSIDE_DEVICE=${OUTSIDE_DEV}"          > /etc/outside.info
        echo "OUTSIDE_IP=${OUTSIDE_IP}"               >> /etc/outside.info
        echo "OUTSIDE_GATEWAY=${DEFAULT_GATEWAY}"     >> /etc/outside.info
        echo "OUTSIDE_NETMASK=${OUTSIDE_NETMASK}"     >> /etc/outside.info
        echo "OUTSIDE_NETWORK=${OUTSIDE_NETWORK}"     >> /etc/outside.info
        echo "OUTSIDE_BROADCAST=${OUTSIDE_BROADCAST}" >> /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. 
#
p=`pidof dnsmasq`
if [ "${DHCP_DAEMON}" = "y" ];
then
	/etc/udhcpd.conf.sh
	udhcpd
        [ $p ] || dnsmasq -i ${INSIDE_DEV}
else
	if [ "${DNSMASQ}" = "y" ];
	then
        	[ $p ] || dnsmasq -i ${INSIDE_DEV}
	fi
fi
