# this file must be sourced
echo `date`

# figure out how to setup enstore on the wildly different nodes at the lab

node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`

timed_cmd () 
{ 
    export t="${1:-10}";
    export c="${2:-id}";
    echo /bin/sh -c "(sleep $t;kill \$\$) >/dev/null 2>&1 &p=\$!; eval \"$c\"; kill \$p 2>/dev/null"
}

# We always run out of a enstore home area, somewhere
# Too bad we can't figure out the name of this sourced script 
#   as it already contains the enstore path!

if [ -r /usr/local/bin/ENSTORE_HOME ]; then
   . /usr/local/bin/ENSTORE_HOME
else
   echo `date` ERROR: Can NOT determine E_H.  Add /usr/local/bin/ENSTORE_HOME link
   exit 1
fi

# make sure ups is configured

fs=$E_H/dcache-deploy/scripts/findsetup.sh
if [ -r $fs ]; then 
  #echo `date` Setting up ups on `uname -n` via script $fs
  . $fs
else
  echo `date` ERROR. Can not find \"$fs\"
  exit 1
fi
if [ -z "$UPS_DIR" ]; then 
  echo `date` ERROR. UPS is not setup correctly
  #exit 1
fi 

se=$E_H/enstore/setup-enstore
if [ -r $se ]; then
  #echo `date` Setting up enstore on `uname -n` via script $se
  hereiam=`pwd`
  . $se
  cd $hereiam
else
  #echo `date` Can not find file \"$se\"
  #echo `date` Trying \"setup enstore\"
  setup enstore >/dev/null 2>&1
fi
if [ -z "$ENSTORE_DIR" ]; then 
  echo `date` ERROR. ENSTORE is not setup correctly.  No \'setup enstore\' or setup-enstore found
  exit 1
else
  case $ENSTORE_CONFIG_HOST in
	stken*) DCACHE_E_CONFIG_HOST=stkendca3a.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
	cdfen*) DCACHE_E_CONFIG_HOST=cdfdcam.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
	d0*)    DCACHE_E_CONFIG_HOST=d0dcam.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
	     *) DCACHE_E_CONFIG_HOST=stkendca3a.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
  esac
  case $node in 
	cmsdcdr1*|cmsdcdr2*|gyoza7*|ketchup*|mustard*|pesto*|chutney*|mayo*|chocolat*) 
	        DCACHE_E_CONFIG_HOST=cmsdcdr2.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
	fcdfdcache5*|fcdfdcache6*|fcdfdata008*|fcdfdata009*|fcdfdata010*|fcdfdata011*)
	        DCACHE_E_CONFIG_HOST=cdfdcatm.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
	cachen1a*|cachen2a*)
	        DCACHE_E_CONFIG_HOST=cachen1a.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
      lqcd*)    DCACHE_E_CONFIG_HOST=lqcd.fnal.gov
                DCACHE_E_CONFIG_PORT=6500
	;;
  esac


  export DCACHE_E_CONFIG_HOST
  export DCACHE_E_CONFIG_PORT
  echo `date` ENSTORE_DIR=$ENSTORE_DIR. `type encp`  $ENSTORE_CONFIG_HOST $ENSTORE_CONFIG_PORT $DCACHE_E_CONFIG_HOST $DCACHE_E_CONFIG_PORT
fi 


echo `date`
