#!/bin/csh
unalias rm
clear
set NASTHOME=/usr/local/libexec/NASTRAN-95
set rfdir=$NASTHOME/rf
set nasexec=$NASTHOME/bin/nastrn.exe
set naschk=$NASTHOME/bin/chkfil.exe
set probname = $1
echo ' '
  if ( $probname == '' ) then
  echo '                                 NASTRAN'
  echo ' '
  echo -n 'Please give problem id for designation of files ===> '
  set probname = $<
  endif
# set ft01=$probname.pun
set dbmem=12000000
set ocmem=2000000
set FT01=none
set FT04=$probname.dic
set FT03=$probname.log
set FT05=$probname.inp
set FT06=$probname.out
set FT08=none
# set ft11=$probname.out11
set FT11=none
set PLT2=none
set script=$probname.cmd
set nasscr=$cwd/temp$$
set FT12=none
set FT15=none
set FT16=none
set SOF1=none
set SOF2=none
set SFT12=
set NPTP=$probname.nptp
set OPTP=none
   if ( ! -e  $FT05 ) then
   set SFT05='(#### does not exist ####)'
   else 
   set SFT05=
   if ( -e nogood1 ) then
   rm nogood1
   endif
   if ( -e nogood2 ) then
   rm nogood2
   endif
   if ( -e nogood3 ) then
   rm nogood3
   endif
   $naschk < $FT05
	if ( -e nogood1 ) then 
	set FT04=$probname.dic
        rm nogood1
	endif
	if ( -e nogood2 ) then
	set PLT2=$probname.PLT
        rm nogood2
	endif
	if ( -e nogood3) then
	set FT04=$probname.dic
	set PLT2=$probname.PLT
        rm nogood3
	endif
   endif
set nogo=1
while ( $nogo != 0 )
clear 
echo '                               NASTRAN'
echo ' '
set snasexec=
set SFT01=
set SFT04=
set SFT03=
set SFT06=
set SFT08=
set SFT11=
set SFT15=
set SFT16=
set SPLT2 =
set SNPTP=
set SOPTP=
set sdir=
set sscript=
set SSOF1=
set SSOF2=
set nogo=1
   if ( $NPTP != 'none' ) then 
      if ( -e $NPTP ) then 
      set SNPTP=' (#### will be replaced ####)'
      endif
      if ( -e "$FT04" ) then
      set SFT04=' (#### will be replaced ####)'
      endif
   endif 
   if ( $SOF1 != 'none' ) then
      if( -e $SOF1 ) then
      set SSOF1=
      else set SSOF1='(#### does not exist ####)'
      endif
   endif
   if ( $SOF2 != 'none' ) then
      if( -e $SOF2 ) then
      set SSOF2=
      else set SSOF2='(#### does not exist ####)'
      endif
   endif
   if ( -e $PLT2 ) then
   set SPLT2='(#### will be replaced ####)'
   else set SPLT2=
   endif
   if ( -d $nasscr ) then
   set sdir=' (#### will be recreated ####)'
   endif
   if ( -e  $FT06 ) then
   set SFT06=' (#### will be replaced ####)'
   endif
   if ( -e $FT03 ) then 
   set SFT03=' (#### will be replaced ####)'
   endif
   if ( -e $FT01 ) then 
   set SFT01=' (#### will be replaced ####)'
   endif
   if ( -e "$FT11" ) then 
   set SFT11=' (#### will be replaced ####)'
   else
   set SFT11=
   endif
   if ( -e  $script ) then 
   set sscript=' (#### will be replaced ####)'
   endif
