#!/bin/sh
##########################################################################
#    This program is the CONFIDENTIAL and PROPRIETARY property
#    of Mainsoft(R) Corporation. Any unauthorized use, reproduction 
#    or transfer of this computer program is strictly prohibited.
#
#    Copyright (c) 1991-2001 Mainsoft Corporation.
#    This is an unpublished work, and is subject to limited distribution
#    and restricted disclosure only. ALL RIGHTS RESERVED.
#
##########################################################################
#
# mwversion program prints out MainWin version information
#

PROG=`basename $0`
versionfile="$MWHOME/../userx/public/mainwin/inc/mwversion.h"
updatefile="$MWHOME/../userx/public/mainwin/inc/mwupdate.h"
inifile="$MWHOME/../mwver/mwversion.ini"
patchfile=$MWHOME/../mwver/mwpatch.txt
stingray=${MWSTINGRAY_HOME:-"$MWHOME/../stingray"}

Usage()
{
 echo "
$PROG prints out MainWin version information
Usage: $PROG [-c] [-h] [-l] [-o] [-p] [-s] [-v]
   -h   - prints this help information
   -c   - prints full  version information extracted from MainWin libraries
   -l   - prints brief version information extracted from MainWin libraries
   -o   - prints version information related to Stingray
   -p   - prints MainWin patches information
   -s   - prints shorter version information
   -v   - prints more extended version information
   no arguments - prints basic version information only
"
  exit 1
}

### Print MainWin version info
PrintVersion()
{
  if [ -z "$short_version" ] ;then
    mwversion="Visual MainWin version "
  else
    mwversion=""
  fi
  mwversion="${mwversion}$MW_VERSION_MAJOR.$MW_VERSION_MINOR"
  if [ "$MW_VERSION_UPDATE"  -a  "$MW_VERSION_UPDATE" -ne 0 ] ;then
    mwversion="$mwversion.$MW_VERSION_UPDATE"
  fi
  if [ ! -z "$MW_VERSION_MISC"  -a  "$MW_VERSION_MISC" != 0 ] ;then
    mwversion="$mwversion ($MW_VERSION_MISC)"
  fi
  if [ ! -z "$MW_VERSION_SP"  -a  "$MW_VERSION_SP" != 0 ] ;then
    mwversion="$mwversion SP$MW_VERSION_SP"
  fi
  if [ ! -z "$MW_VERSION_HF"  -a  "$MW_VERSION_HF" != 0 ] ;then
    mwversion="$mwversion HF$MW_VERSION_HF"
  fi
  if [ "$extended_mode" ] ;then
    if [ ! -z "$PS_CYCLE_ID"  -a  "$PS_CYCLE_ID" != 0 ] ;then
      mwversion="$mwversion Cycle$PS_CYCLE_ID"
      if [ ! -z "$PS_PACK_ID"  -a  "$PS_PACK_ID" != 0 ] ;then
        mwversion="${mwversion}$PS_PACK_ID"
      fi
    fi
  fi
  echo "$mwversion"
}

### extract macro value from include file (format: #define MW_VERSION_MAJOR 4)
FromInc()
{
  ## Usage: FromInc mwversion.h MW_VERSION_MAJOR 
  if [ "$2" = "MW_VERSION_MISC" ] ;then
    eval $2=\"`awk '/#define '$2'/ {$1=$2=""; print}' $1 | sed -e 's:"::g' -e 's:^[ ]*::' -e 's:[ ]*$::'`\"
  else
    eval $2=\"`awk '/#define '$2'/ {print $3}' $1`\"
  fi
  if [ "$DebugMwversion" ] ;then
    echo Result-of-FromInc: $2 = \<`eval echo \$"$2"`\>
  fi
}

