#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: start
# Description: start script for the Inventory Agent.

# pragma ident	"$Id: cc-invagent.sh,v 1.2 2005/05/19 17:29:00 js149924 Exp $"
##############################################################################
## Very Important: This file is regenerated by enable.
## If you want to edit it, edit 
## src/package/solaris/SUNWccinv/etc/skeleton/cc-invagent.skel 
## or your changes will be LOST.
##############################################################################

##
# Log a message to syslogd
# $1 - priority. Something like daemon.warning
# rest - the message
log()
{
    level=$1
    shift
    msg="$*"
    logger -t "IM-CC_ENG-inv cc-invagent" -p $level "$msg"
}

#########
# We want only one of these running at a time so we lock at startup 
# and unlock when done.
# There is a directory we use for storing our lock files and only ours.
LOCK_DIR=@LOCK_DIR@
## Debug
##LOCK_DIR=/var/opt/SUNWccinv/lock
LOCK_FILE=${LOCK_DIR}/lock.$$

##
# lock
# We have a directory for lock files. 
# To lock we touch a file there with our pid in its name. 
# Then we look to see if there are any files there other than ours. 
# If none, continue.
# If some and they are newer than 12 hours old, print an informative message
# and quit.
# TBD:If some and they are more than 12 hours old, remove them, continue
lock()
{
    # put down our lock so others will notice we have started
    touch $LOCK_FILE
    # Has anyone else started?
    others=`ls -1 $LOCK_DIR | grep -v $$`
    if [ -n "$others" ]; then
        # OK, this is complex but I know of no way in sh to ask about the
        # age of a file in less than one day chunks.
        # Create a marker file which is twelve hours older than now
        hour=`date +"%H"`
        day=`date +"%d"`
        twelveHoursAgo=`expr $hour - 12`
        # Adjust for midnight.
        if [ $twelveHoursAgo -lt 0 ]; then
            twelveHoursAgo=`expr 24 + $twelveHoursAgo`
            day=`expr $day - 1`
        fi
        # Zero pad $day
        if [ "$day" -lt 10 ]; then
            day="0$day"
        fi
        #  and $twelveHoursAgo
        if [ "$twelveHoursAgo" -lt 10 ]; then
            twelveHoursAgo="0$twelveHoursAgo"
        fi
        twelveHoursAgo=`date +"%m${day}${twelveHoursAgo}00"`
        MARKER=olderThan.$$
        touch $twelveHoursAgo $MARKER
        # find all files older than that marker and remove them.
        find $LOCK_DIR -type f ! -newer $MARKER | xargs rm -f
        # Previous also removes the marker.
        
        # Is there anything left?
        others=`ls -1 $LOCK_DIR | grep -v $$`
        if [ -n "$others" ]; then
            msg="Another Inventory Agent is running. Try again later"
            log daemon.warning $msg
            echo $msg
            exit 0
        fi
    fi
}

##
# unlock
# Remove our file in the lock directory.
unlock()
{
    rm -f $LOCK_FILE
}

##
# trap exit so we can unlock
trap unlock 0 2 3 15

platform="/usr/lib/cc-cfw/platform"
invagent="$platform/invagent/"
PX_AGENT_JAR="$platform/transport/lib/cctagent.jar"
INV_AGENT_JAR="$invagent/lib/ccinv.jar"
INV_COLLECTOR="$invagent/bin/solaris_inventory"
log_prop="$invagent/etc/logging.properties"

LIB_PATH="/usr/lib"
CLASSPATH="$CLASSPATH:$PX_AGENT_JAR:$INV_AGENT_JAR"

# We want to run with the version of java we went through QA with rather
# than the default on this platform so we set this path at install time.
JAVA_HOME=@JAVA_HOME@

export LIB_PATH
export CLASSPATH

lock

# First gather the data:
$INV_COLLECTOR

# Then send it up via the transport client:
JavaOut=/tmp/agent.out.$$.log
$JAVA_HOME/bin/java -cp $CLASSPATH -Djava.library.path=$LIB_PATH -Djava.util.logging.config.file=$log_prop com.sun.cc.transport.agent.inventory.InventoryAgent > $JavaOut 2>&1
rc=$?

unlock

if [ $rc -ne 0 ]; then
    log daemon.warning "Inventory Agent returned error: $rc : `cat $JavaOut`"
fi

rm -f $JavaOut

exit $rc