echo '  (i)  Input file       ===> '$FT05   $SFT05      
echo '  (o)  Output file      ===> '$FT06   $SFT06
echo '  (l)  Logfile          ===> '$FT03   $SFT03
echo '  (s)  Script file      ===> '$script $sscript
echo '  (pu) Punch file       ===> '$FT01   $SFT01
echo '  (pl) Plot file        ===> '$PLT2   $SPLT2
echo '  (c)  Checkpoint NPTP  ===> '$NPTP   $SNPTP
echo '  (d)  Checkpoint dict. ===> '$FT04   $SFT04
echo '  (r)  Restart OPTP     ===> '$OPTP   $SOPTP
echo '  (ou) FTN11 file       ===> '$FT11   $SFT11
echo '  (in) FTN12 file       ===> '$FT12   $SFT12
echo '  (s1) SOF1 file        ===> '$SOF1   $SSOF1
echo '  (s2) SOF2 file        ===> '$SOF2   $SSOF2
echo '  (i1) FTN15 file       ===> '$FT15   $SFT15
echo '  (i2) FTN16 file       ===> '$FT16   $SFT16
echo ' '
echo '  (oc) Memory for Open Core    ===> '$ocmem
echo '  (im) In-Memory DB Allocation ===> '$dbmem
echo '  (w)  Work Directory ===> '$nasscr $sdir
echo '  (g)  To create shell script and execute NASTRAN'
echo '  (a)  Abort without building shell script'
echo
echo -n ' Specify Option ===> ' 
set  opt = $<
   switch ($opt) 
     case 'im':
     case 'Im':
     case 'iM':
     case 'IM':
        echo -n 'Please give allocation (in words) for in-memory db ===> '
        set dbmem = $<
        breaksw
     case 'oc':
     case 'Oc':
     case 'oC':
     case 'OC':
        echo -n 'Please give allocation (in words) for open core ===> '
        set ocmem = $<
        breaksw
     case 'a':
     case 'A':
     case 'q':
     case 'Q':
	clear
	exit
	breaksw
     case 'i':
     case 'I':
     echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give input file ===> '
	   set FT05 = $<
	      if ( ! -e $FT05 ) then
	      echo $FT05 'does not exist'
	      else set ok = i0
                if ( -e nogood1 ) then
                rm nogood1
                endif
                if ( -e nogood2 ) then
                rm nogood2
                endif
                if ( -e nogood3 ) then
                rm nogood3
                endif
	      $naschk < $FT05
	      set SFT05=
              set NPTP=$probname.nptp
              set FT04=none
              set PLT2=none
	         if ( -e nogood1 ) then
		 set FT04=$probname.dic
                 rm nogood1
		 endif
		 if ( -e nogood2 ) then 
		 set PLT2=$probname.PLT
                 rm nogood2
		 endif
		 if ( -e nogood3 ) then 
		 set FT04=$probname.dic
		 set PLT2=$probname.PLT
                 rm nogood3
		 endif
	      endif
	   end
	 breaksw
       case 'G':
       case 'g':
	 set nogo=0
	 clear
	    if ( ! -e $FT05 ) then 
	    echo '                ERROR'
	    echo ' '
	    echo $FT05 'does not exist--cannot create script'
	    set  nogo=i1
	    set  anything =$<
	    endif
	    if ( -d $nasscr ) then 
	    echo ' '
	    echo ' '
	    echo '                  WARNING'
	    echo ' '
	    echo 'Directory '$nasscr ' exists.  It will be recreated.'
	    echo 'All existing files in this directory will be lost!!!'
	    echo ' '
	    echo -n 'Are you sure you want this to happen? (y or n) '
	    set ans = $<
	       if (  $ans != 'y' ) then 
	       set nogo=i1
	       endif
	    endif
	  breaksw
     case 'r':
     case 'R':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give restart OPTP file ===> '
	   set  OPTP = $<
	      if ( ! -e $OPTP ) then 
	      echo $OPTP '(#### does not exist ####)'
	      else set ok=i0
	      endif
	   end
	 breaksw
     case 's1':
     case 'S1':
	echo ' '
        echo -n 'Please give SOF1 file ===> ' 
	set  SOF1 = $<
	   if ( ! -e $SOF1 ) then 
	   set  SSOF1='(#### does not exist ####)'
	   else set SSOF1=
	   endif
	 breaksw
     case 's2':
     case 'S2':
	echo ' '
        echo -n 'Please give SOF2 file ===> ' 
	set  SOF2 = $<
	   if ( ! -e $SOF2 ) then 
	   set SSOF2='(#### does not exist ####)'
	   else set SSOF2=
	   endif
	 breaksw
     case 'RD':
     case 'Rd':
     case 'rd':
     case 'rD':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give restart dict ===> '
	   set FT08 = $<
	      if ( ! -e $FT08 ) then 
	      echo $FT08='(#### does not exist ####)'
	      else set ok=i0
	      set SFT08=
	      endif
	   end
	 breaksw
     case 'in':
     case 'IN':
     case 'In':
     case 'iN':
	echo ' '
	set ok=i1
	while ($ok != i0 )
	   echo -n 'Please give in12 file ===> '
	   set FT12 = $<
	      if ( ! -e $FT12  ) then 
              set SFT12='(#### does not exist ####)'
              set ok=i0
              else
	      set SFT12=
              set ok=i0
	      endif
	   end
	 breaksw
     case 'ou':
     case 'OU':
     case 'Ou':
     case 'oU':
	echo ' '
	set ok=i1
	while ($ok != i0 )
	   echo -n 'Please give out11 file ===> '
	   set FT11 = $<
	      if ( -e  $FT11 ) then 
	      set SFT11=
	      set ok=i0
	      else set ok=i0
	      set SFT11='**** will be replaced ****'
	      endif
	   end
	 breaksw
     case 'i1':
     case 'I1':
	echo ' '
	set ok=i1
	while ($ok != i0 )
	   echo -n 'Please give FT15 file ===> '
	   set FT15 = $<
	      if ( -e  $FT15 ) then 
	      set SFT15=
	      set ok=i0
	      else set ok=i0
	      set SFT15='**** will be replaced ****'
	      endif
	   end
	 breaksw
     case 'i2':
     case 'I2':
	echo ' '
	set ok=i1
	while ($ok != i0 )
	   echo -n 'Please give FT16 file ===> '
	   set FT16 = $<
	      if ( -e  $FT16 ) then 
	      set SFT16=
	      set ok=i0
	      else set ok=i0
	      set SFT16='**** will be replaced ****'
	      endif
	   end
	 breaksw
     case 'O':
     case 'o':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give output file ===> '
	   set  FT06 = $<
	      if ( -e $FT06 ) then
	      echo -n $FT06 'exist, do you want to keep it? (y or n) '
	      set  ans = $<
		 if (  $ans == 'n' ) then
		 rm $FT06
		 set ok=i0
		 endif
	      else set ok=i0
	      endif
	   end
	 breaksw
     case 'l':
     case 'L':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give log file ===> '
	   set  FT03 = $<
	      if ( -e  $FT03 ) then
	      echo -n $FT03 'exist, do you want to keep it? (y or n) '
	      set ans = $<
		 if ( $ans == 'n' ) then 
		 rm $FT03
		 set ok=i0
		 endif
	      else set ok=i0
	      endif
	   end
	 breaksw
     case 'd':
     case 'D':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give checkpoint dict. ===> '
	   set FT04 = $<
	      if ( -e  $FT04 ) then 
	      echo -n $FT04 'exist, do you want to keep it? (y or n) '
	      set ans = $<
		 if ( $ans == 'n' ) then
		 rm $FT04
		 set ok = i0
		 endif
	      else set ok = i0
	      endif
	   end
	 breaksw
     case 'pu':
     case 'Pu':
     case 'pU':
     case 'PU':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give punch file ===> '
	   set  FT01 =  $<
	      if ( -e  $FT01 ) then
	      echo -n $FT01 'exist, do you want to keep it? (y or n) '
	      set  ans = $<
		 if ( $ans == 'n' ) then
		 rm $FT01
		 set ok = i0
		 endif
	      else set ok = i0
	      endif
	   end
	 breaksw
     case 's':
     case 'S':
	echo ' '
	set ok=i1
	while ( $ok != i0 )
	   echo -n 'Please give script file ===> '
	   set  script = $<
	      if ( -e  $script ) then 
	      echo -n $script 'exist, do you want to keep it? (y or n) '
	      set ans = $<
		 if ( $ans == 'n' ) then 
		 rm $script
		 set ok = i0
		 endif
	      else set ok = i0
	      endif
	   end
	 breaksw
     case 'c':
     case 'C':
	echo ' '
	set ok = i1
	while (  $ok != i0 )
	   echo -n 'Please give checkpoint NPTP file ===> '
	   set NPTP = $<
	      if ( -e  $NPTP ) then 
	      echo -n $NPTP 'exist, do you want to keep it? (y or n) '
	      set ans = $<
		 if ( $ans == 'n' ) then 
		 rm $NPTP
		 set ok = i0
		 endif
	      else set ok = i0
	      endif
	   end
	 breaksw
     case 'w':
     case 'W':
	echo ' '
	echo -n 'Please give work directory ===> '
	set  nasscr = $<
	breaksw 
     case 'pl':
     case 'Pl':
     case 'pL':
     case 'PL':
	echo ' '
	set ok = i1
	while ( $ok != i0 )
	   echo -n 'Please give plot file ===> '
	   set PLT2 = $<
	      if ( -e '$PLT2' ) then 
	      echo -n $PLT2 'exist, do you want to keep it? (y or n) '
	      set  ans =$<
		 if ( $ans == 'n' ) then 
		 then rm $PLT2
		 set ok = i0
		 endif
	      else set ok = i0
	      endif
	   end
	 breaksw
   endsw
