#!/bin/sh

# 
# -------------------------------------------------------------------
#                                   X-BONE
#
#                          http://www.isi.edu/xbone
#                USC Information Sciences Institute (USC/ISI)
#                   Marina del Rey, California 90292, USA
#                          Copyright (c) 1998-2005
# 
# -------------------------------------------------------------------
#
# Copyright (c) 1998-2005 by the University of Southern California.
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation in source and binary forms for non-commercial
# purposes and without fee is hereby granted, provided that the above
# copyright notice appear in all copies and that both the copyright
# notice and this permission notice appear in supporting
# documentation, and that any documentation, advertising materials,
# and other materials related to such distribution and use acknowledge
# that the software was developed by the University of Southern
# California, Information Sciences Institute.  The name of the
# University may not be used to endorse or promote products derived
# from this software without specific prior written permission.
# 
# THE UNIVERSITY OF SOUTHERN CALIFORNIA MAKES NO REPRESENTATIONS ABOUT
# THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
# PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Other copyrights might apply to parts of this software and are so
# noted when applicable.
#
# -------------------------------------------------------------------
#
# Effort partly sponsored by the Defense Advanced Research Projects
# Agency (DARPA) and Air Force Research Laboratory, Air Force Materiel
# Command, USAF, under agreement numbers F30602-98-1-0200 (X-Bone) and
# F30602-01-2-0529 (DynaBone). The views and conclusions contained
# herein are those of the authors and should not be interpreted as
# necessarily representing the official policies or endorsements,
# either expressed or implied, of the Defense Advanced Research
# Projects Agency (DARPA), the Air Force Research Laboratory, or the
# U.S. Government.
#
# This work was partly supported by the NSF STI-XTEND (ANI-0230789)
# and NETFS (ANI-0129689) projects. Any opinions, findings, and
# conclusions or recommendations expressed in this material are those
# of the authors and do not necessarily reflect the views of the
# National Science Foundation.
#
# -------------------------------------------------------------------

#==============================================================================
# Outerlay Script:
#   Script to deploy an outerlay over multiple innerlays deployed previously.
#   (1) Extract innerlay addresses from innerlay files created using "innerlay".
#   (2) Construct outerlay barp table and store it in a file.
#   (3) Call prm.pl to construct the outerlay.
#
# Notes:
#   1. Only works on FreeBSD 4.4+ (ifconfig gif create).
#   2. Need to work on Linux and older versions of FreeBSD.
#==============================================================================

# Parameters used by XBone Resource Daemon (RD) to determine if the application
# is installed/available for this script:
# 
# APP_NAME= Outerlay
# APP_VCMD=

# Parameters to be replaced at XBone Overlay Manager (OM):
perhost_ips="PERHOST_IP_LIST"
all_names="ALL_NAMES"
real_ips="REAL_IPS"

#perhost_ip="172.26.4.1 172.26.4.2 172.26.4.5 172.26.4.6 172.27.4.9"
#all_names="sub add cos sec amc"
#real_ips="128.9.160.3 128.9.160.4 128.9.160.1 128.9.160.5 128.9.160.2"

# Parameters to be replaced at XBone RD:
OVL_NAME="OVLNAME"
IP_ALL="IPLIST"
IP="IP1"

#OVL_NAME="tree"

# Other parameters
RUN_DIR="/var/xbone/apps"
# XXX GET RID OF THIS ABSOLUTE PATH SOMEHOW!
XB_DIR="/home/larse/projects/xbone/apps/prm/"
INTERFACE = $ARGV[1];

#= Script starts ==============================================================

case "$1" in

 inner)
    echo ">>> Inner"
    rm -f ilay-$OVL_NAME
    echo "$real_ips" >  ilay-$OVL_NAME
    echo "$perhost_ip" >> ilay-$OVL_NAME
    echo "<<< End Inner"
    ;;

  outer)
    echo ">>> Outer"
    for l in `ls ilay-*`; do
      echo "    - $l"
    done
    echo "<<< End Outer"
    ;;

  conf)
    echo ">>> Outerlay conf:"
    echo "    - create $RUN_DIR/ilay-$OVL_NAME"
    rm -f $RUN_DIR/ilay-$OVL_NAME
    echo "$real_ips" > $RUN_DIR/ilay-$OVL_NAME
    echo "$perhost_ips" >> $RUN_DIR/ilay-$OVL_NAME
    echo "<<< End conf"
    ;;

  run)
    echo ">>> Outerlay run:"
    $XB_DIR/barp-table.pl $RUN_DIR/ilay-$OVL_NAME `ls $RUN_DIR/ilay-*`
    NEWIP=`echo "$IP" | sed s/^172/10/`
    $XB_DIR/prm.pl -outerlay $NEWIP/24 -barp $RUN_DIR/barp-$OVL_NAME -pidfile $RUN_DIR/pid-$OVL_NAME 
    for f in `ls $RUN_DIR/ilay-*`; do
      rm -f $f
    done
    echo "<<< End run"
    ;;

  dump)
    echo ">>> Outerlay Dump:"
    #INTERFACE = $ARGV[1];
    NEWIP=`echo "$IP" | sed s/^172/10/`
    $XB_DIR/dump.pl -barp $RUN_DIR/barp-$OVL_NAME -outerlay $NEWIP -interface "xl0" 
    echo "<<< End dump"
    ;;

  dumpl)
    echo ">>> Outerlay Dump:"
    NEWIP=`echo "$IP" | sed s/^172/10/`
    $XB_DIR/dump.pl -barp $RUN_DIR/barp-$OVL_NAME -outerlay $NEWIP -d 1
    echo "<<< End dump"
    ;;

  pid)
    echo ">>> pid"
    ;;

  kill)
    echo ">>> Outerlay kill:"
    kill -TERM `cat $RUN_DIR/pid-$OVL_NAME`
    echo "<<< End kill"
    ;;

  cleanup)
    echo ">>> Outerlay cleanup"
    rm -f $RUN_DIR/barp-$OVL_NAME
    echo "<<< End cleanup"
    ;;

esac

exit 0
