#!/bin/sh
                                                                                   
#  This script works by
#   1. If pkgcond exec exists it uses it to determine whether to
#	   make any file system modifications
#   2. if pkgcond does not exist it works by using the older FCS logic in CheckZones()
#	   The LocalZones() function now calls the function that modified /usr
#	   based on whether /usr/java/jre/lib is writable

#  Basically local zones can have any arbitrary directory readonly or writable
#  So a script that needs to modify say /usr/java needs
#  to figure out if this is actually doable first.


PKGCOND=/usr/bin/pkgcond
CheckZones()
{
	if [ "${PKG_INSTALL_ROOT:=/}" = "/" -a -x /usr/bin/zonename ]; then
		ZONENAME=`/usr/bin/zonename`
		if [ ${ZONENAME} = "global" ]; then
			GLOBAL_ZONE=true
		else
                    	GLOBAL_ZONE=false
		fi
	else
            	# Unable to determine zone
		GLOBAL_ZONE=true
	fi
}

#  I am testing if /usr/java/jre/lib/ is writable as this is the only dir touched by this postinstall script
#  If there are other dirs that are touched later then need to add them here as well
LocalZones () {
    /usr/bin/touch  ${BASEDIR}/java/jre/lib/.test.$$ > /dev/null 2>&1
    if [ $? !=	0 ]; then
	   return 0
    fi
    rm -f  ${BASEDIR}/java/jre/lib/.test.$$ > /dev/null 2>&1
    ExecuteDefaultCmds
    return $?

}


ExecuteDefaultCmds() {


  if [ -h ${BASEDIR}/java/jre/lib/accessibility.properties ];
then
	rm -f ${BASEDIR}/java/jre/lib/accessibility.properties
  fi
  # Remove the incorrect /usr/java entries from the packaging database.
  # Ignore errors for situation where files no longer in db as removing a
  # non-existant reference does not cause any harm.
  removef $PKGINST /usr/java/jre/lib/accessibility.properties >/dev/null 2>&1
  removef $PKGINST /usr/java/jre/lib/ext/gnome-java-bridge.jar >/dev/null 2>&1
  removef -f $PKGINST >/dev/null 2>&1

  # Determine the full path (symlinks resolved) to JDK pointed to by /usr/java.
  cd $BASEDIR
  JAVADIR=java
  JDK_DIR=$BASEDIR
  # If $JAVADIR is a symlink then we must resolve it.
  if [ -d $JAVADIR -a -h $JAVADIR ]
  then
    # Get symlink target.
    DIR=`ls -l $JAVADIR | awk '{print $NF}'`

    # If the target is a symlink then need to resolve it.
    if [ -d $DIR -a -h $DIR ]
    then
      nextdir=`dirname $DIR`
      cd $nextdir
      JDK_DIR="$JDK_DIR/$nextdir"  # Append this bit.

      f=`basename $DIR`
      nextdir=`ls -l $f | awk '{print $NF}'`
      JDK_DIR="$JDK_DIR/$nextdir"
    else
      # otherwise just append to $JDK_DIR and we're done.
      JDK_DIR="$JDK_DIR/$DIR"
    fi
  else
    # If the 'java' item is not a symlink (presumably a dir) there will be no
    # symlink to installf items to.
    return 0
  fi

  cd ${JDK_DIR}/jre/lib || exit 2
  # Dynamically determine number of '..' required.
  JAR_DIR=share/jar
  DOTS=.
  LEVEL=0
  FILE=accessibility.properties
  # Keep testing, up to a max of 10 levels to avoid infinite loop.
  while [ ! -f $DOTS/$JAR_DIR/$FILE ] && [ $LEVEL -lt 10 ]
  do
    DOTS="$DOTS/.."
    LEVEL=`expr $LEVEL + 1`
  done
  if [ -f $DOTS/$JAR_DIR/$FILE ]; then
    ln -s $DOTS/$JAR_DIR/$FILE . || exit 2
    installf $PKGINST  ${JDK_DIR}/jre/lib/$FILE=$DOTS/$JAR_DIR/$FILE s || exit 2
  fi
  FILE=gnome-java-bridge.jar
  if [ -f $DOTS/$JAR_DIR/$FILE ]; then
    cp $DOTS/$JAR_DIR/$FILE ext || exit 2
    installf $PKGINST  ${JDK_DIR}/jre/lib/ext/$FILE f 0644 root bin || exit 2
  fi

  installf -f $PKGINST || exit 2

  return 0

}

