#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program 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 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program 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 this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   ns-echoclient
#
# Description:
#   Send various kind of echo request
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Options:
#   -S name or IP address of the server
#   -f protocol family
#	4: IPv4
#	6: IPv6
#   -s array of packet size
#   -t timeout [sec]
#   -h display this usage
#
# Outputs:
#   Process ID of the TCP traffic server
#
# Exit Value:
#    0: Exit normally
#   >0: Exit abnormally
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

#-----------------------------------------------------------------------
#
# Function: usage
#
# Description:
#   Print the usage of this script, then exit
#
# Argument
#   value: exit value
#
#-----------------------------------------------------------------------
usage(){
    value=$1
    cat << EOD >&2
ns-echoclient [OPTION]
	-S name or IP address of the server
	-f protocol family
		4: IPv4
		6: IPv6
	-s array of packet size
	-h display this usage
EOD

    exit $value
}


#
# Main
#

family=0

while getopts 'S:f:s:h' opt ; do
    case $opt in
	'S')
	server_name=$OPTARG
	;;
	'f')
	family=$OPTARG
	;;
	's')
	size_array="$OPTARG"
	;;
	'h')
	usage 0
	;;
	*)
	echo "Unknown option" >&2
	usage 1
	;;
    esac
done

# Check the server name
if [ x$server_name = x ]; then
    echo "server name isn't specified."
    usage 1
fi

# Define the protocol family
case $family in
    4)
    ping_command="ping"
    ;;
    6)
    ping_command="ping6"
    ;;
    *)
    echo "protocol family should be 4 or 6."
    usage 1
    ;;
esac

# Send the echo request
if [ x"$size_array" = x ]; then
    $ping_command $server_name >/dev/null 2>&1 &
else
    for size in $size_array ; do
	$ping_command -s $size $server_name >/dev/null 2>&1 &
    done
fi

exit 0