#!/bin/csh
unalias cd echo eval exit pwd set

# Assume that this script is invoked as "foo"
# if the shell variable FOO is set then the cache location is
#	$FOO
# otherwise the cache location is
#	./FOO
# if the shell variable FOO_SYS is set then the system cache location is
#	$FOO_SYS$machine_subdir
# otherwise the system cache location is
#	$sys_cache_parent/FOO$machine_subdir

set machine_subdir = ''
#set machine_subdir = /`mach`

set sys_cache_parent = /kirk/tools

alias LN /bin/ln
alias MKDIR /bin/mkdir
alias MV /bin/mv
alias PWD 'pwd | sed s,^/tmp_mnt,,' # hack for Sun auto-mounter
alias RM /bin/rm
alias TR /usr/bin/tr

unset cache drvgrf new_cache reset_cache quit
while ($#argv > 0)
   switch ($1)
      # use specified cache rather than the default cache
      case -c:
	 set cache = $2
	 if ($#argv < 2) goto default
	 shift; shift; breaksw
      # install a new derivation graph
      case -d:
	 set drvgrf = $2
	 set reset_cache
	 if ($#argv < 2) goto default
	 shift; shift; breaksw
      # create a new cache
      case -n:
	 set new_cache = $2
	 if ($#argv < 2) goto default
	 shift; shift; breaksw
      # reset the cache (clear all derived objects)
      case -r:
	 set reset_cache
	 shift; breaksw
      # quit after performing cache operations
      case -q:
	 set quit
	 shift; breaksw
      default:
	 set name = $0
	 echo "usage: $name:t [-r] [-c cache] [-d derivation_graph] [-n new_cache] [-q]"
	 exit 1
	 endsw
   end #while

#select
   if ($?cache) then
      if (! -d $cache) then
	 echo "Cache location not found : $cache."
	 exit 2
	 endif
   else
      set cache_var = `echo $0 | TR '[a-z]' '[A-Z]'`
      set cache_var = $cache_var:t
      set cache = $cache_var
      eval 'set cache_var_set = $?'$cache_var
      if ($cache_var_set) eval 'set cache = $'$cache_var
      if (! -d $cache) then
	 if (! $?new_cache) set new_cache = $cache
	 set cache = $sys_cache_parent/$cache_var
	 eval 'set sys_cache_var_set = $?'${cache_var}_SYS
	 if ($sys_cache_var_set) eval 'set cache = $'${cache_var}_SYS
	 set cache = $cache$machine_subdir
	 if (! -d $cache) then
	    echo 'Use -c, set $'$cache_var' to the name of a working cache,'
	    echo 'or set $'${cache_var}_SYS' to the name of a master cache.'
	    exit 3
	    endif
	 endif
     endif #select

if (! -e $cache/odin.exe || ! -d $cache/Cmd || ! -d $cache/Tool) then
   echo "Invalid cache at $cache."
   exit 4
   endif

set cache = `cd $cache; PWD`

if ($?new_cache) then
   if (-e $new_cache) then
      echo "Existing version of $new_cache renamed to be $new_cache.$$"
      MV $new_cache $new_cache.$$
      if ($status != 0) exit $status
      endif
   
   MKDIR $new_cache
   if ($status != 0) then
      echo "Cannot create cache directory: $new_cache"
      exit 5
      endif
   LN -s $cache/{odin.exe,Tool,Cmd} $new_cache
   if (-e $cache/local.dg.tab) then
      LN -s $cache/local.dg.tab $new_cache
      endif
   set cache = `cd $new_cache; PWD`
   echo "New cache created at $new_cache."
   endif

if ($?drvgrf) then
   if (! -e $cache/Tool/dg.exe) then
      echo "$cache/Tool/dg.exe does not exist"
      exit 5
      endif
   $cache/Tool/dg.exe < $drvgrf > local.dg.out
   if ($status != 0) then
      echo "Error installing $drvgrf for $cache.  odin request aborted."
      exit 6
      endif
   MV DRVGRF $cache/local.dg.tab
   RM DG.c
   echo "$drvgrf installed as the derivation graph for $cache."
   endif

if ($?reset_cache) then
   RM -rf $cache/{FILES,DEBUG,INFO,LOCAL}
   echo "Cache at $cache has been reset."
   endif

if (! -w $cache/INFO && (-e $cache/INFO || ! -w $cache)) then
   echo "Cache directory is not writable: $cache"
   exit 7
   endif

if ($?quit) exit 0

setenv ODIN $cache
exec $cache/odin.exe
