#! /bin/sh
#
#  Copyright (C) 2000, The MITRE Corporation
#
#  Use of this software is subject to the terms of the GNU General
#  Public License version 2.
#
#  Please read the file LICENSE for the exact terms.
#

# Create or delete a GRE point to point tunnel
#
# Usage:
#   mmtunnel add <name> <local addr> <remote addr> <tunnel addr>
#   mmtunnel delete <name>
#

usage ()
{
 echo "Usage:"
 echo "  mmtunnel add <name> <local addr> <remote addr> <tunnel addr>"
 echo "           del <name> "
 exit 1
}

if [ $# -lt 2 ] ; then usage ; fi

ACTION=$1 ; NAME=$2 ;

#echo "n=$# 0=$0 ACTION=$ACTION NAME=$NAME"

case "$ACTION" in

'add')
    if [ $# -ne 5 ] ; then usage ; fi
    LOCAL=$3
    REMOTE=$4
    TUNNEL=$5
#    echo "local=$LOCAL remote=$REMOTE tunnel=$TUNNEL"
    ip tunnel add $NAME mode gre remote $REMOTE local $LOCAL || exit 1
    ip addr   add $TUNNEL dev $NAME || exit 1
    ip link   set $NAME up         || exit 1
    ;;

'del')
    if [ $# -ne 2 ] ; then usage ; fi
    ip link   set $NAME down       || exit 1
    ip tunnel del $NAME            || exit 1
    ;;

*)
    usage
    ;;

esac
 
exit 0