ExecuteWholerootCmds() {


  if [ -h ${BASEDIR}/java/jre/lib/accessibility.properties ];
then
	rm -f ${BASEDIR}/java/jre/lib/accessibility.properties
  fi
  # Remove the incorrect /usr/java entries from the packaging database.
  # Ignore errors for situation where files no longer in db as removing a
  # non-existant reference does not cause any harm.
  removef $PKGINST /usr/java/jre/lib/accessibility.properties >/dev/null 2>&1
  removef $PKGINST /usr/java/jre/lib/ext/gnome-java-bridge.jar >/dev/null 2>&1
  removef -f $PKGINST >/dev/null 2>&1

  # Determine the full path (symlinks resolved) to JDK pointed to by /usr/java.
  cd $BASEDIR
  JAVADIR=java
  JDK_DIR=$BASEDIR
  # If $JAVADIR is a symlink then we must resolve it.
  if [ -d $JAVADIR -a -h $JAVADIR ]
  then
    # Get symlink target.
    DIR=`ls -l $JAVADIR | awk '{print $NF}'`

    # If the target is a symlink then need to resolve it.
    if [ -d $DIR -a -h $DIR ]
    then
      nextdir=`dirname $DIR`
      cd $nextdir
      JDK_DIR="$JDK_DIR/$nextdir"  # Append this bit.

      f=`basename $DIR`
      nextdir=`ls -l $f | awk '{print $NF}'`
      JDK_DIR="$JDK_DIR/$nextdir"
    else
      # otherwise just append to $JDK_DIR and we're done.
      JDK_DIR="$JDK_DIR/$DIR"
    fi
  else
    # If the 'java' item is not a symlink (presumably a dir) there will be no
    # symlink to installf items to.
    return 0
  fi

  cd ${JDK_DIR}/jre/lib || exit 2
  # Extra '..' because traversing up through symlink.
  #
  # Due to bugid 6358671, need to check whether we are running pkgadd relative to /export/Solaris_1*
  # and the link ${PKG_INSTALL_ROOT}/usr_${ARCH}.all to see whether this is smosservice.
  #  Also not do installf since diskless client root and user packages are not in installed relative 
  #  to the same directory.  installf/removef will never work properly
  #
  if [ -h ${PKG_INSTALL_ROOT}/usr_${ARCH}.all ]; then
        # Dynamically determine number of '..' required.
        JAR_DIR=share/jar
        DOTS=.
        LEVEL=0
        FILE=accessibility.properties
        # Keep testing, up to a max of 10 levels to avoid infinite loop.
        while [ ! -f $DOTS/$JAR_DIR/$FILE ] && [ $LEVEL -lt 10 ]
        do
          DOTS="$DOTS/.."
          LEVEL=`expr $LEVEL + 1`
        done
        if [ -f $DOTS/$JAR_DIR/$FILE ]; then
          ln -s $DOTS/$JAR_DIR/$FILE . || exit 2
        fi
        FILE=gnome-java-bridge.jar
        if [ -f $DOTS/$JAR_DIR/$FILE ]; then
          cp $DOTS/$JAR_DIR/$FILE ext || exit 2
        fi
  else
        # Dynamically determine number of '..' required.
        JAR_DIR=share/jar
        DOTS=.
        LEVEL=0
        FILE=accessibility.properties
        # Keep testing, up to a max of 10 levels to avoid infinite loop.
        while [ ! -f $DOTS/$JAR_DIR/$FILE ] && [ $LEVEL -lt 10 ]
        do
          DOTS="$DOTS/.."
          LEVEL=`expr $LEVEL + 1`
        done
        if [ -f $DOTS/$JAR_DIR/$FILE ]; then
          ln -s $DOTS/$JAR_DIR/$FILE . || exit 2
  	  installf $PKGINST  ${JDK_DIR}/jre/lib/$FILE=$DOTS/$JAR_DIR/$FILE s || exit 2
        fi
        FILE=gnome-java-bridge.jar
        if [ -f $DOTS/$JAR_DIR/$FILE ]; then
          cp $DOTS/$JAR_DIR/$FILE ext || exit 2
  	  installf $PKGINST  ${JDK_DIR}/jre/lib/ext/$FILE f 0644 root bin || exit 2
        fi

  	installf -f $PKGINST || exit 2
  fi
  	return 0

}

