#!/bin/sh
#
#
#
# Sun Grid Engine service tag management script
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__


#---------------------------------------------------------------------------
# This script requires the that $SGE_ROOT and $SGE_CELL be set
#

if [ -z "$SGE_ROOT" -o -z "$SGE_CELL" ]; then
   echo "\$SGE_ROOT and \$SGE_CELL must be set to valid values"
   exit 1
fi

if [ -z "$ARCH" ]; then
   ARCH=`$SGE_ROOT/util/arch`
fi

if [ -z "$V5UTILBIN" ]; then
   V5UTILBIN=$SGE_ROOT/utilbin/$ARCH              # adminrun, infotext, openssl, uidgid
fi

if [ ! -d "$V5UTILBIN" ]; then
   echo
   echo "Error: The utilbin directory "$V5UTILBIN" does not exist"
   echo "Please verify your setup and restart this script. Exit."
   exit 1
fi

if [ -z "$INFOTEXT" ]; then
   INFOTXT_DUMMY=$V5UTILBIN/infotext
   INFOTEXT=$INFOTXT_DUMMY
   if [ ! -x $INFOTXT_DUMMY ]; then
      echo "Error: Can't find binary \"$INFOTXT_DUMMY\""
      echo "Please verify your setup and restart this script. Exit."
      exit 1
   fi

   # Test the infotext binary
   tmp=`$INFOTEXT test 2>&1`
   if [ $? -ne 0 ]; then
      echo "Error: Execution of $INFOTEXT failed: $tmp"
      echo "Please verify your setup and restart this script. Exit."
      exit 1
   fi

   SGE_INFOTEXT_MAX_COLUMN=5000; export SGE_INFOTEXT_MAX_COLUMN
fi

# ST are supported just on linux and solaris
case $ARCH in
  lx* | sol*)
   ;;
  *) 
   if [ -z "$2" ]; then
      $INFOTEXT "The service tags are not supported"
   fi  
   exit 0
   ;;  
esac


#customized cut
CUT=cut

#---------------------------------------------------------------------------
# show the usage of the standalone command
# if ADMINUSER = default then execute command unchanged
STusage()
{
   $INFOTEXT "sge_st enable|disable|register|unregister|check|enabled|supported|help"
   $INFOTEXT " -------------------------------------------------------------------------------"
   $INFOTEXT "       enable   ... enable The Service Tags"
   $INFOTEXT "       disable  ... disable The Service Tags"
   $INFOTEXT "       register ... register product"
   $INFOTEXT "       unregister . unregister product"
   $INFOTEXT "       check    ... check if the product is registered"
   $INFOTEXT "       enabled  ... check if the Service Tags are enabled"
   $INFOTEXT "       supported .. check if the Service Tags packages are installed on host"
   $INFOTEXT "       help     ... this help"
}

#-------------------------------------------------------------------------
#arch_variables Setting up common variables and paths
#
STArchVariables()
{
   if [ -f "$SGE_ROOT/util/arch_variables" ]; then
     . $SGE_ROOT/util/arch_variables
   else
      $INFOTEXT "Could not source arch_variables"
      exit 1
   fi
}

#-------------------------------------------------------------------------
#Service Tags Setting up 
#
STBasicSettings()
{  
   stclient=""
   if [ -f "$SGE_ROOT/util/sgeST/st_settings.sh" ]; then
      . $SGE_ROOT/util/sgeST/st_settings.sh
   else
      $INFOTEXT "Could not source basic settings"
      exit 1
   fi
}

#---------------------------------------------------------------------------
# Test the instance_urn is valid
# urn should start with urn:st
# USES: parameter $1 "instance_urn" 
# Return "true" /  "false"  to strdout 
#
STvalidateInstanceUrn()
{  
   ret="false"
   if [ -n "$1" ]; then
      instance_urn_valid=`echo "$1" | $CUT -f1-2 -d:`
      if [ "$instance_urn_valid" = "urn:st" ]; then
         ret="true"
      fi
   fi
   echo $ret
}

