#!/bin/sh
#
# Use the $min and $max from new to see how many test seq #s are available
#

tmp=/var/tmp/$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15

_do_range()
{
    awk '
BEGIN	{ first = last = -2 }
	{ if ($1 == last+1) {
	    last = $1
	  }
	  else {
	    if (first != -2) {
		if (first == last) print first
		else print first "-" last
	    }
	    first = last = $1
	  }
	}'
}

eval `grep '^min=' new`
eval `grep '^max=' new`

sed -n <group >$tmp.taken \
    -e 's/:\(retired\|reserved\) / /' \
    -e '/^[0-9]/s/[        ].*//p' \
# end

try=000
while [ $try -lt $min ]
do
    if [ ! -f "$try" ]
    then
	if grep "^$try\$" $tmp.taken >/dev/null
	then
	    :
	else
	    echo $try
	fi
    fi
    try=`expr $try + 1`
    try=`printf "%03d" $try`
done >$tmp.avail

if [ -s $tmp.avail ]
then
    echo "9999999" >>$tmp.avail
    _do_range <$tmp.avail
    echo "Below min=$min [`wc -l <$tmp.avail | sed -e 's/ //g'` available] ..."
    echo
fi

try=$min
while [ $try -le $max ]
do
    if [ ! -f "$try" ]
    then
	if grep "^$try\$" $tmp.taken >/dev/null
	then
	    :
	else
	    echo $try
	fi
    fi
    try=`expr $try + 1`
    try=`printf "%03d" $try`
done >$tmp.avail

if [ -s $tmp.avail ]
then
    echo "9999999" >>$tmp.avail
    _do_range <$tmp.avail
    echo "$min $max `wc -l <$tmp.avail | sed -e 's/ //g'`" \
    | awk '
      { printf "Between min=" $1 " and max=" $2 ", " $3 " available (%.1f%% free)\n",100 * $3 /($2 - $1 + 1) }'
fi
