#!/bin/sh

#     Copyright 2002-2009 Stefano Pettini, Fabio Ricci, Massimo Rimondini
#     Computer Networks Research Group, Roma Tre University.
#
#     This file is part of Netkit.
# 
#     Netkit is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     Netkit is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with Netkit.  If not, see <http://www.gnu.org/licenses/>.

# This script displays information about a lab without running it. Optionally,
# it can create a PostScript file containing the level 2 topology of the lab.

SCRIPTNAME=`basename $0`

# The following line has been introduced to ensure backward compatibility
: ${NETKIT_HOME:=$VLAB_HOME}

if [ -z "$NETKIT_HOME" ]; then
   echo 1>&2 "$SCRIPTNAME: The NETKIT_HOME environment variable is not properly set;"
   echo 1>&2 "please set it as described in the Netkit documentation and try"
   echo 1>&2 "again."
   exit 1
fi

CMDLINE="$0 $*"

. "$NETKIT_HOME/bin/script_utils"


# Write to the vcommands log
logWrite $0 $*


# This function is used to print the lhalt usage
help() {
   echo
   echo "Usage: $SCRIPTNAME [options]"
   cat << END_OF_HELP

This command displays information about a Netkit lab without running it.
Displayed information includes the number of virtual machines that make up the
lab. If asked to, the command can create a PostScript file containing a
graphical map of the link-level topology of the lab.
Available options are:

  -d DIRECTORY        Assume the lab is located inside DIRECTORY. By default,
                      the lab is assumed to be located in the current directory.
  -m, --map=FILE      Create a graphical link-level topology map of the lab and
                      save it into FILE in PostScript format. This requires the
                      Graphviz library to be installed.

Other standard options are:

  -h, --help          Show this help.
      --version       Print version information and exit.

END_OF_HELP
}


# Get command line options
INVALID_OPTION=0
OPT_LIST="help,version,map:"
CMDLINE_OPTIONS=`getopt -ql "$OPT_LIST" -- "hd:m:" "$@"`
if [ $? -gt 0 ]; then
   INVALID_OPTION=1
fi


# Using verbose mode tells vcrash to wait for virtual machines to stop before
# returning.
VERBOSE=1

# Parse command line options
parseCmdLine() {
   while [ $# -gt 0 ]; do
		CURRENT_ARGUMENT="$1"
      case "$CURRENT_ARGUMENT" in
      
         -d)
            shift; CURRENT_ARGUMENT="$1"
            if [ ! -z "$LAB_DIRECTORY" ]; then
               warning "$SCRIPTNAME" "$CMDLINE" "$0" \
                  "Multiple directory specifications: where can I find the lab?"
               exit 1
            fi
            LAB_DIRECTORY=`makeAbsolutePath "$CURRENT_ARGUMENT"`;;
         
         --help|-h)
            help
            exit;;
            
         --map|-m)
            shift; CURRENT_ARGUMENT="$1"
            checkSpaces "$CURRENT_ARGUMENT"
            MAP_FILE="$CURRENT_ARGUMENT";;
            
         --version)
            showVersion
            exit 0;;
            
         --)
            shift
            break;;
      esac
      shift
   done
   
   if [ $# -gt 0 ]; then
      warning "$SCRIPTNAME" "$CMDLINE" "$0" "Invalid option: \"$1\"."
      exit 1
   fi
}
eval parseCmdLine $CMDLINE_OPTIONS


# Check whether user gave some strange options
if [ $INVALID_OPTION -eq 1 ]; then
   warning "$SCRIPTNAME" "$CMDLINE" "$0" "Invalid option or missing option parameter."
   exit 1
fi

# If no lab directory has been given, assume current directory
LAB_DIRECTORY=${LAB_DIRECTORY:-${PWD}}
. "$NETKIT_HOME/bin/lcommon"

# Check that the lab directory exists
if [ ! -d "$LAB_DIRECTORY" ]; then
   warning "$SCRIPTNAME" "$CMDLINE" "$0" "Lab directory ($LAB_DIRECTORY) does not exist."
   exit 1
fi

# Check whether path to the lab directory contains spaces
if containsRegexp LAB_DIRECTORY " "; then
   warning "$SCRIPTNAME" "$CMDLINE" "$0" \
            "Invalid lab directory: \"$LAB_DIRECTORY\" (path contains spaces)."
   exit 1
fi


# Print some information about the lab
echo
becho "====================== Lab information =========================="
labWelcome
echo
buildAffectedVHostList
becho "The lab is made up of `echo $LABHOSTS | awk '{print NF}'` virtual machines ($LABHOSTS)."
if [ -f "$LAB_DIRECTORY/lab.dep" ]; then
   becho "The lab supports parallel startup."
fi

if [ ! -z "$MAP_FILE" ]; then
   if [ ! -f "$LAB_DIRECTORY/lab.conf" ]; then
      warning "$SCRIPTNAME" "$CMDLINE" "$0" "Unable to find file lab.conf: cannot create topology map."
      exit 1
   fi
   if labMap "$MAP_FILE"; then
      becho "Link-level topology map saved into $MAP_FILE."
   fi
fi
becho "================================================================="

echo
