#!/bin/sh

#  Description:
#
# Swiss army tool to manage natfree virtual interface.
# The script mimics in a simplified way the syntax of the ip command,
# and provides default values for most parameters sourcing files with .cfg extension
# Run the script with option -h or no parameters to get help.
#

###############
# global vars #
###############
SRV_VIRT_IFACE=""
SRV_PHYS_IFACE=""
CIDR=""
ACTION=""
DEBUG=""
FORCE=""

# load common functions
. /usr/share/natfree-common/natfree-common
SRV_PHYS_IFACE="$(get_phys_iface)"
SRV_VIRT_IFACE="$(get_virt_iface)"


#############
# functions #
#############

usage(){
        die "Usage" "$(basename "$0") [ OPTIONS ] COMMAND"  \
            "       OPTIONS:" \
            "           -i PHYS_IFACE : physical interface name (defaults to $SRV_PHYS_IFACE)" \
            "           -v VIRT_IFACE : virtual interface name (defaults to $SRV_VIRT_IFACE)" \
	    "           -f            : force (create interface if the COMMAND require it)" \
	    "           -d            : debug mode (verbose)" \
	    "       COMMAND:" \
	    "           CREATE                   : create interface" \
	    "           DELETE                   : delete interface" \
	    "           SHOW                     : show interface configuration" \
	    "           ADDRESS|SET [CIDR|(-)NUM]: show/set interface network address" \
	    "           UNSET                    : remove interface address" \
	    "           UP                       : bring interface up" \
	    "           DOWN                     : bring interface down" \
            "Command names can be used in lower or upper case and abbreviated down to a single letter." \
	    "Where there is ambiguity, dictionary order is used to select a command."
}

main(){

	# check parameters

	[ "$1" ] || usage
	OPTIONS=""
	while echo "x$1" | grep -q "^x-" ; do
		case "$1" in
			-v)
				[ "$#" -ge 3 ] || usage
				shift
				SRV_VIRT_IFACE="$(get_virt_iface "$1")"
				;;
			-i)
				[ "$#" -ge 3 ] || usage
				shift
				SRV_PHYS_IFACE="$(get_phys_iface "$1")"
				;;
			-f)
				FORCE="Y"
				;;
			-d)
				DEBUG="Y"
				;;
			*)
				usage
				;;
		esac
		OPTIONS="$OPTIONS $1"
		shift
	done

	# check commands
	[ "$1" ] || usage
	if [ "$DEBUG" ] ; then
		cat << EOF
- Options selected  : [$OPTIONS ]
- Virtual interface : [ $SRV_VIRT_IFACE ]
- Physical interface: [ $SRV_PHYS_IFACE ]
EOF
	fi

	A="$(echo "$1" |tr "[:lower:]" "[:upper:]")"

	case "$A" in
		A*|S|SE*)
			ACTION="ADDRESS SET"
			OK=""
                        for a in $ACTION ; do
                                if echo "$a" |grep -q "^$A" ; then
                                       OK="Y"
                                fi
                        done
                        [ "$OK" ] || usage

			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"

			if [ "$#" -ge 2 ] && [ "$2" ] ; then
				shift
				CIDR="$1"
				if echo "x$1" |grep -q "^x[+-]\?[[:digit:]]\+$" ; then
					# remove beginning '+' if any
					IP_NUM="$(echo "x$1" |sed -e "s%^x%%;s%^+%%")"
					PCIDR="$(get_iface_cidr "$SRV_PHYS_IFACE")"
					CIDR="$(get_srv_cidr "$PCIDR" "$IP_NUM")"
					[ -z "$DEBUG" ] || echo "  -- IP number: [ $IP_NUM of $PCIDR ]"
				fi

				if echo "$CIDR" |grep -q "^\([[:digit:]]\+\.\)\{3\}[[:digit:]]\+/\-\?[[:digit:]]\+$" ; then
					[ -z "$DEBUG" ] || echo "  --  cidr: [ $CIDR ]"
					[ -z "$FORCE" ] || create_virt_iface "$SRV_VIRT_IFACE" "$SRV_PHYS_IFACE" 2>/dev/null

					addr_virt_iface "$SRV_VIRT_IFACE" "$CIDR"
				else
					die "Invalid parameter: $CIDR"
				fi
			else
				show_addr_virt_iface "$SRV_VIRT_IFACE"
			fi
			;;
		C*)
			ACTION="CREATE"
			echo "$ACTION" |grep -q "^$A" || usage
			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"
			create_virt_iface "$SRV_VIRT_IFACE" "$SRV_PHYS_IFACE"
			;;
		D|DE*)
			ACTION="DELETE"
			echo "$ACTION" |grep -q "^$A" || usage
			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"
			if test_iface "$SRV_VIRT_IFACE" ; then
				delete_virt_iface "$SRV_VIRT_IFACE"
			else
				die "Virtual interface not created"
			fi
			;;
		DO*)
			ACTION="DOWN"
			echo "$ACTION" |grep -q "^$A" || usage
			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"
			test_iface "$SRV_VIRT_IFACE" || die "Virtual interface not created"
			down_virt_iface "$SRV_VIRT_IFACE"
			;;
		SH*|ST*)
			ACTION="SHOW STATUS"
			OK=""
                        for a in $ACTION ; do
                                if echo "$a" |grep -q "^$A" ; then
                                       OK="Y"
                                fi
                        done
                        [ "$OK" ] || usage

			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"
			show_virt_iface "$SRV_VIRT_IFACE"
			;;
		U|UN*)
			ACTION="UNSET"
			echo "$ACTION" |grep -q "^$A" || usage
			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"
			unset_virt_iface "$SRV_VIRT_IFACE"
			;;
		UP)
			ACTION="UP"
			echo "$ACTION" |grep -q "^$A" || usage
			[ -z "$DEBUG" ] || echo "- Action: [ $ACTION ]"
			[ -z "$FORCE" ] || create_virt_iface "$SRV_VIRT_IFACE" "$SRV_PHYS_IFACE" 2>/dev/null
			test_iface "$SRV_VIRT_IFACE" || die "Virtual interface not created"
			up_virt_iface "$SRV_VIRT_IFACE"
			;;
		*)
			usage
			;;
	esac


}

################
# main program #
################
main "$@"
exit 0

