#!/bin/csh

set testName  = (`getArgD $argv -in None`)
set bmrbName  = None
set pdbName   = None
set csName    = None
set starArgs  = ""
set protArgs  = ""
set tExclList = ""

set sInfo    = (0 0 0)
set gInfo    = (0 0 0)
set pInfo    = (0 0 0)

if (`flagLoc $argv -noauto`) then
   echo "PROMEGA Using Input without Format Testing ..."
else
   echo "PROMEGA Testing Input Format ..."

   set bmrbName = (`getArgD $argv -bmrb bmrb.tab`)
   set pdbName  = (`getArgD $argv -ref  None`)
   set csName   = (`getArgD $argv -in   None`)

   if (($pdbName != None) && (-e $pdbName)) then
      set pInfo = (`checkNMRFile.tcl -pdb -in $pdbName` 0)

      if ($pInfo[1] == 0) then
         echo \
         "PROMEGA Error. Reference PDB Input does not seem to be in PDB Format."

         exit 1
      endif
   endif

   if (($csName != None) && (-e $csName)) then
      set gInfo = (`checkNMRFile.tcl -cs   -in $csName` 0)
      set sInfo = (`checkNMRFile.tcl -bmrb -in $csName` 0)

      if ($gInfo[1] == 0 && $sInfo[1] == 0) then
         echo "PROMEGA Error. Chemical Shift Input is not correctly formatted."
         exit 1
      endif
   endif

   if ($sInfo[1] == 1) then
      echo "Converting Input from BMRB NMR-Star Format ..."
      star2cs.tcl -in $csName -out $bmrbName
      set starArgs = "-star $bmrbName"
      set testName = $bmrbName
   endif
endif

if (`flagLoc $argv -autoproton`) then
   echo "Testing Input Proton Count ..."

   set hInfo = (`selectTab.tcl -in $testName -show RESID -cond "strmatch( ATOMNAME, 'H*' )" | wc` 0 0 0)

   if ($hInfo[1] == 0) then
      echo "Found 0 Protons in Input, Using PROMEGA No-Proton Mode."
      set protArgs = "-noproton"
   else
      echo "Found $hInfo[1] Protons, Using PROMEGA Standard Mode."
   endif
endif

# set tExclList = (`talosExclList.tcl $* $starArgs`)
#
# if ($#tExclList > 1) then
#    echo "Auto-Excluding Identical Sequence: $tExclList[2-]"
# endif

$PROMEGA_DIR/bin/PROMEGA.${NMRBINTYPE} $* $tExclList $starArgs $protArgs

if (-e send.list) then
   /bin/rm send.list
endif

echo pred.tab > send.list
 
foreach i (predAdjCS.tab)
   if (-e $i) then
     echo $i >> send.list
   endif
end

exit

