#! /usr/bin/sh
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
#DEBUG=1
#

CheckZones()
{
    ZONENAME=`/usr/bin/zonename`
    if [ ${ZONENAME} != "global" ]; then
        echo "Update Manager Notification is only supported in the global zone. Exiting..."
        logger -tswupnot -p2 "Non global zone not supported. zone: $ZONENAME. Exiting"
        exit 1
    fi
}

# Update Manager Notification app will only run in the global zone
CheckZones

###
# setup LD_LIBRARY_PATH
###
if [ $LD_LIBRARY_PATH ] ; then
    LD_LIBRARY_PATH=/usr/lib/patch/jdic:${LD_LIBRARY_PATH}
else
    LD_LIBRARY_PATH=/usr/lib/patch/jdic
fi
export LD_LIBRARY_PATH
if [ $DEBUG ] ; then
    echo LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
fi

DISABLE_NOTIFICATION=disableNotification
SOFTWARE_UPDATE=.softwareupdate
DELAY=delay
PID_FILE=pid

JAVA_EXEC=/usr/bin/java
CCR_EXEC=/usr/lib/cc-ccr/bin/ccr
MKDIR_EXEC="/usr/bin/mkdir -p"
PS_EXEC=/usr/bin/ps
GREP_EXEC=/usr/bin/grep
AWK_EXEC=/usr/bin/awk
CAT_EXEC=/usr/bin/cat
WHOAMI=/usr/ucb/whoami

###
# ---- find any running process that matches the input
#      parameter, and that is a notification app.
###
findNotAppProc() {
    pid=`$PS_EXEC -ef |
    $GREP_EXEC -w $1 |
    $GREP_EXEC -v grep |
    $GREP_EXEC -w swupna.jar`
    echo $pid
}

###
# ---- find the named process(es)
###
findProc() {
    pid=`$PS_EXEC -ef |
    $GREP_EXEC -w $1 |
    $GREP_EXEC -v grep |
    $AWK_EXEC '{print $2}'`
    echo $pid
}

###
# ---- find any running process that matches the input 
#      parameter, and that is owned by this userid.
###
findUserIdProc() {
    who=`$WHOAMI`
    pid=`$PS_EXEC -ef |
    $GREP_EXEC -w $1 |
    $GREP_EXEC -v grep |
    $GREP_EXEC -w $who`
    echo $pid
}

###
# ---- check for already running notification app process for this user ----
#      return 0 - 'running swupnot process found'
#             1 - 'NO running swupnot process found'
###
checkRunningNotApp() {

    if [ -f ${HOMEDIR}/${PID_FILE} ]; then
        if [ $DEBUG ] ; then
	    echo "checkRunningNotApp: HOMEDIR=$HOMEDIR"
        fi

	###
	# get the process id saved in the 'pid' file.  if 'pid' file 
        # does not exist than no need to check any further, return 1 
  	# to indicate 'not found'
	###

        SAVE_PID=`$CAT_EXEC $HOMEDIR/$PID_FILE`
	if [ $? != 0 ]
        then
	   echo "cannot find $HOMEDIR/$PID_FILE file" >> $TRC_FILE
           rc=1
	   return
        fi
	
        if [ $DEBUG ] ; then
	    echo "SAVE_PID=$SAVE_PID"
	fi

	###
	# get the process id of any running notification app process, 
	# if no process id is found, than no need to check any further,
	# return 1 to indicate 'NO running swupnot process found'
	###

	found_pid=`findNotAppProc $SAVE_PID`
	echo "search and found swupnot pid=$found_pid" >> $TRC_FILE

	if [ "$found_pid" != "" ] ; then
	    echo "found swupnot process" >> $TRC_FILE
            rc=0			# found the running process
	else

	    echo "does not find any swupnot process" >> $TRC_FILE
	    rc=1
        fi

    ###
    # there is no pid file found, return 1 (no swupnot found).
    ###
    else
	echo "pid file does not exist" >> $TRC_FILE
	rc=1
    fi
}

###
# ---- Check for negative assetid, if it is, replace leading "-" with "N".
###
checkNegAssetId() {
    GDIR=`echo $ASSETID | sed -e "s/^-/N/"`
    if [ $DEBUG ] ; then
        echo "here is the fixed name: $GDIR"
    fi
}

###
# ---- Check for gnome-netstatus-applet process to be running before
#      we can start notification application.  If no such process exists,
#      sleep a second and recheck, repeat this process for 60 secs and
#      if such process is still not existed, we could be in CDE window
#      environment, quits.
###
checkPreReq() {

    ###
    #  ---- check for CDE environment
    ###
    if [ $SDT_NO_TOOLTALK ] ; then
	echo "JDS window environment, continue." >> $TRC_FILE
    elif  [ $SDT_NO_DTDBCACHE ] ; then
	echo "in JDS window environment, continue." >> $TRC_FILE
    else
	echo "in CDE window environment, quits." >> $TRC_FILE
        exit 0
    fi

    timeout=1
    while [ $timeout -le 60 ]; do
        gpid=`findUserIdProc gnome-netstatus-applet`
	if [ $DEBUG ] ; then
	    echo "gpid=$gpid"
	fi

       	if [ "$gpid" = "" ] ; then
       	    sleep 1
	    timeout=`(expr $timeout + 1)`
	else
	    timeout=71
       	fi
    done

    if [ $timeout -eq 61 ]; then
	echo "timing out waiting for gnome-netstatus-applet, quits." >> $TRC_FILE
	exit 0
    fi
}


