#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi
if [ "${1:-}" = "-c" ] ; then dont_complain=1; shift; else dont_complain=0; fi

# $Id$

# try to determine which enstore system the user wants based on his node
# This script is called by the enstore/encp table files.

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

# This is an FNAL specific script.  Exit early if not running on the 
# .fnal.gov domain.  Get the full hostname, then check the last 9 characters
# for ".fnal.gov"
fqdn=`uname -n`
domain_index_check=`expr \`echo $fqdn | wc -c\` - 9`
if [ `echo $fqdn | cut -c ${domain_index_check}-` != ".fnal.gov" ]; then
   #This is not an FNAL node.
   exit 0
fi

d0en=conf-d0en.fnal.gov
stken=conf-stken.fnal.gov
cdfen=conf-cdfen.fnal.gov
gccen=conf-gccen.fnal.gov

dmsen=dmsen02.fnal.gov
happy=happy.fnal.gov
fndapr=fndapr.fnal.gov
h2oen=131.225.81.68
woodlawn=woodlawn.fnal.gov
kenwood=kenwood.fnal.gov

#lancs=192.9.200.103 # THE UK lancaster group on bulk1.ukwork

qualifier=""
complain=1

get_logname()
{
    #Logname is the posix way.  It just doesn't work from crons.
    which logname > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	the_logname=`logname 2> /dev/null`
	if [ $? -eq 0 ]; then
	    echo $the_logname
	    return 0
	fi
    fi
    which whoami > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	the_logname=`whoami 2> /dev/null`
	if [ $? -eq 0 ]; then
	    echo $the_logname
	    return 0
	fi
    fi
    which id > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	the_logname=`id -nu 2> /dev/null`
	if [ $? -eq 0 ]; then
	    echo $the_logname
	    return 0
	fi
    fi
    return 1
}

# default for environment variables with a default
ENSTORE_MAIL=enstore-auto@fnal.gov
if [ `get_logname`x = 'dcache'x ]; then ENSTORE_MAIL=dcache-auto@fnal.gov; fi

case $node in

  stkenscan*)
	    ENSTORE_CONFIG_HOST=stkenscan1.fnal.gov
	    ENSTORE_CONFIG_FILE=offline.conf
	    complain=0
	    ;;

  stken*|cmspnfs*|eagpnfs*)
	    ENSTORE_CONFIG_HOST=$stken
	    ENSTORE_CONFIG_FILE=stk.conf
	    qualifier=stken
	    complain=0
	    ;;

  d0en*)    ENSTORE_CONFIG_HOST=$d0en
	    ENSTORE_CONFIG_FILE=sam.conf
	    qualifier=d0en
	    complain=0
	    ;;

  cdfen*)   ENSTORE_CONFIG_HOST=$cdfen
	    ENSTORE_CONFIG_FILE=cdf.conf
	    qualifier=cdfen
	    complain=0
	    ;;

  *minos*)  ENSTORE_CONFIG_HOST=$stken
	    ENSTORE_CONFIG_FILE=stk.conf
	    qualifier=stken
	    complain=0
	    ;;

  *cdf*|*b0*)
	    ENSTORE_CONFIG_HOST=$cdfen
	    ENSTORE_CONFIG_FILE=cdf.conf
	    qualifier=cdfen
	    ;;

  *sam*|*d0*)
	    ENSTORE_CONFIG_HOST=$d0en
	    ENSTORE_CONFIG_FILE=sam.conf
	    qualifier=d0en
	    ;;

  gccen*)   ENSTORE_MAIL=moibenko@fnal.gov,litvinse@fnal.gov
	    ENSTORE_CONFIG_HOST=$gccen
	    ENSTORE_CONFIG_FILE=gccen.conf
	    complain=0
	    ;;
  dmsen*)   ENSTORE_MAIL=moibenko@fnal.gov,litvinse@fnal.gov
	    ENSTORE_CONFIG_HOST=$dmsen
	    ENSTORE_CONFIG_FILE=dmsen.conf
	    complain=0
	    ;;

  *)    ENSTORE_CONFIG_HOST=$stken
	    ENSTORE_CONFIG_FILE=stk.conf
	    qualifier=stken
	    ;;

esac

# ENSTORE_USER_DEFINED_CONFIG_FILE allows user to override default
# If ENSTORE_USER_DEFINED_CONFIG_FILE is set, ENSTORE_CONFIG_FILE will
# be assigned by $ENSTORE_USER_DEFINED_CONFIG_FILE
if [ "${ENSTORE_USER_DEFINED_CONFIG_FILE:-x}" != "x" ]
then
    ENSTORE_CONFIG_FILE=${ENSTORE_USER_DEFINED_CONFIG_FILE}
    # assuming the user who sets ENSTORE_USER_DEFINED_CONFIG_FILE knows
    # what he/she is doing
    complain=0
