#!/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-1998 Mainsoft Corporation.
#    This is an unpublished work, and is subject to limited distribution
#    and restricted disclosure only. ALL RIGHTS RESERVED.
#
##########################################################################

#
# Program to list MainWin library version information
#

PROG=`basename $0`

print_help()
{
    echo "$PROG prints version information of the MainWin libraries."
    echo "Usage: $PROG [-c] [-h] [-v] [-p]"
    echo "       -c     Prints the configuration information of the MainWin Libraries"
    echo "       -h     Prints this help information"
    echo "       -p     Prints only the MainWin patch information"
    echo "       -v     Prints verbose output"
    echo ""
}

show_lib_versions()
{
    for i in lib*.so* lib*.sl*; do
	if [ -f $i ]; then
	    echo $i
	    strings $string_opt $i | grep "MW_" | sed 's/^.*MW_/MW_/' | sort -r | ${MWHOME}/lib-${MWCONFIG_NAME}/mwgetcs $1
	    echo "" 
	fi
    done
    echo ""
}

show_mw_version()
{
    for i in libmw32*.so* libmw32*.sl*; do
	if [ -f $i ]; then
	    echo $i
	    strings $string_opt $i | grep "MW_" | sed 's/^.*MW_/MW_/' | sort -r | ${MWHOME}/lib-${MWCONFIG_NAME}/mwgetcs $1
	    echo ""
	fi
    done
    echo ""
}

show_patch_info()
{
    PATCH_LIST_FILE=$MWHOME/patches.lst
    if [ -s ${PATCH_LIST_FILE} ]
    then
		echo "Following patches were installed:"
		echo "================================"
		cat $PATCH_LIST_FILE | cut -f1 -d':' | uniq

		if [ -n "$1" ]; then
			echo ""
			echo "Details of the patches installed:"
			echo "================================"
			cat $PATCH_LIST_FILE
		fi
    elif [ "$PRINT_PATCHINFO" = 1 ]
    then
		echo "No patches were applied to this MainWin installation."
    fi
}   

#
# Main script starts from here
#

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

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

set -- `getopt cpvh $*`
if [ $? -ne 0 ]
then
    print_help
    exit 1
fi

for i in $*
do
    case $i in
	-c)
	    ECHO_CONFIG=-c
	;;
	-p)
        if [ "$ECHO_CONFIG" = "-c" -o "$VERBOSE_MODE" = "1" ] ; then
			/bin/echo "\n`basename $0`: the -c and -v options are mutually exclusive to the -p option\n"
			print_help
            exit
		fi
	    PRINT_PATCHINFO=1
	;;
	-v)
	    VERBOSE_MODE=1
	;;
	-h)
	    print_help
	    exit 1
	;;
	--)
	    shift
	    break
	;;
    esac
done

MWCONFIG_NAME=`mwgetconfigname OUTPUT_FORMAT=SUFFIX`
OLDDIR=`pwd`
if [ $MWCONFIG_NAME = aix4 ] ; then
	string_opt="-"
fi

if [  "$PRINT_PATCHINFO" = 1 ]
then
    show_patch_info
    exit 0
fi

echo ""
if [ -d ${MWHOME}/lib-${MWCONFIG_NAME}_debug ]; then
  cd ${MWHOME}/lib-${MWCONFIG_NAME}_debug
  echo "Debug libraries"
  echo "==============="
  if [ -z "$VERBOSE_MODE" ]
    then
      show_mw_version "$ECHO_CONFIG"
    else
      show_lib_versions "$ECHO_CONFIG"
  fi
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 "==================="
  if [ -z "$VERBOSE_MODE" ]
  then
    show_mw_version "$ECHO_CONFIG"
  else
    show_lib_versions "$ECHO_CONFIG"
  fi
else
  echo "Optimized libraries do not exist!"
  echo ""
fi


cd $OLDDIR

show_patch_info 
