#!/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:
#   find_portbundle
#
# Description:
#   Find a bundle of consecutive ports
#
# Arguments:
#   $1: tcp or udp
#   $2: port which is the start point to check
#   $3: quantity of the ports
#
# Returns:
#   0: Success
#   1: Fail
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
export LTPROOT


# Check argument
if [ $# -ne 3 ] ; then
    echo "Usage: find_portbundle protocol start_port quantity_of_ports" >&2
    exit 1
fi
protocol=$1
start_port=$2
port_quantity=$3

# Specify the option of the nestat command
case $protocol in
    tcp)
    proto_opt="-t"
    ;;
    udp)
    proto_opt="-u"
    ;;
    *)
    echo "$protocol is not supported" >&2
    exit 1
    ;;
esac

# Create the open port list
port_list=`mktemp`
netstat -a -n ${proto_opt} \
    | tail -n +3 \
    | awk '{ print $4 }' \
    | sed 's/^.*://' \
    | sort -n \
    | uniq > ${port_list}

# Search the available ports
skip=1
min_port=$start_port
max_port=`expr $min_port + $port_quantity - 1`
if [ ${max_port} -gt 65535 ]; then
    rm -f $port_list
    exit 1
fi

while read line ; do
    # Skip to the required port
    if [ $skip -eq 1 ]; then
	if [ $line -lt $start_port ]; then
	    continue
	else
	    skip=0
	fi
    fi

    # If required quantity of the port isn't available, seach in the next range
    if [ $line -le $max_port ]; then
	min_port=`expr $line + 1`
	max_port=`expr $min_port + $port_quantity - 1`
	if [ $max_port -gt 65535 ]; then
	    rm -f $port_list
	    exit 1
	fi
	continue
    fi
    break
done < $port_list

rm -f $port_list

# Print the result. If required quantity is not 1, print in range expression
if [ $min_port -eq $max_port ]; then
    echo "$min_port"
else
    echo "$min_port-$max_port"
fi

exit 0