### extract macro value from ini-file (format: MW_VERSION_MAJOR=4)
FromIni()
{
  ## Usage: FromIni mwversion.ini MW_VERSION_MAJOR 
  if [ "$2" = "MW_VERSION_MISC" ] ;then
    eval $2=\"`awk -F= '/^'$2='/ {$1=""; print}' $1 | sed -e 's:^[ ]*::' -e 's:[ ]*$::'`\"
  else
    eval $2=\"`awk -F= '/^'$2='/ {$1=""; print}' $1 | sed 's: ::g'`\"
  fi
  if [ "$DebugMwversion" ] ;then
    echo Result-of-FromIni: $2 = \<`eval echo \$"$2"`\>
  fi
}

show_lib_versions()
{
  if [ "$extended_mode" ] ;then
    libs="lib*.so lib*.sl"
  else
    libs="libkernel32*.so libkernel32*.sl"
  fi
  for i in $libs
  do
    if [ -f "$i" ]; then
      echo "$i"
      ${MWHOME}/lib-${MWCONFIG_NAME}/mwgetcs $1 "$i" | sed 's: ()::'
      echo "" 
    fi
  done
  echo ""
}

#
# Main script starts from here
#

# Check for MWHOME variable and its validity
if [ -z "$MWHOME" ] ;then
  echo "$PROG: Error: MWHOME environment variable is not defined"
  exit 1
fi

if [ ! -d $MWHOME ] ;then
  echo "$PROG: Error: MWHOME environment variable does not point to a valid directory"
  echo "          MWHOME=$MWHOME"
  exit 1
fi

set -- `getopt chlopsv $*`
if [ $? -ne 0 ] ;then
  Usage
fi

print_lib=
print_lib_full=
print_stingray=
print_sp=
short_version=
extended_mode=

for i in $*
do
  case $i in
    -h)
      Usage
      ;;
    -c)
      print_lib=1
      print_lib_full="-c"
      ;;
    -l)
      if [ ! -z "$print_lib_full" ] ;then
        echo "$PROG: Error: -c and -l options are mutually exclusive"
        Usage
      fi
      print_lib=1
      ;;
    -o)
      print_stingray=1
      ;;
    -p)
      print_sp=1
      ;;
    -s)
      if [ "$extended_mode" ] ;then
        echo "$PROG: Error: -s and -v options are mutually exclusive"
        Usage
      fi
      short_version=1
      ;;
    -v)
      if [ "$short_version" ] ;then
        echo "$PROG: Error: -s and -v options are mutually exclusive"
        Usage
      fi
      extended_mode=1
      ;;
    --)
      shift
      break
      ;;
  esac
done


### Extract info from data files and print out

if [ -s "$versionfile" ] ;then
  FromInc "$versionfile" MW_VERSION_MAJOR
  FromInc "$versionfile" MW_VERSION_MINOR
fi

if [ -s "$updatefile"  -a  -s "$inifile" ] ;then
  echo "$PROG: Warning: detected both mwupdate.h and mwversion.ini"
  echo "   Normally, MainWin user site contains only mwversion.ini"
  echo "   Normally, Mainsoft company site contains only mwupdate.h"
  echo "   The following two lines will contain version strings obtained from"
  echo "   mwupdate.h (the first line) and from mwversion.ini (the second line)"
  echo ""
fi

if [ -s "$updatefile" ] ;then
  if [ ! -s "$versionfile" ] ;then
    echo "$PROG: Warning: detected mwupdate.h but mwversion.h is missing!"
  fi
  FromInc "$updatefile"  MW_VERSION_UPDATE
  FromInc "$updatefile"  MW_VERSION_MISC
  FromInc "$updatefile"  MW_VERSION_SP
  FromInc "$updatefile"  MW_VERSION_HF
  FromInc "$updatefile"  PS_ISSUE_ID
  PrintVersion
fi

if [ -s "$inifile" ] ;then
  #MW_VERSION_MAJORv="$MW_VERSION_MAJOR"
  #MW_VERSION_MINORv="$MW_VERSION_MINOR"
  # here add comparison against values extracted earlier from mwversion.h
  FromIni "$inifile" MW_VERSION_MAJOR
  FromIni "$inifile" MW_VERSION_MINOR
  FromIni "$inifile" MW_VERSION_UPDATE
  FromIni "$inifile" MW_VERSION_MISC
  FromIni "$inifile" MW_VERSION_SP
  FromIni "$inifile" MW_VERSION_HF
  FromIni "$inifile" PS_ISSUE_ID
  FromIni "$inifile" PS_TYPE_ID
  FromIni "$inifile" PS_CYCLE_ID
  FromIni "$inifile" PS_PACK_ID
  PrintVersion
