#!/bin/csh -f
#
#  Master Makefile for Otheroption server
#   - note that only makefile.extention files are 'make'd
#
#  Accepts 2 kinds of command line arguments:
#    1.  clean   (opt-build clean)  will make tree clean of targets
#    2.  subdirectory (opt-build lib) will make only that subdir's targets
#
source opt-build-env
#
#  make the stuff in lib, first - others depend on these
#
  set LIST=`ls -1 optsrc/lib/makefile.* | grep -v "~"`
#
#  then, make the methods
#
  set LIST=($LIST `ls -1 optsrc/methods/makefile.* | grep -v "~"`)
#
#  finally, make other subdirectory stuff ONE LEVEL under optsrc
#     
  set LIST=($LIST `ls -1 optsrc/*/makefile.* | grep -v "/lib/" | grep -v "/methods/" | grep -v "~"`)
#
# if no arguments given on command line, make everything
#
if ( "x$1" == "x" ) then
#
# Go through and make stuff
#
 foreach g (${LIST})
    echo making $g ...
    pushd $g:h > /dev/null
    make -f $g:t
    popd > /dev/null
 end
else 
  switch ( $1 )
#
# clean up all .o files and all targets - ready for a fresh build
#
    case "clean":
      foreach i (${LIST})
        echo cleaning $i ...
        pushd $i:h > /dev/null
        make -f $i:t clean
        popd > /dev/null
      end      
      breaksw
    default:
      set LIST=`ls optsrc/$1/makefile.* | grep -v "~"`
      if ("x$LIST" != "x") then
        foreach m (${LIST})
          echo making $m ...
          pushd $m:h > /dev/null
          make -f $m:t
          popd > /dev/null
        end
      else 
        echo "Error!  Either the subdirectory optsrc/$1 is not found"
        echo "        or there are no makefile.X files in that subdirectory"
        echo "        where .X represents a file extension."
      endif
      breaksw
  endsw
endif