#---------------------------------------------------------------------------
# The Service Tags Enabled Test
# Return true if the st.enabled file is present
#
# Return "true", the ST are enabled, "false" ST are not enabled
STShowEnabled()
{
   st_file=$SGE_ROOT/$SGE_CELL/common/st.enabled
   if [ -f "$st_file" ]; then
      echo "true"
   else
      echo "false"
   fi
}

#---------------------------------------------------------------------------
# Enable the service tegs
STEnable()
{
   st_file=$SGE_ROOT/$SGE_CELL/common/st.enabled
   if [ ! -f "$st_file" ]; then
      CreateAndMakeWriteable $st_file
      echo $PRODUCT_URN >> $st_file
      result=`STShowEnabled`
      if [ $result = "true" ]; then
         $INFOTEXT "Sun Service Tags are now enabled"
      else
         $INFOTEXT "Sun Service Tags could not be enabled"
         return 1
      fi
   fi   
   return 0
}

#---------------------------------------------------------------------------
# Disable The Service Tags
#
STDisable()
{
   st_file=$SGE_ROOT/$SGE_CELL/common/st.enabled
   if [ -f "$st_file" ]; then
      ExecuteAsAdmin $RM $st_file
      result=`STShowEnabled`
      if [ $result = "false" ]; then
         $INFOTEXT "Sun Service Tags are now disabled"
      else 
         $INFOTEXT "Sun Service Tags could not be disabled"
         return 1
      fi
   fi      
   return 0
}

#---------------------------------------------------------------------------
# Get the product instance_URN from ST registry 
# using the find all instances of the product function
#
# PARAMS: $stclient 
#         $product_defined_id
# return $instance_urn to stdout
STgetInstanceUrn()
{
   stclient=$1
   product_defined_id=$2
   instance_urn=""
   #find first product instance with the same product_defined_inst_id
   urn_list=`$stclient -f -t "$PRODUCT_URN"`
   for registered_urn in $urn_list ;do
      valid=`STvalidateInstanceUrn $registered_urn`
      if [ $valid = "true" ]; then
         registered_inst_id=`$stclient -g -i $registered_urn | grep product_defined_inst_id | $CUT -f2 -d=`
          if [ "$registered_inst_id" = "$product_defined_id" ]; then
            instance_urn=$registered_urn
            break
         fi
      fi;
   done

   echo $instance_urn
}

#---------------------------------------------------------------------------
# Test the stclient is valid and installed
#
# return stclient or "undefined" if not stclient
#
STgetServiceTagClient()
{
   if [ -z "$stclient" ]; then
      # library path setting required only for architectures where RUNPATH is not supported
      case $ARCH in
      lx*)
         stclient=`rpm -ql sun-servicetag 2>/dev/null | grep bin/stclient`
         ;;
      sol*)
         # a strange error message comes from pkgchk -l, need to be filtered out
         if [ -x /usr/bin/stclient ]; then
            stclient=/usr/bin/stclient
         fi   
         ;;
      esac
   fi
   if [ -z "$stclient" ]; then
      stclient="undefined"
   fi
   echo $stclient
}

#---------------------------------------------------------------------------
# Sub function create st client
# return registered or not to stdout
STClient()
{
   #get the stclinet with full path
   stclient=`STgetServiceTagClient`
   if [ "$stclient" = "undefined" ]; then
      $INFOTEXT "The service tags are not supported"    
      return 1
   fi 
   #the executeble file must exist
   if [ ! -x "$stclient" ]; then
      $INFOTEXT "The service tags are not installed correctly" 
      return 1
   fi

   return 0
}


#---------------------------------------------------------------------------
# The Service Tags Packages are installed on the node
#
# Return "true", the ST are supported, "false" ST are not  supported
STShowSupported()
{
   #get the stclinet with full path
   stclient=`STgetServiceTagClient`
   if [ "$stclient" = "undefined" ]; then
      echo "false"
   else
      #the executable file must exist
      if [ -x "$stclient" ]; then
         echo "true" 
      else
         echo "false"
      fi
   fi
}

#---------------------------------------------------------------------------
# Get the product instance_URN from ST registry 
# using the find all instances of the product function
#
# PARAMS: $product_defined_id
# return registered or unregistered
STShowRegistered()
{
   product_defined_id=$1

   STClient
   if [ $? = 0 ]; then
      #check again, to be sure wos done
      instance_urn=`STgetInstanceUrn "$stclient" "$product_defined_id"`
      if [ -n "$instance_urn" ]; then
         $INFOTEXT "registered:%s" "$instance_urn"
      else
         $INFOTEXT "unregistered"
      fi
   fi   
}

