#############################################################################
# 
# Copyright (C)2004-2005 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: enable
# Description: enable for SUNWccinv pkg. This script is required
# of any package which conforms to the Client Framework and is a cron-based
# agent.
#
# pragma ident	"$Id: enable.sh,v 1.2.2.4 2005/05/11 16:06:49 jr150870 Exp $"
##############################################################################

################################################################################
#  Function of the enable 
#  1.  Create the right environment so that the executable can function correctly
#  2.  create the shell script which runs the agent from the skeleton file.
#  3.  Make an entry into the crontab for the executable.
#  4.  Add an entry to /etc/init.d and /etc/rcN.d so the inventory agent 
#      is run at each boot.
#################################################################################


fw_pkg="SUNWccfw"

verifyFrameworkInstalled()
{
    ###
    ##  Check for the presence of the Framework Package
    ###

    pkginfo $fw_pkg > /dev/null
    if [ $? -ne 0 ] ; then
        $log "enable ERROR : The Framework package $fw_pkg is not available."
        $log "enable ERROR : Please install $fw_pkg and retry "
        exit 1
    fi
}

createcronentry()
{
    user="root"
    
    # make a crontab entry to run the inventory agent periodically
    # NOTE: set the time so the agent runs within 3 minutes and from then
    # on, once a day.
    min=`date +%M`
    hrs=`date +%H`
    
    newmin=`echo "( $min + 3 ) % 60" | bc`
    if [ $newmin -lt $min ] ; then
        # hour will roll over
        if [ $hrs -lt 23 ] ; then
            hrs=`expr $hrs + 1`
        else
            hrs=0
        fi
    fi
    min=$newmin
    
    # the entry to be made in the user's crontab
    # run config provider once a day. 
    
    INV_AGENT_COLLECTOR=/usr/lib/cc-cfw/platform/invagent/bin/cc-invagent
    
    command="$INV_AGENT_COLLECTOR"
    
    entry="$min $hrs * * *"
    entry="$entry $command"
    
    ###
    ## Invoking the create_init entry function with the executable script
    ###
   
    `/usr/lib/cc-cfw/framework/lib/get_interface CREATE_CRONTAB_ENTRY`  "$user" "$entry" platform invagent

}

createInitdEntry()
{
    create=`/usr/lib/cc-cfw/framework/lib/get_interface CREATE_INITD_ENTRY`
    startOrderFile="/usr/lib/cc-cfw/platform/invagent/etc/runlevel"
    ctrlScript="/usr/lib/cc-cfw/platform/invagent/etc/cc-invagent"
    
    $create $ctrlScript $startOrderFile 
}


editOurBinary()
{
    # where things start from
    reloc="/usr/lib/cc-cfw/platform/invagent"
    RELOC_BIN="$reloc/bin"
    RELOC_ETC="$reloc/etc"
    
    ###
    ## Make any configuration changes we need
    ###
    
    # cc-invagent needs JAVA_HOME and LOCK_DIR set
    
    LOCK_DIR="$reloc/lock"
    
    # Which JAVA_HOME?
    START_OK_JDK_PKG_NAME="SUNWj3rt"
    START_OK_JDK_PKG_NAME_64bit="SUNWj3rtx"
    
    # Note: we want stderr from pkginfo here:
    START_JAVA_HOME=`pkginfo -r $START_OK_JDK_PKG_NAME`
    if [ $? -ne 0 ]; then
        # Not installed. What about 64bit?
        START_JAVA_HOME=`pkginfo -r $START_OK_JDK_PKG_NAME_64bit`
        if [ $? -ne 0 ]; then
            # Neither installed. Bail.
            $log "enable ERROR : pkginfo says Java 1.4 run time ($START_OK_JDK_PKG_NAME or $START_OK_JDK_PKG_NAME_64bit) not installed. Can not continue."
            exit 3
        fi
    fi
    # If here then $START_JAVA_HOME is the first part of the path to the jdk
    # (but it does not have $PKG_INSTALL_ROOT prepended as it should)
    # (we know the SUNWj3rt pkg installs in /usr/j2se)
    START_JAVA_HOME="$START_JAVA_HOME/j2se"
    
    # Paranoia: confirm we have a path to java
    if [ -x $START_JAVA_HOME/bin/java ]; then
      :
    else
        $log "enable ERROR : path to java is bad: $START_JAVA_HOME/bin/java. Can not continue"
        exit 4
    fi
    
    # If here, have a path to appropriate version of java.
    
    ###
    # Now edit the cc-invagent
    tmpCcinvagent="$RELOC_BIN/T"
    skel="$reloc/etc/skeleton/cc-invagent.skel"
    # Using perl rather than sed as perl can handle lack of trailing \n on last line.
    perl -p -e "s#\@JAVA_HOME\@#$START_JAVA_HOME#g;" -e "s#\@LOCK_DIR\@#$LOCK_DIR#g;" < $skel > $tmpCcinvagent 
    if [ $? -eq 0 ]; then
        PKGINST=SUNWccinv
        # tell pkg db we are going to change a file
        # This should match the line in the package prototype
        installf $PKGINST $RELOC_BIN/cc-invagent f 0750 root sys
        # change the file
        mv $tmpCcinvagent $RELOC_BIN/cc-invagent
        # finalize the change
        installf -f $PKGINST
    else
        $log "enable ERROR : failed to edit $RELOC_BIN/cc-invagent"
        $log "enable ERROR : Can not continue."
        exit 4
    fi
}

###
# Main

#
# control logging method
#
log=echo
#log="logger -pdaemon.err"

verifyFrameworkInstalled


editOurBinary

createcronentry

createInitdEntry

exit 0

