#!/bin/sh
#
# find next unallocated PMIDs for the sample PMDA
#

if [ $# -gt 1 ]
then
    echo "Usage: $0 [N]"
    echo "N defaults to 1 (PMID to generate)"
    exit 1
fi
want=1
[ $# -eq 1 ] && want="$1"

root=root
if [ ! -f $root ]
then
    root=../root
fi
if [ ! -f $root ]
then
    echo "Error: cannot find PMNS in root nor ../root"
    exit 1
fi

pminfo -m -n $root \
| sed -n -e '/ 29/s/\(.*\): \(.*\)/\2 \1/p' \
| sort -t . -n -k 1,1 -k 2,2 -k 3,3 \
| sed -e 's/ .*//' -e 's/\./ /g' \
| awk '
BEGIN	{ lastidx = -1; found = 0 }
NF != 3	{ next }
$2 != 0	{ next }
	{ if (lastidx+1 != $3) {
	    for (i = lastidx+1; i < $3; i++) {
		print "SAMPLE:0:" i
		found++
		if (found == '"$want"') exit
	    }
	  }
	  lastidx = $3
	}
END	{ while (found < '"$want"') {
	    lastidx++
	    print "SAMPLE:0:" lastidx
	    found++
	  }
	}'