###
#  wait to make sure that gnome session is started.
###
if [ "$1" = "-wait" ] || [ "$2" = "-wait" ] || [ "$3" = "-wait" ]; then
   sleep 20
fi

if [ "$1" = "-debug" ] || [ "$2" = "-debug" ] || [ "$3" = "-debug" ]; then
   DEBUG=1
fi

if [ $DEBUG ] ; then
    echo "home = $HOME"
fi

who=`$WHOAMI`
TRC_FILE="/tmp/${who}swup.trc"

if [ $DEBUG ] ; then
    echo "tracefile=$TRC_FILE"
fi

#####
# ---- read the assetId and use it to build the softwareupdate path
#####

##
# TODO need to check for CCR package installed?
##

rm -f $TRC_FILE
HOSTID=`/usr/bin/hostid`
ASSETID=`$CCR_EXEC -g cns.assetid`

if [ $DEBUG ] ; then
    echo "$ASSETID"
fi

###
#  build the home directory for this user.
###
if test $ASSETID ; then
   
   # if negative, replace '-' with 'N'
   GDIR=""
   checkNegAssetId
   if [ $DEBUG ] ; then
       echo "after checkNegAssetId(), gdir=$GDIR"
   fi

   HOMEDIR=${HOME}/${SOFTWARE_UPDATE}/${GDIR}
   echo "user is registered" >> $TRC_FILE
else
   HOMEDIR=${HOME}/${SOFTWARE_UPDATE}/${HOSTID}
   echo "user is not registered" >> $TRC_FILE
fi

echo "HOMEDIR=$HOMEDIR" >> $TRC_FILE

###
# ----- if this user's home directory exists -----
###
if [ -d ${HOMEDIR} ]; then
    echo "HOMEDIR=$HOMEDIR exists" >> $TRC_FILE
    
    ###
    # ----- if 'disable notification app' flag is off, than proceed 
    #       to start Notification Apps
    ###
    if [ ! -f ${HOMEDIR}/${DISABLE_NOTIFICATION} ]; then
	echo "disable notification flag is off" >> $TRC_FILE

	###
	# ---- check for any already running notification apps for
	#      this user.
	###
        rc=0
        checkRunningNotApp

	echo "checkRunningNotApp return $rc" >> $TRC_FILE
        if [ $rc -eq 1 ] ; then

	    echo "NO running NotApp process found" >> $TRC_FILE
	    ###
	    # ---- check for gnome applet to be there.
	    ###
	    checkPreReq
	    echo "gnome is ready" >> $TRC_FILE

	    ###
	    # ---- start Notification Application
	    ###
            if [ -f ${HOMEDIR}/${DELAY} ]; then
                $JAVA_EXEC -jar /usr/lib/patch/swupna.jar -delay 6 "$@" &
	    else
                $JAVA_EXEC -jar /usr/lib/patch/swupna.jar "$@" &
	    fi
	    echo "NotApp is started" >> $TRC_FILE

	    ###
	    # --- save the process id into the 'pid' file.
	    ###
            echo $! > "$HOMEDIR/$PID_FILE"
	    echo "saved pid" >> $TRC_FILE
        else
	    echo "don't start NotApp and quits now" >> $TRC_FILE
            echo "The Sun Notification Application is already running in "
	    echo "the system for this user."
        fi

    ###
    # ----- if 'disable notification app' flag is on, cannot start Not Apps.
    ###
    else

	echo "disable notification flag on, cannot start NotApp" >> $TRC_FILE
	echo "don't start NotApp and quits now" >> $TRC_FILE

        echo "The Sun Notification Application is disabled, To enable it,"
	echo " use the Preferences menu in the Sun Update Manager."
    fi

###
# ----- The $HOME/$SOFTWARE_UPDATE/$GDIR|$HOSTID directory does NOT
#       exist, create it and start Notification Application.
###
else
    echo "Cannot find $HOMEDIR, will create it" >> $TRC_FILE

    # --- create the home directory
    $MKDIR_EXEC $HOMEDIR

    # ---- check for any already running notification apps.
    rc=0
    checkRunningNotApp
    echo "checkRunningNotApp return $rc" >> $TRC_FILE

    if [ $rc -eq 1 ] ; then

	echo "found NO running NotApp process"  >> $TRC_FILE
  	###
	# ---- check for gnome applet to be there.
  	###
	checkPreReq
	echo "gnome is ready (2)"  >> $TRC_FILE

  	###
        # --- start Notification Apps
  	###
        if [ -f ${HOMEDIR}/${DELAY} ]; then
            $JAVA_EXEC -jar /usr/lib/patch/swupna.jar -delay 6 "$@" &
	else
            $JAVA_EXEC -jar /usr/lib/patch/swupna.jar "$@" &
	fi
	echo "NotApp is started (2)"  >> $TRC_FILE

  	###
	# --- save the process id into the 'pid' file.
  	###
        echo $! > "$HOMEDIR/$PID_FILE"
	echo "saved pid (2)"  >> $TRC_FILE
    else
	echo "don't start NotApp and quits now (2)" >> $TRC_FILE
        echo "The Sun Notification Application is already running in "
	echo "the system for this user."
    fi
fi

exit 0