fi

# calculate platform name MWCONFIG_NAME
if [ -z "$MWCONFIG_NAME" ] ;then
  case "`uname`" in
    SunOS) MWCONFIG_NAME=sunos5;;
    HP-UX) MWCONFIG_NAME=ux11
           case "`uname -m`" in 
	   ia64) MWCONFIG_NAME=ia64_ux11;;
	   esac
	  ;;
    AIX)   MWCONFIG_NAME=aix4;;
    Linux) MWCONFIG_NAME=linux;;
    IRIX*) MWCONFIG_NAME=irix6;;
    OSF1)  MWCONFIG_NAME=osf1v4;;
    *)     break;;
  esac
fi

### Print Stingray related version information (one line per component)
if [ "$print_stingray" ] ;then
  MW_VERSION_MISC=
  for component in foundation chart edit grid toolkit views
  do
    file="$stingray/include/$component/mwversion.h"
    if [ -r "$file" ] ;then
      ## probably, binary and source versions should be compared?
      case "$component" in
        foundation) suf=sfl;;
        chart)      suf=oc;;
        edit)       suf=oe;;
        grid)       suf=og;;
        toolkit)    suf=otp;;
        views)      suf=ov;;
      esac
      ## check existence of the component library (e.g., liboc603as.so)
      lib="$stingray/lib-${MWCONFIG_NAME}_optimized/lib${suf}[1-9]*as.s?"
      if [ ! -r $lib ] ;then
        continue
      fi
      FromInc "$file" MW_VERSION_MISC
      if [ "$short_version" ] ;then
        echo "$MW_VERSION_MISC" | sed "s:(.*::"
      else
        echo "$MW_VERSION_MISC  $MWCONFIG_NAME"
      fi
    fi
  done
  if [ -z "$MW_VERSION_MISC" ] ;then
    echo "No Stingray products installed"
  fi
fi

### Print patches information
if [  "$print_sp" ] ;then
  if [ ! -s "$patchfile" ] ;then
    echo "No patches have been installed"
  else
    i=`egrep -c "^[^#]" "$patchfile"`
    if [ "$i" -eq 0 ] ;then
      echo "No patches have been installed"
    else
      egrep "<$MWCONFIG_NAME>" "$patchfile" |
       awk '!/^#/ {print $1}'  | sort -n | uniq | 
       xargs echo "The following patches installed for $MWCONFIG_NAME:" |
       fold -s -w 78 | sed -e 's/ for / /2' | sed -e 's/ for$/ /'
      if [ "$extended_mode" ] ;then
        echo "Details of the installed patches:"
        sed -n "s:^\([^#]\):  \1:p" "$patchfile"
      fi
    fi
  fi
fi

### Print info extracted from libraries

if [ -z "$print_lib" ] ;then
  exit
fi

MWCONFIG_NAME=`mwgetconfigname OUTPUT_FORMAT=SUFFIX`

echo ""
if [ -d ${MWHOME}/lib-${MWCONFIG_NAME}_debug ]; then
  cd ${MWHOME}/lib-${MWCONFIG_NAME}_debug
  echo "Debug libraries"
  echo "==============="
  show_lib_versions "$print_lib_full"
else
  echo "Debug libraries do not exist!"
  echo ""
fi

if [ -d ${MWHOME}/lib-${MWCONFIG_NAME}_optimized ]; then
  cd ${MWHOME}/lib-${MWCONFIG_NAME}_optimized
  echo "Optimized libraries"
  echo "==================="
  show_lib_versions "$print_lib_full"
else
  echo "Optimized libraries do not exist!"
  echo ""
fi

### the last line
