#!/bin/sh
# Begin $network_devices/services/ipv4-static

# Based upon lfs-bootscripts-1.12 $network_devices/if{down,up}
# Rewritten by Nathan Coulson <nathan@linuxfromscratch.org>
# Enhanced by Kevin P. Fleming <kpfleming@linuxfromscratch.org>

. /etc/sysconfig/rc 
. $rc_functions 
. $IFCONFIG

if [ -z "$IP" ]; then
	echo "IP variable missing from $IFCONFIG, cannot continue"
	exit 1
fi

if [ -z "$PREFIX" -a -z "$PEER" ]; then
	echo -n "PREFIX variable missing from $IFCONFIG, "
	echo    "assuming 24"
	PREFIX=24
	args="$args $IP/$PREFIX"
elif [ -n "$PREFIX" -a -n "$PEER" ]; then
	echo "PREFIX and PEER both specified in $IFCONFIG, cannot continue"
	exit 1
elif [ -n "$PREFIX" ]; then
	args="$args $IP/$PREFIX"
elif [ -n "$PEER" ]; then
	args="$args $IP peer $PEER"
fi

if [ -n "$BROADCAST" ]; then
	args="$args broadcast $BROADCAST"
fi


case "$2" in
	up)
		echo "Adding IPV4 address $IP to the $1 interface..."
		ip addr add $args dev $1
		evaluate_retval
	
		if [ -n "$GATEWAY" ]; then
			if ip route | grep -q default; then
				echo "Gateway already setup; skipping..."
				print_status warning
			else
				echo "Setting up default gateway..."
				ip route add default via $GATEWAY dev $1
				evaluate_retval
			 fi
		fi
	;;
	
	down)
		if [ -n "$GATEWAY" ]; then
			echo "Removing default gateway..."
			ip route del default
			evaluate_retval
		fi
	
		echo "Removing IPV4 address $IP from the $1 interface..."
		ip addr del $args dev $1
		evaluate_retval
	;;
	
	*)
		echo "Usage: $0 [interface] {up|down}"
		exit 1
	;;
esac

# End $network_devices/services/ipv4-static
