#!/bin/csh -f
#----------------------------------anopt------------------------------------
#
#                     ********  A N  O P T  *********
#
#
#                       programmed by Robert A. O'Barr                
#                            9 September 1989 
#
#           
#        Questions should be directed to:
#
#                           Orlando Merino
#                           Dept. of Mathematics
#                           UCSD, La Jolla, CA  92093
#
#                    phone:    (619) 534-3839
#                              (619) 534-3118
#
#                    email adress:  merino@osiris.ucsd.edu
# 
#-----------------------------------------------------------------------------
  



# math is executed in directory ctrlmath
# where init.m automatically loads code.m
      cd ctrlmath
      math  
      cd ..

      if ( -e .gk ) then 
                    else goto leave
      endif

      set warn = "no"

# print out readable formulas
      cat .gk .ak? .hk? > formulas

# compact the formula and
# check to make sure gk doesn't have too many
# continuation lines

      cp .gk /tmp/$$.file
      sed -f sremove /tmp/$$.file > .gk
      set len = `wc -l .gk | colrm 9`
      echo ".gk" $len | spacer
      set len = `wc -l .gk | colrm 9`
      if($len >= 19) then
            echo "Warning: gk(i) is too long"
            set warn = "yes"
      endif


# compact the formulas and
# check to make sure the ak[j] don't have too
# many continuation lines
      @ i = 1
      set file = ".ak1"
      while -e $file

        
        cp $file /tmp/$$.file
        sed -f sremove /tmp/$$.file > $file
        set len = `wc -l $file | colrm 9`
        echo $file $len | spacer
        set len = `wc -l $file | colrm 9`
        if($len >= 19) then
                echo "Warning: ak("$i") is too long"
                set warn = "yes"
        endif
        @ i = $i + 1
        set file = ".ak"$i
      end

# compact the formulas and
# check to make sure the hk[j] don't have too
# many continuation lines
      @ i = 1
      set file = ".hk1"
      while -e $file

        cp $file /tmp/$$.file
        sed -f sremove /tmp/$$.file > $file
        set len = `wc -l $file | colrm 9`
        echo $file $len | spacer 
        set len = `wc -l $file | colrm 9`
        if($len >= 19) then
                echo "Warning: hk("$i") is too long"
                set warn = "yes"
        endif
        @ i = $i + 1
        set file = ".hk"$i
      end

# execute csh replace
      replace

# exit if too many continuation lines
      if $warn == "yes" then
         echo ""
         echo "compiling error: too many continuation lines"
         echo ""
      endif

      if $warn == "yes" then
         echo ""
         exit
      endif

leave:

      echo ""
      echo -n "Do you want to abort this run? (y=yes,n=no): " 
      set wish=$<
      if ($wish == "y") then
         echo ""
         exit
      else if ( $wish =="" ) then 
              goto leave 
           endif
      endif

ques:

      echo ""
      echo -n "Do you wish to edit the resulting functions? (y=yes,n=no): "
      set junk=$<
      if ( ( $junk == "y" ) || ( $junk == "Y" ) ) then
         echo ""
 echo 'Type ":w" to write the changes, ":n" to edit the next file and ":q\!" to'
         echo 'finish editing.'
         echo ""
         echo -n "Hit return to continue: "
         set junk=$<
         vi makegk.f makeak.f makehk.f
      else if ( $junk == "" ) then
	      goto ques
           endif
      endif

# compile dsaprx

      echo ""
      echo "This will take a few seconds to compile and link the program."

      if { make } then

        f77 -o check check.o makegk.o makeak.o makehk.o

        check

      endif
     

end:
      rm -f /tmp/temp /tmp/$$file 
