#! /bin/echo Must be sourced, not executed

# usage: switch-karma  production|experimental

if ("$#argv" != "1") then
    echo "Usage: switch-karma  production|experimental"
    echo "Found $#argv arguments, expecting 1"
    if ("$shell:t" == "csh") then
	echo "You appear to be using the standard C shell. Use tcsh instead".
    endif
    exit 1
endif

if ("$?HOSTNAME" == "0") then
    # Find out hostname: so many ways...
    if (-x /bin/uname) then
	setenv HOSTNAME `/bin/uname -n`
    else if (-x /usr/bin/uname) then
	setenv HOSTNAME `/usr/bin/uname -n`
    else if (-x /bin/hostname) then
	setenv HOSTNAME `/bin/hostname`
    else if (-x /usr/bin/hostname) then
	setenv HOSTNAME `/usr/bin/hostname`
    else
	echo "I don't know how to find out your hostname. Not another way!"
	exit 1
    endif
endif

set _os_minor = ""

switch ("$OS")
  case "IRIX5":
  case "IRIX6":
  case "Solaris":
    breaksw
  case "Linux":
    if ("$OS_MINOR" == "") then
	set _os_minor = "-libc5"
    else
	set _os_minor = "-$OS_MINOR"
    endif
    breaksw
  default:
    exit 0
    breaksw
endsw

switch ("$HOSTNAME")
  case "vindaloo":
  case "workaholix":
  case "mobilix":
  case "tinylinux":
    exit 0
    breaksw
endsw

if ("$argv[1]" == "production") then
    if ( ! -d /usr/local/karma ) then
	echo "Production version of sparc_Solaris Karma not available"
	exit 2
    endif
    switch ("$HOSTNAME")
	case "raptor":
	    set oldroot = /usr/local/karma-exp
	    set oldbase = ${oldroot}/${MACHINE_OS}
	    set newroot = /nfs/atapplic/karma
	    set newbase = /usr/local/karma
	    echo "Using production version of ${MACHINE_OS} Karma"
	    breaksw
	case "mayhem":
	    set oldroot = /mayhem/karma
	    set oldbase = ${oldroot}/${MACHINE_OS}
	    set newroot = /nfs/atapplic/karma
	    set newbase = /usr/local/karma
	    echo "Using production version of ${MACHINE_OS} Karma"
	    breaksw
	case "b1":
	    set oldroot = /usr/ratnet/home/karma
	    set oldbase = ${oldroot}/${MACHINE_OS}
	    set newroot = /nfs/atapplic/karma
	    set newbase = /usr/local/karma
	    echo "Using production version of ${MACHINE_OS} Karma"
	    breaksw
	case "potoroo.tip.csiro.au":
	    set oldroot = /home/karmaexp
	    set oldbase = ${oldroot}/${MACHINE_OS}
	    set newroot = /nfs/atapplic/karma
	    set newbase = /usr/local/karma
	    echo "Using production version of ${MACHINE_OS} Karma"
	    breaksw
	default:
	    if ("$MACHINE_SITE" == "RAS") then
		set oldroot = ~rgooch/karma
		set oldbase = /usr/local/karmaexp
		set newroot = /n/home/karma
		set newbase = /usr/local/karma
	    else
		set oldroot = /atpkg/karmaexp
		set oldbase = /atpkg/karmaexp/$MACHINE_OS
		set newroot = /nfs/atapplic/karma
		set newbase = /usr/local/karma
	    endif
	    echo "Using production version of ${MACHINE_OS} Karma"
	    breaksw
    endsw
    #if ("$user" == "rgooch") setenv MAKE_STATIC_KLIBS TRUE
endif

