#!/bin/sh

#find the eth0:# and add the ip to the system
OS=`uname`

addIPv6()
{
	MASK=/64
	if echo $2 | grep -m1 -q '/'; then
		MASK=$2
	fi

	if [ "${OS}" = "FreeBSD" ]; then
		/sbin/ifconfig $ETH_DEV inet6 add ${1}${MASK}
	else
		/sbin/ip addr add ${1}${MASK} dev $ETH_DEV preferred_lft 0 >/dev/null 2>&1
		if [ "$?" -ne 0 ]; then
			/sbin/ifconfig $ETH_DEV inet6 add ${1}${MASK}
		fi
	fi

	exit 0;
}

getBroadcast() {
        IP1=`echo $1 | cut -d. -f1`;
        IP2=`echo $1 | cut -d. -f2`;
        IP3=`echo $1 | cut -d. -f3`;
        IP4=`echo $1 | cut -d. -f4`;

        NM1=`echo $2 | cut -d. -f1`;
        NM2=`echo $2 | cut -d. -f2`;
        NM3=`echo $2 | cut -d. -f3`;
        NM4=`echo $2 | cut -d. -f4`;

        BC1=$((($IP1 & $NM1) | (255 & ~$NM1)));
        BC2=$((($IP2 & $NM2) | (255 & ~$NM2)));
        BC3=$((($IP3 & $NM3) | (255 & ~$NM3)));
        BC4=$((($IP4 & $NM4) | (255 & ~$NM4)));

        BROADCAST="$BC1.$BC2.$BC3.$BC4";
}

ETH_DEV=eth0
if [ $# -lt 1 ]; then # we need the ip
	echo "Usage: $0 <ip> (<netmask> (<eth dev> (<broadcast>)))";
	echo "example: $0 1.2.3.4 255.255.255.0 eth0";
	exit 1;
fi

IP_FILE=/usr/local/directadmin/data/admin/ips/$1
if [ -s ${IP_FILE} ]; then
	C=`grep -c 'add_to_device=no' ${IP_FILE}`
	if [ "${C}" -gt 0 ]; then
		echo "IP $1 has add_to_device=no set. Skipping"
		exit 0;
	fi
fi

#check to make sure it isn't already running
IP_ALREADY_EXISTS=false
if [ "${OS}" = "FreeBSD" ]; then
	if /sbin/ifconfig | grep -m1 -q " $1 "; then
		IP_ALREADY_EXISTS=true
	fi
else
	if /sbin/ip a | grep -m1 -q " $1/"; then
		IP_ALREADY_EXISTS=true
	fi
fi

if ${IP_ALREADY_EXISTS}; then
	echo "IP $1 already exists"
	exit 1
fi

#echo "have device: $3";
if [ $# -gt "2" ]; then
    ETH_DEV=$3;
fi

if echo $1 | grep -m1 -q ':'; then
	addIPv6 $1 $2
fi

netmaskToPrefixIPv4(){
	NM1=`echo ${NETMASK} | cut -d. -f1`;
	NM2=`echo ${NETMASK} | cut -d. -f2`;
	NM3=`echo ${NETMASK} | cut -d. -f3`;
	NM4=`echo ${NETMASK} | cut -d. -f4`;

	NM1BIN=`perl -e "printf \"%b\n\",${NM1}"`
	NM2BIN=`perl -e "printf \"%b\n\",${NM2}"`
	NM3BIN=`perl -e "printf \"%b\n\",${NM3}"`
	NM4BIN=`perl -e "printf \"%b\n\",${NM4}"`

	echo "${NM1BIN}${NM2BIN}${NM3BIN}${NM4BIN}" | grep -o '1' | wc -l
}

NETMASK=255.255.255.0
PREFIX="/24"
SET_BROADCAST=true
if [ $# -gt "1" ]; then
	#echo "have netmask: $2";
	NETMASK=$2
	if ! echo "${NETMASK}" | grep -m1 -q '/'; then
		PREFIX="/`netmaskToPrefixIPv4 ${NETMASK}`"
	else
		PREFIX="${NETMASK}"
		SET_BROADCAST=false
	fi
fi

if [ $# -gt "3" ]; then
    BROADCAST=$4
elif ${SET_BROADCAST}; then
	getBroadcast $1 $2
fi

if [ "${OS}" = "FreeBSD" ]; then
	ifconfig $ETH_DEV inet $1 netmask $NETMASK broadcast $BROADCAST alias
else
	/sbin/ip addr add ${1}${PREFIX} dev $ETH_DEV >/dev/null 2>&1
	if [ "$?" -ne 0 ] && ${SET_BROADCAST}; then
		DEVNUM=0
		while [ `/sbin/ifconfig $ETH_DEV:$DEVNUM | grep -F -c inet` -gt "0" ]
		do
		{
			DEVNUM=$(($DEVNUM+1));
		}
		done;

		/sbin/ifconfig $ETH_DEV:$DEVNUM $1 netmask $NETMASK broadcast $BROADCAST
		/sbin/route add -host $1 dev $ETH_DEV:$DEVNUM
	fi
fi

exit 0