#---------------------------------------------------------------------------
# Add a new instance of product to a ST registry 
# using the platform CLI command
# Add only if $instance_urn is empty
#
# ARGS: $stclient CLI tool
#       $instance_urn 
#       $product_defined_id
#   
STaddServiceTag()
{
   stclient=$1
   instance_urn=$2
   product_defined_id=$3
   if [ -z "$instance_urn" ]; then
      platform_name=`uname -p`

      case $ARCH in
      lx*)
         zone=`uname`
         ;;
      sol*)
         zone="global"

         if [ -x /usr/bin/zonename ]; then
            zone=`/usr/bin/zonename`
         fi
         ;;
      esac
      
      ExecuteAsAdmin ${stclient} -a \
      -p "${PRODUCT_NAME}" \
      -e "${PRODUCT_VERSION}" \
      -t "${PRODUCT_URN}" \
      -F "${PRODUCT_PARENT_URN}" \
      -P "${PRODUCT_PARENT_NAME}" \
      -I "${product_defined_id}" \
      -m "${PRODUCT_VENDOR}" \
      -A "${platform_name}" \
      -z "${zone}" \
      -S "${PRODUCT_SOURCE}" > /dev/null 2>&1
   fi
}

#---------------------------------------------------------------------------
# Remove instance of product to a ST registry 
# using the platform CLI command
# Remove only if $instance_urn is not empty
#
# USES: $stclient CLI tool
#       $instance_urn 
# 
STremoveServiceTag()
{
   stclient=$1
   instance_urn=$2
   if [ -n "$instance_urn" ]; then
      ExecuteAsAdmin $stclient -d -i "$instance_urn"  > /dev/null  2>&1
   fi
}

#---------------------------------------------------------------------------
# show the usage of the standalone command
# PARAMS: $product_defined_id
# return registered or not to stdout
STRegister()
{   
   product_defined_id=$1

   #get the stclinet with full path
   STClient
   if [ $? = 0 ]; then
      STEnable
      if [ $? = 0 ]; then
         # get instance_urn
         instance_urn=`STgetInstanceUrn "$stclient" "$product_defined_id"`
         STaddServiceTag "$stclient" "$instance_urn" "$product_defined_id"
         STShowRegistered "$product_defined_id"
      fi   
   fi   
}

#---------------------------------------------------------------------------
# show the usage of the standalone command
# PARAMS: $product_defined_id
# return registered or not to stdout
STUnregister()
{
   product_defined_id=$1

   STClient
   if [ $? = 0 ]; then
      # get instance_urn
      instance_urn=`STgetInstanceUrn "$stclient" "$product_defined_id"`
      STremoveServiceTag "$stclient" "$instance_urn"
      STShowRegistered "$product_defined_id"
   fi   
}

#---------------------------------------------------------------------------
# Main routine
# PARAMS: $command 
#         check ... called from install script
# return a customer stdout output
STServiceTag()
{   
   command="$1"
   if [ "$#" -lt 1 -o  "$#" -gt 2 -o "$command" = "-h" -o "$command" = "help" ]; then
      STusage
      return 0
   fi

   #Architecture related variables
   STArchVariables
   #ExecuteAsAdmin
   SetAdminUser
   #load static data from file
   STBasicSettings
   product_defined_id=$SGE_ROOT/$SGE_CELL

   if [ "$command" = "enable" ]; then
      STEnable 
   elif [ "$command" = "disable" ]; then
      STDisable 
   elif [ "$command" = "register" ]; then
      STRegister "$product_defined_id"
   elif [ "$command" = "unregister" ]; then
      STUnregister "$product_defined_id"
   elif [ "$command" = "enabled" ]; then
      STShowEnabled
   elif [ "$command" = "supported" ]; then
      STShowSupported
   elif [ "$command" = "check" ]; then
      STShowRegistered "$product_defined_id"
   else
      STusage
      return 1
   fi
   return 0
}

STServiceTag $1
