#! /bin/sh
if [ $# = 0 -o $1 = "--help" ]; then
    echo "Command syntax: "
    echo "   newpart [-l] [--help] <partition>"
    echo "where:"
    echo "   -l          List partitions created on this node and their status"
    echo "   <partition> Partition name as listed in column 1 of -l output"
    exit 1
fi
#---  find the process manager
proclist=`ps -fu $USER \
        | grep procmgt \
        | grep -v grep \
        | sed -e 's,^.*--file:,,g' -e 's, .*$,,g'`

node=`uname -n`
inifile="${proclist}_Initial"
startfile="${proclist}_Start"
nodestart="${startfile}.${node}"
[ -f ${nodestart} ] && startfile="$nodestart"
#
# --  Check for a listing request
if [ "$1" = "-l" ]; then
#   echo "partitions defined in: $inifile"
#   ipstat="`cat $inifile | tr '\t' ' ' \
#         | grep "PART " \
#         | sed -e 's,^ ,,' -e 's, [ ]*, ,g' \
#         | cut -d ' ' -f1 | sed -e 's,PART,,'`"
   echo "partitions defined in: $startfile"
   ipstat=`cat $startfile | tr '\t' ' ' \
         | sed -e 's,  *, ,g' -e 's,^,,g' \
         | grep 'smcreate' \
         | sed -e 's,^.*smcreate ,,g' -e 's,^\\$,,g' -e 's, .*$,,g' \
         | cut -d ' ' -f2 | sed -e 's,PART,,'`
   for pname in $ipstat; do
     part=`cat $inifile | tr '\t' ' ' \
          | grep "${pname}PART " \
          | sed -e 's,^ *,,' -e 's,  *, ,g' \
          | cut -d ' ' -f 2`
      if [ `smstat exists $part` = 0 ]; then
         echo "$pname $part missing" | tr ' ' '\t'
      else
         echo "$pname $part" | tr ' ' '\t'
      fi
   done
   exit
fi
#
# create the named partition
id=$1
pname=`cat $inifile | tr '\t' ' ' \
        | grep "^${id}PART " \
        | sed 's,  *, ,g' \
        | cut -d ' ' -f 2`
pnbuf=`cat $inifile | tr '\t' ' ' \
        | grep "^${id}BUFFCNT " \
        | sed 's,  *, ,g' \
        | cut -d ' ' -f 2`
plbuf=`cat $inifile | tr '\t' ' ' \
        | grep "^${id}BUFFLEN " \
        | sed 's,  *, ,g' \
        | cut -d ' ' -f 2`
if [ `smstat exists $pname` != 0 ]; then
  smrepair --keep $pname
else
  smcreate $pname -nbuf $pnbuf -lbuf $plbuf
  FrTest -p $pname > /dev/null &
fi
