#! /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":
    set tmp = (`fgrep "#define" /usr/include/features.h | fgrep "__GLIBC_MINOR__"`)
    if ("$tmp" == "") then
	set _os_minor = "-libc5"
    else
	set _os_minor = "-libc6.$tmp[3]"
    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
	case "grizzly":
	case "bison":
	case "coyote":
	case "eagle":
	case "coyote.ras.ucalgary.ca":
	case "pika.ras.ucalgary.ca":
	case "lynx.ras.ucalgary.ca":
	case "eagle.ras.ucalgary.ca":
	case "bobcat":
	case "caribou":
	case "cougar":
	    set oldroot = ~rgooch/karma
	    set oldbase = ${oldroot}/${MACHINE_OS}${_os_minor}
	    set newroot = /n/home/karma
	    set newbase = /usr/local/karma
	    echo "Using production version of ${MACHINE_OS} Karma"
	    breaksw
	default:
	    set oldroot = /atpkg/karmaexp
	    set oldbase = /atpkg/karmaexp/$MACHINE_OS
	    set newroot = /nfs/atapplic/karma
	    set newbase = /usr/local/karma
	    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
	case "grizzly":
	case "bison":
	case "coyote":
	case "eagle":
	case "coyote.ras.ucalgary.ca":
	case "pika.ras.ucalgary.ca":
	case "lynx.ras.ucalgary.ca":
	case "eagle.ras.ucalgary.ca":
	case "bobcat":
	case "caribou":
	case "cougar":
	    set oldroot = /n/home/karma
	    set oldbase = /usr/local/karma
	    set newroot = ~rgooch/karma
	    set newbase = ${newroot}/${MACHINE_OS}${_os_minor}
	    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:
	    set oldroot = /nfs/atapplic/karma
	    set oldbase = /usr/local/karma
	    set newroot = /atpkg/karmaexp
	    set newbase = /atpkg/karmaexp/$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
    endsw
    #if ("$user" == "rgooch") unsetenv MAKE_STATIC_KLIBS
endif

if ("$oldroot" != "$KARMAROOT") then
    echo "KARMAROOT: $KARMAROOT is not $oldroot"
    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
if (-d $KARMABASE/static-lib) setenv KARMASTATICLIBPATH $KARMABASE/static-lib
setenv KARMAINCLUDEPATH $KARMABASE/include
setenv KARMAROOT $newroot
setenv PATH `echo "$PATH" | sed -e "s*${oldbinpath}*${KARMABINPATH}*g" | sed -e "s*${oldbase}/*${newbase}/*g" | sed -e "s*${oldroot}/*${newroot}/*g"`
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
