#! /bin/sh
# set -x

####################################################################
# Post Installation Script for PEX 2.1     v2.0  (05/13/93)
####################################################################

OW_CONFIG_DIR=$BASEDIR/openwin/server/etc
OW_CONFIG=$OW_CONFIG_DIR/OWconfig
TMP_OW_CONFIG=/tmp/OWconfig
SO_OBJ_NAME="SUNWX3D-PEX.so.2"

if [ -f $OW_CONFIG ]; then
   if [ -d $OW_CONFIG_DIR -a -w $OW_CONFIG_DIR -a -w  $OW_CONFIG ]; then
##########################################################
#   Get the  PEX version specified in the OW_CONFIG
##########################################################
      SUNPEX_VERSION=`awk -F= '$1 ~ /sharedObject/ && $2 ~ /SUNWX3D-*PEX/ {print substr($2, 2, length($2)-2)}' $OW_CONFIG`

#########################################################
#  If SUNWX3D-PEX extension does not exist, then add the entries
#########################################################
      if  [ "$SUNPEX_VERSION" = "" ]; then 
         echo "
# SUNWX3D-PEX extension
class=\"XEXTENSION\" name=\"X3D-PEX\"
       sharedObject=\"SUNWX3D-PEX.so.2\"
       initFunc=\"PexExtensionInit\"
       preLoad=\"NO\";"  >> $OW_CONFIG

########################################################
#  If there exists another PEX entries in OWconfig already, eg,
#  from another PEX installation process, then delete the old
#  entries and add the current PEX entries format.
#########################################################
      else
        if [ $SUNPEX_VERSION != $SO_OBJ_NAME ]; then
          awk '
                BEGIN {
                        i = 0
                        j = 0
                        lines = 0
                }
                {
                        i = index($0, "#")
                        j = index($0, "SUNWX3D-PEX")
                        if (i != 0 && j != 0 && i < j) {
                            lines = 5
                        }
                        if (lines > 0) {
                            lines--
                        }
                        else
                            print $0
                }' < $OW_CONFIG > $TMP_OW_CONFIG

           echo "
# SUNWX3D-PEX extension
class=\"XEXTENSION\" name=\"X3D-PEX\"
       sharedObject=\"SUNWX3D-PEX.so.2\"
       initFunc=\"PexExtensionInit\"
       preLoad=\"NO\";" >> $TMP_OW_CONFIG
            cp $TMP_OW_CONFIG $OW_CONFIG
            rm $TMP_OW_CONFIG
         fi 
      fi
   else
      echo "$OW_CONFIG is not writable"
      echo  
      echo " Terminating PEX 2.1 Post Installation"

      exit 1
   fi
else
   echo
   echo
   echo "$OW_CONFIG  does not exists..."
   echo
   echo " Terminating PEX 2.1 Post Installation"
   echo
   exit 1
fi
	
installf -f $PKGINST 