end
if ( -e $script ) then 
rm $script
endif
echo '#/bin/csh' >> $script
echo ' unalias rm ' >> $script
echo 'if ( -d ' $nasscr' ) then' >> $script
echo 'rm -r '$nasscr >> $script
echo 'endif' >> $script
echo 'mkdir '$nasscr >> $script
echo 'if ( -e '$NPTP ' ) then'>> $script
echo 'rm '$NPTP >> $script
echo 'endif' >> $script
echo 'if ( -e '$FT03 ' ) then'>> $script
echo 'rm '$FT03 >> $script
echo 'endif' >> $script
echo 'if ( -e '$FT01 ' ) then'>> $script
echo 'rm '$FT01 >> $script
echo 'endif' >> $script
echo 'if ( -e '$FT04 ' ) then'>> $script
echo 'rm '$FT04 >> $script
echo 'endif' >> $script
echo 'if ( -e '$FT06 ' ) then'>> $script
echo 'rm '$FT06 >> $script
echo 'endif' >> $script
echo 'if ( -e '$PLT2 ' ) then'>> $script
echo 'rm '$PLT2 >> $script
echo 'endif' >> $script
echo 'echo ==== NASTRAN is beginning execution of "'$probname'" ====' >> $script
echo ' env  NPTPNM='$NPTP '\' >> $script
echo '  PLTNM='$PLT2 ' DICTNM='$FT04 ' PUNCHNM='$FT01 '\' >> $script 
echo '  FTN11='$FT11 ' FTN12='$FT12 ' DIRCTY='$nasscr '\' >> $script
echo '  LOGNM='$FT03 ' OPTPNM='$OPTP ' RFDIR='$rfdir '\' >> $script 
echo '  FT06='$FT06 ' FT05='$FT05 ' PROJ=''.''' '\' >> $script
echo '  FTN13=none SOF1='$SOF1 ' SOF2='$SOF2  '\' >> $script 
echo '  FTN14=none FTN17=none FTN18=none FTN19=none FTN20=none \' >> $script
echo '  FTN15='$FT15 ' FTN16='$FT16  '\' >> $script 
echo '  FTN21=none FTN22=none FTN23=none \' >> $script 
echo '  DBMEM='$dbmem ' OCMEM='$ocmem '\' >> $script
echo    $nasexec  >> $script
#echo    $nasexec' < '$FT05' >'$FT06 >> $script
echo 'rm -r '$nasscr >> $script
echo 'if ( -e none  ) then'>> $script
echo 'rm none' >> $script
echo 'endif' >> $script
# echo 'rm COS*' >> $script
echo 'echo   ===== NASTRAN has completed problem "'$probname'" ====='>> $script
clear 
echo 'The shell script '$script' was successfully created'
echo
echo -n 'Do you want to execute this problem now? (y or n) ===> '
chmod +x $script
set ans = $<
   if ( $ans == 'y' ) then
   echo ' '
   echo -n 'Do you want to run in foreground or background? (f or b) ===> '
   set ans = $<
      if ( $ans == 'f' ) then
      $cwd/$script
      else
      $cwd/$script &
      endif
   else echo ' '
   echo 'Type the command "'$script'" to execute this problem intereactively'
   echo '                            OR '
   echo '  the command "'$script' &"  to execute this problem in batch'
   endif
