#! /bin/csh
#

#
#  Choose your compilers.
#


which g77 >& /dev/null
if ( $status == 0 ) then
	setenv F_COMPILER g77
	echo Using the g77 compiler.
else
	which fort77 >& /dev/null
	if ( $status == 0 ) then
		setenv F_COMPILER fort77
		echo Using the fort77 compiler.
	else
		echo The FORTRAN compiler is not in the current path:
		echo  $PATH
		echo As a result, I am unable to build the toolkit.
		exit 1;
	endif
endif

set C_COMPILER  =  "gcc"

#
#  What compile options do we want to use? If they were 
#  set somewhere else, use those values.  The same goes
#  for link options.
#
if ( $?F_COMPILEOPTIONS ) then
   echo " "
   echo "      Using Fortran compile options: "
   echo "      $F_COMPILEOPTIONS"
else
   set F_COMPILEOPTIONS = "-c -C"
   echo " "
   echo "      Setting default Fortran compile options:"
   echo "      $F_COMPILEOPTIONS"
endif

if ( $?C_COMPILEOPTIONS ) then
   echo " "
   echo "      Using C compile options: "
   echo "      $C_COMPILEOPTIONS"
else
   set C_COMPILEOPTIONS = "-c"
   echo " "
   echo "      Setting default C compile options:"
   echo "      $C_COMPILEOPTIONS"
endif

if ( $?TKLINKOPTIONS ) then
   echo " "
   echo "      Using link options: "
   echo "      $TKLINKOPTIONS"
else
   set TKLINKOPTIONS = " "
   echo " "
   echo "      Setting default link options:"
   echo "      $TKLINKOPTIONS"
endif

echo " "

#
#   Determine a provisional LIBRARY name.
#
   foreach item ( `pwd` )
      set LIBRARY = $item:t
   end

#
#  Are there any *.f files that need to be compiled?
#
\ls *.f >& /dev/null

if ( $status == 0 ) then

   foreach SRCFILE ( *.f )
      echo "      Compiling: "   $SRCFILE
      $F_COMPILER $F_COMPILEOPTIONS $SRCFILE
   end

endif

#
#  Are there any *.c files that need to be compiled?
#
\ls *.c >& /dev/null

if ( $status == 0 ) then

   foreach SRCFILE ( *.c )
      echo "      Compiling: "   $SRCFILE
      $C_COMPILER $C_COMPILEOPTIONS $SRCFILE
   end

endif


echo " "

#
#  If object files exist, we need to create an object library.
#

\ls *.pgm >& /dev/null

if ( $status == 0 ) then
   set LIBRARY = "locallib"
endif

\ls *.o >& /dev/null

if ( $status == 0 ) then

   echo "      Inserting objects in the library $LIBRARY ..."
   ar  crv $LIBRARY.a *.o
   ranlib  $LIBRARY.a
   \rm                *.o    
   echo " "

endif

#
#  If there are any main programs in the directory, compile
#  them. If they have their own locallib.a link with it in addition
#  to the default libraries.
#

\ls *.pgm >& /dev/null

if ( $status == 0 ) then

   echo " "

   foreach MAIN ( *.pgm )
      set TARGET  = $MAIN:r.f
      set MAINOBJ = $MAIN:r.o
      set EXECUT = $MAIN:r
      \cp $MAIN  $TARGET
   
      echo "      Compiling and linking: " $MAIN
      
      if ( -e locallib.a ) then

         $F_COMPILER    $F_COMPILEOPTIONS $TARGET
         $F_COMPILER -o $EXECUT           $MAINOBJ             \
                                          locallib.a           \
                                          /naif/toolkit/lib/mgsspice.a \
                                          /naif/toolkit/lib/support.a  \
                                          /naif/toolkit/lib/spicelib.a \
                                          $TKLINKOPTIONS    

         \rm $TARGET
         \rm $MAINOBJ
         \rm locallib.a 

      else

         echo "Compiling and linking: " $MAIN     
         $F_COMPILER    $F_COMPILEOPTIONS $TARGET
         $F_COMPILER -o $EXECUT           $MAINOBJ             \
                                          /naif/toolkit/lib/mgsspice.a \
                                          /naif/toolkit/lib/support.a  \
                                          /naif/toolkit/lib/spicelib.a \
                                          $TKLINKOPTIONS
 
         \rm $TARGET
         \rm $MAINOBJ

      endif

   end

endif

#
#  Cleanup.
#

echo " "

\ls *.o >& /dev/null

if ( $status == 0 ) then
   \rm *.o
endif


exit 0


