#!/bin/sh



usage(){
	echo "Usage: $(basename $0) [-c <config>]"
}

defaultconfig=$(dirname $0)/hosts

TEMP=`getopt -o h,c: --long help,config: -n $(basename $0) -- "$@"`
if [ $? != 0 ] ; then
        usage
	exit 1
fi
eval set -- "$TEMP"

config=$defaultconfig

while true ; do
        case "$1" in
		-h|--help)
			usage; exit 0;
		;;
		-c|--config)
			if [ -f "$2" ] ; then
				config=$2
			else
				echo "Configuration file $2 not found!" >&2
			fi
			shift 2
		;;
		--)
			shift
			break
		;;
		*)
			echo "Internal error!: \"$1\"" >&2
			exit 1
		;;
			
	esac
done

if [ -f "$config" ] ; then
	hosts=$(grep connection $config | perl -ne 's/connection=\s*//; s/-/ /g; print')
	prefix=$(grep prefix $config | perl -ne 's/prefix=\s*//; print')
else
	echo "Config not found." >&2
	exit 1
fi

cmd=""
for j in $hosts ; do
	host=$(echo $j | perl -ne 's/\D+//g; print')
	cmd="$cmd route delete $prefix.$host;"
done

for i in $hosts ; do
	echo -e "\033[01;33mDeleting routes on $i...\033[00m"
	ssh root@$i "$cmd"
done
