#!/bin/bash

#set -x

osname=`uname`

##  Arguments:
##  stop  - stops cimom and kills processes
##  start - starts HSC by calling startup scripts
##  start debug - set debug log flag, start rmiregistry, switch sniacimom.jar.

function checkVersion {
   typeset -i pv1
   typeset -i pv2
   typeset -i v1
   typeset -i v2
   pv1=0
   pv2=0
   pr=0
   if [ "$osname" != "AIX" ]
   then
      r=`rpm -q -i IBMhsc.coreserver|grep Release| tail -1|cut -d ':' -f 2 | cut -d ' ' -f 2`
      v1=`rpm -q -i IBMhsc.coreserver|grep Version| tail -1|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1`
      v2=`rpm -q -i IBMhsc.coreserver|grep Version| tail -1|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 2`
      if [[ $pv1 -eq 0  ||  $v1 -gt $pv1 ]]
      then
           pv1=v1
      fi
      if [[ $pv2 -eq 0  ||  $v2 -gt $pv2 ]]
      then
           pv2=v2
      fi 
   else
      r=`lslpp -hc hmc.base | grep -v '^#' | cut -d ':' -f 3`
      pv1=0
      pv2=0
      if [ -n "$r" ]
      then
         val1=`echo $r | cut -d '.' -f 1`
         if [ -n "$val1" ]
         then
            val2=`expr "$r" : "$val1\.\(.*\)"`
            if [ -n "$val1" -a -n "$val2" ]
            then
               pv1=$val1
               r=$val2
            fi
         fi
      fi
   fi
   if [ "$1" = "curr_version" ]; then
#      dr=`echo $r | /usr/bin/tr -d '.'`
# Keep dot in the string
      echo -n ""V"$pv1"R"$r"."$pv2"
   else
      echo " Version: "$pv1
      echo " Release: "$r
      echo " Service Pack: "$pv2
   fi
}

if [ "$2" = "" ]
then
  FLAG=false
else
  FLAG=$2
fi
case $1 in
version )
  checkVersion
  cat /opt/hsc/data/version
;;
base_version )
  if [ -f /etc/HmcBase ]; then
     bv=`cat /etc/HmcBase | cut -d':' -f2`
     if [ "$bv" != "" ]; then
        echo "$bv"
     else
        echo "n/a"
     fi
  else
     echo "n/a"
  fi
  ;;
all_version )
  checkVersion
  cat /opt/hsc/data/version
  if [ -f /etc/HmcBase ]; then
     bv=`cat /etc/HmcBase | cut -d':' -f2`
     if [ "$bv" = "" ]; then
        bv="n/a"
     fi
  else
     bv="n/a"
  fi
  echo "base_version: $bv"
;;
curr_version )
  checkVersion curr_version
  ;;
"" )
 echo        hsc version - Show HMC Version
;;

esac
