#!/bin/sh
# Custom script to maintain additional IPs

# Program development at projectes_publics/ctctl/ovh-vps/

TargetInterface=eth0
TargetPrimaryIP=1.2.3.4
TargetSecondaryIPs=""

InstalledScript=/etc/network/if-down.d/zz-ovps-secondary-ips
Result=0 ; ResultN=0

if [ "$1" != "install" ] ; then
	EventInterfaces="$IFACE"
	if [ "$EventInterfaces" = "" ] ; then EventInterfaces="$DEVICE_IFACE" ; fi
	if [ "$EventInterfaces" = "--all" ] ; then
		EventInterfaces="$(/sbin/ip link show up | grep -ve '^ ' | cut -f 2 -d ':')"
	fi
	for CurInterface in $EventInterfaces ; do
		if [ "$CurInterface" = "$TargetInterface" ] ; then
			for CurIP in $TargetSecondaryIPs ; do
				/sbin/ip addr del ${CurIP}/32 dev "$CurInterface"
			done
		fi
	done
else
	cat "$0" > "$InstalledScript"
	Result=$? ; if [ $ResultN -eq 0 ] ; then ResultN=$Result ; fi
	chmod +x "$InstalledScript"
	Result=$? ; if [ $ResultN -eq 0 ] ; then ResultN=$Result ; fi
	if [ $ResultN -eq 0 ] ; then
		printf '%s\n' "I: You may want to edit and customize file: $InstalledScript"
	fi
fi

exit $ResultN