fi
# ENSTORE_USER_DEFINED_MAIL allows user to override default
# If ENSTORE_USER_DEFINED_MAIL is set, ENSTORE_MAIL will
# be assigned by $ENSTORE_USER_DEFINED_MAIL
if [ "${ENSTORE_USER_DEFINED_MAIL:-x}" != "x" ]
then
    ENSTORE_MAIL=${ENSTORE_USER_DEFINED_MAIL}
    # assuming the user who sets ENSTORE_USER_DEFINED_CONFIG_FILE knows
    # what he/she is doing
    complain=0
fi
# ENSTORE_USER_DEFINED_CONFIG_HOST allows user to override default
# If ENSTORE_USER_DEFINED_CONFIG_HOST is set, ENSTORE_CONFIG_HOST will
# be assigned by $ENSTORE_USER_DEFINED_CONFIG_HOST
if [ "${ENSTORE_USER_DEFINED_CONFIG_HOST:-x}" != "x" ]
then
    ENSTORE_CONFIG_HOST=${ENSTORE_USER_DEFINED_CONFIG_HOST}
    # if ENSTORE_USER_DEFINED_CONFIG_FILE is not set, the value for
    # ENSTORE_CONFIG_FILE will be guessed based on the value of
    # $ENSTORE_CONFIG_HOST (same as ${ENSTORE_USER_DEFINED_CONFIG_HOST}
    # for now)
    if [ "${ENSTORE_USER_DEFINED_CONFIG_FILE:-x}" = "x" ]
    then
	case $ENSTORE_CONFIG_HOST in
	    stken*)
		ENSTORE_CONFIG_FILE=stk.conf
		;;
	    d0en*)
		ENSTORE_CONFIG_FILE=sam.conf
		;;
	    cdfen*)
		ENSTORE_CONFIG_FILE=cdf.conf
		;;
	    *)
		ENSTORE_CONFIG_FILE=stk.conf
		;;
	esac
    fi
    complain=0
fi

if [ $dont_complain -eq 1 ]; then
  #We want to override this warning message.
  complain=0;
fi


if [ "${1:-x}" = "x" ]; then
  if [ $complain -eq 1 ]; then
    # note that these warnings go to stderr
    echo "WARNING: based on your node, `uname -n`, ENSTORE_CONFIG_HOST has been set to $ENSTORE_CONFIG_HOST" 1>&2
    echo "WARNING: If this is not correct; either reset ENSTORE_CONFIG_HOST by hand, set ENSTORE_USER_DEFINED_CONFIG_HOST by hand before running setup or use a qualifier in your setup command!" 1>&2
  fi
  # need to feed this back up to the en*.table file on stdout to set it in the users's environment
  echo $ENSTORE_CONFIG_HOST
elif [ "${1:-x}" = "file" ]; then
  if [ $complain -eq 1 ]; then
    # note that these warnings go to stderr
    echo "WARNING: based on your node, `uname -n`, ENSTORE_CONFIG_FILE has been set to $ENSTORE_CONFIG_FILE" 1>&2
    echo "WARNING: If this is not correct; either reset ENSTORE_CONFIG_FILE by hand, set ENSTORE_USER_DEFINED_CONFIG_FILE by hand before running setup or use a qualifier in your setup command!" 1>&2
  fi
  # need to feed this back up to the en*.table file on stdout to set it in the users's environment
  echo $ENSTORE_CONFIG_FILE
elif [ "${1:-x}" = "mail" ]; then
  if [ $complain -eq 1 ]; then
    # note that these warnings go to stderr
    echo "WARNING: based on your node, `uname -n`, ENSTORE_MAIL has been set to $ENSTORE_MAIL" 1>&2
    echo "WARNING: If this is not correct, either reset ENSTORE_MAIL by hand, set ENSTORE_USER_DEFINED_MAIL by had before running setup or use a qualifier in your setup command!" 1>&2
  fi
  # need to feed this back up to the en*.table file on stdout to set it in the users's environment
  echo $ENSTORE_MAIL
elif [ "${1:-x}" = "qualifier" ]; then
  if [ $complain -eq 1 ]; then
    # note that these warnings go to stderr
    echo "WARNING: based on your node, `uname -n`, the qualifier to use is ${qualifier}." 1>&2
  fi
  # need to feed this back up to the en*.table file on stdout to set it in the users's environment
  echo $qualifier
fi