if ("$argv[1]" == "experimental") then
    switch ("$HOSTNAME")
	case "raptor":
	    set oldroot = /nfs/atapplic/karma
	    set oldbase = /usr/local/karma
	    set newroot = /usr/local/karma-exp
	    set newbase = ${newroot}/${MACHINE_OS}
	    if ( ! -d $newbase ) then
		echo "Experimental version of $MACHINE_OS Karma not available"
		exit 2
	    endif
	    echo "Using experimental version of ${MACHINE_OS} Karma"
	    breaksw
	case "mayhem":
	    set oldroot = /nfs/atapplic/karma
	    set oldbase = /usr/local/karma
	    set newroot = /mayhem/karma
	    set newbase = ${newroot}/${MACHINE_OS}
	    if ( ! -d $newbase ) then
		echo "Experimental version of $MACHINE_OS Karma not available"
		exit 2
	    endif
	    echo "Using experimental version of ${MACHINE_OS} Karma"
	    breaksw
	case "b1":
	    set oldroot = /nfs/atapplic/karma
	    set oldbase = /usr/local/karma
	    set newroot = /usr/ratnet/home/karma
	    set newbase = ${newroot}/${MACHINE_OS}
	    if ( ! -d $newbase ) then
		echo "Experimental version of $MACHINE_OS Karma not available"
		exit 2
	    endif
	    echo "Using experimental version of ${MACHINE_OS} Karma"
	    breaksw
	case "potoroo.tip.csiro.au":
	    set oldroot = /nfs/atapplic/karma
	    set oldbase = /usr/local/karma
	    set newroot = /home/karmaexp
	    set newbase = ${newroot}/${MACHINE_OS}
	    if ( ! -d $newbase ) then
		echo "Experimental version of $MACHINE_OS Karma not available"
		exit 2
	    endif
	    echo "Using experimental version of ${MACHINE_OS} Karma"
	    breaksw
	default:
	    if (-d /usr/local/karmaexp) then
		set oldroot = /n/home/karma
		set oldbase = /usr/local/karma
		set newroot = ~rgooch/karma
		set newbase = /usr/local/karmaexp
	    else
		set oldroot = /nfs/atapplic/karma
		set oldbase = /usr/local/karma
		set newroot = /atpkg/karmaexp
		set newbase = /atpkg/karmaexp/$MACHINE_OS
	    endif
	    if ( ! -d $newbase ) then
		echo "Experimental version of $MACHINE_OS Karma not available"
		exit 2
	    endif
	    echo "Using experimental version of ${MACHINE_OS} Karma"
	    breaksw
    endsw
    #if ("$user" == "rgooch") unsetenv MAKE_STATIC_KLIBS
endif

if ("$oldbase" != "$KARMABASE") then
    echo "KARMABASE: $KARMABASE is not $oldbase"
    exit 1
endif

setenv KARMABASE $newbase
if ("$?newbinpath" == "0") then
    setenv KARMABINPATH $KARMABASE/bin
else
    setenv KARMABINPATH $newbinpath
endif
if ("$?oldbinpath" == "0") then
    set oldbinpath = ${oldbase}/bin
endif
setenv KARMALIBPATH $KARMABASE/lib
setenv KARMASTATICLIBPATH $KARMABASE/static-lib
setenv KARMASTATICBINPATH $KARMABASE/static-bin
setenv KARMAINCLUDEPATH $KARMABASE/include
setenv KARMAROOT $newroot

if ("$?_newpath" == 0) then
    set _np = ($path)
else
    set _np = ($_newpath)
endif
set _np = (`echo "$_np" | sed -e "s*${oldbinpath}*${KARMABINPATH}*g" | sed -e "s*${oldbase}/*${newbase}/*g" | sed -e "s*${oldroot}/*${newroot}/*g"`)
if ("$?_newpath" == 0) then
    set path = ($_np)
else
    set _newpath = ($_np)
endif
unset _np

set _cdpath = ()
foreach i ($cdpath)
    if ("$i" == "$oldroot") then
	set _cdpath = ($_cdpath $newroot)
    else
	set _cdpath = ($_cdpath $i)
    endif
end
set cdpath = ($_cdpath)
unset _cdpath

# Set version number environment variable
set _versionfile = $KARMAINCLUDEPATH/k_version.h
set tmp = `fgrep KARMA_VERSION $_versionfile | tr '"' ' '`
setenv KARMA_VERSION "$tmp[$#tmp]"
if ("$KARMA_VERSION" == "") then
    echo "WARNING: KARMA_VERSION environment variable could not be computed."
endif
unset _versionfile

if (-r ~/.karma/.switch-karma.rc) then
    source ~/.karma/.switch-karma.rc
endif