ExecuteDisklessCmds() {


  if [ -h ${BASEDIR}/java/jre/lib/accessibility.properties ];
then
	rm -f ${BASEDIR}/java/jre/lib/accessibility.properties
  fi
  # Remove the incorrect /usr/java entries from the packaging database.
  # Ignore errors for situation where files no longer in db as removing a
  # non-existant reference does not cause any harm.
  removef $PKGINST /usr/java/jre/lib/accessibility.properties >/dev/null 2>&1
  removef $PKGINST /usr/java/jre/lib/ext/gnome-java-bridge.jar >/dev/null 2>&1
  removef -f $PKGINST >/dev/null 2>&1

  # Determine the full path (symlinks resolved) to JDK pointed to by /usr/java.
  cd $BASEDIR
  JAVADIR=java
  JDK_DIR=$BASEDIR
  # If $JAVADIR is a symlink then we must resolve it.
  if [ -d $JAVADIR -a -h $JAVADIR ]
  then
    # Get symlink target.
    DIR=`ls -l $JAVADIR | awk '{print $NF}'`

    # If the target is a symlink then need to resolve it.
    if [ -d $DIR -a -h $DIR ]
    then
      nextdir=`dirname $DIR`
      cd $nextdir
      JDK_DIR="$JDK_DIR/$nextdir"  # Append this bit.

      f=`basename $DIR`
      nextdir=`ls -l $f | awk '{print $NF}'`
      JDK_DIR="$JDK_DIR/$nextdir"
    else
      # otherwise just append to $JDK_DIR and we're done.
      JDK_DIR="$JDK_DIR/$DIR"
    fi
  else
    # If the 'java' item is not a symlink (presumably a dir) there will be no
    # symlink to installf items to.
    return 0
  fi

  cd ${JDK_DIR}/jre/lib || exit 2
  # Dynamically determine number of '..' required.
  JAR_DIR=share/jar
  DOTS=.
  LEVEL=0
  FILE=accessibility.properties
  # Keep testing, up to a max of 10 levels to avoid infinite loop.
  while [ ! -f $DOTS/$JAR_DIR/$FILE ] && [ $LEVEL -lt 10 ]
  do
    DOTS="$DOTS/.."
    LEVEL=`expr $LEVEL + 1`
  done
  if [ -f $DOTS/$JAR_DIR/$FILE ]; then
    ln -s $DOTS/$JAR_DIR/$FILE . || exit 2
  fi
  FILE=gnome-java-bridge.jar
  if [ -f $DOTS/$JAR_DIR/$FILE ]; then
    cp $DOTS/$JAR_DIR/$FILE ext || exit 2
  fi

#
# Comment this out since diskless service root and usr packages are installed in seperate directories and do not have a common package database.  installf and removef will not work

#  installf $PKGINST  ${JDK_DIR}/jre/lib/accessibility.properties=../../../../../share/jar/accessibility.properties s || exit 2
#  installf $PKGINST  ${JDK_DIR}/jre/lib/ext/gnome-java-bridge.jar f 0644 root bin || exit 2
#  installf -f $PKGINST || exit 2

  return 0

}
ExecuteInProperEnvironment () {

#
# The following is comment out since pkgcond does not support smosservice
# if $PKGCOND is_smosservice > /dev/null 2>&1 ; then
	# Execute smosservice for diskless clients service commands.
	# We use $BASEDIR because of diskless client restrictions
#	ExecuteDisklessCmds
#	return 0
#  fi

  if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
	# Execute non-global whole root zone commands.
	# Should be same action as the default action.
	ExecuteWholerootCmds
	return 0
  fi

  if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
     # Execute non-global zone commands. Should be no action here
    return 0
  fi

  if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
	# Execute commands specific to an alternate root
	ExecuteDefaultCmds
	return 0
   fi

   if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
	# In a global zone and system is mounted on /.
	# Execute all commands.
	ExecuteDefaultCmds
	return 0
   fi

   return 0
}



if [ -n "$PKG_INIT_INSTALL" ] ; then
   # initial installation of Solaris
	ExecuteDefaultCmds
elif [ -n "$SUNW_PKG_INSTALL_ZONENAME" ] ; then
   # initial installation of non-global zone
   # Execute non-global zone commands. Should be no action here
	exit 0
elif [ -x $PKGCOND ] ; then
   # use pkgcond to determine what the package is being installed to
	   ExecuteInProperEnvironment && exit 0 || exit 1
else
   CheckZones
   if [ "${GLOBAL_ZONE}" = "true" ]; then
        # Call LocalZones() to verify that the /usr/java/jre/lib dir is
        # writeable before trying to create the symlink and copy the file. The
        # function name is unfortunate. To minimize changes it is not being
        # renamed nor this 'if' statement removed. Fixes 6531193.
        LocalZones
   else
	LocalZones
   fi
fi

exit 0
