#! /bin/sh

EXECUTABLE=/home/stlucia/ddb/xtaldv/bin/dec_alpha/xtal_35
XTALHOME=/home/stlucia/ddb/xtaldv/aux/
export XTALHOME

SYMMAP="${XTALHOME}symmap"
SYMLINE="spaceg" #   which to choose??   Herman Maugin (y) or Hall notation (n)
LINE=48        #   default  - set starting line of symmap file


#process command line options 
while [ $# -gt 0 ]
    do
        case $1 in
          -sgname) 
              SYMLINE="sgname"
              shift ;;
          -spaceg) 
              SYMLINE="spaceg"
              shift ;;
          -line) 
              LINE=$2
              shift  
              shift ;;
          -h) 
             echo "Usage: $0  -line n [-sgname | -spaceg] (def: n=48 -spaceg)"
             exit 0 ;;
          *) 
             echo $1 'is not a valid option'
             echo "Usage: $0  -line n [-sgname | -spaceg] (def: n=48 -spaceg)"
             exit 1 ;;
        esac
   done

echo "Running with $EXECUTABLE "
echo "Using  symmap file  $SYMMAP "
echo "Using  compid  debug in current directory "
echo "Checking STARTX $SYMLINE operation"
echo "First line used is line $LINE \n"



#  loop for all space groups in symmap file
while [ 1 -eq 1 ]
  do 
  if   test "$SYMLINE" = "spaceg"  ; then
       SGNAME=`sed -n -e $LINE's/^\([ ]*[^ ]*[ ]*[^ ]*[ ]*\)\([^ ]*\).*$/\2/p' $SYMMAP`
  else 
       SGNAME=`sed -n -e $LINE's/^\([ ]*[^ ]*[ ]*[^ ]*[ ]*[^ ]*[ ]*\)\([^ ]*\).*$/\2/p' $SYMMAP`
  fi


# report the sgname being analysed 
echo $SGNAME

#  exit if a null entry or at end of symmap file
  case $SGNAME in
    "")
    echo " No sgname "
    exit 1 ;;
    ' '*)
    echo " No sgname "
    exit 1 ;;
  esac   


# run with $SYMLINE $SGNAME as an xtal test case
# and pipe the xtal output through  awk for pattern matching and error reports
echo "\
compid debug
startx
cell 10 10 10 90 90 90
$SYMLINE $SGNAME
celcon C 8
end
startx upd
FINISH 
"  | $EXECUTABLE  | awk ' 
      BEGIN            { count= 0 ; error="no"}
      /^ Space group / { ++count ; line[count] = $0 }
      /^ Failure in ./ { error="yes"}
      /^ A consoling / { error="no"}
                       { if (error =="yes") print $0 }
      END              { if (count < 2 || ( line[1] != line[2] ) ) {
                               print " LINE '$LINE' sgname '$SGNAME' " 
                               print line[1] 
                               print line[2] }
                       }' $LISTFILE 

# increment the symmap line number
  LINE=`expr $LINE + 1`

# end of infinite loop
done


##################################END END END###########################

