#! /bin/sh
#Copyright 02/21/01 Sun Microsystems, Inc. All rights reserved.
#pragma ident "@(#)get_local_host	1.6 01/02/21 Sun Microsystems"

###############################################################################
# get_local_host
###############################################################################
#
# Return the name of the local host.  In a cluster, this is the logical
# host name.  Outside of a cluster, it's the physical host name.
#
###############################################################################

DS="em_services"
phyhost=`/bin/uname -n | /usr/bin/cut -f1 -d'.'`

# check if cluster software installed
if /bin/pkginfo -q SUNWscr ; then
   PATH=/usr/cluster/bin:$PATH
   # Find all resource groups
   res_grps=`scha_cluster_get -O all_resourcegroups`
   
   # Find which group has em_services
   found=FALSE
   for grp in $res_grps ;  do
      foundLH=FALSE
      foundDS=FALSE
      lh=
      for res in `scha_resourcegroup_get -O resource_list -G $grp`; do
         if [ `scha_resource_get -O is_logical_hostname -R $res` = "TRUE" ] ; then
            foundLH=TRUE
            lh="$lh $res"
         fi
         if [ $res = $DS ]; then 
            foundDS=TRUE
         fi
         if [ $foundLH = TRUE ] && [ $foundDS = TRUE ]; then
            found=TRUE
            break 2
         fi
      done 
   done
   
   if [ $found = TRUE ]; then
      for host in $lh; do
         hostname=`scha_resource_get -O extension -R $host -G $grp HostnameList |\
                   /bin/awk 'NR == 2 { print }'`
         break
      done
   else
      echo $phyhost
      exit 1
   fi

   if [ `scha_resource_get -O resource_state -R $host | cut -c1-6` = ONLINE ]; then
      echo $hostname
      exit 0
   else 
      echo $phyhost
      exit 1
   fi

else
    echo $phyhost
    exit 0
fi

