#!/bin/sh
#############################################################################
# 
# Copyright 2005 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: enable
# Description: enable for SUNWupdatemgrr pkg. This script is required
# of any package which conforms to the Client Framework and is a cron-based
# agent.
#
##############################################################################

################################################################################
#  Function of the enable 
#  1.  Create the right environment so that the executable can function correctly
#  2.  Make an entry into the crontab for the executable.
#################################################################################


fw_pkg="SUNWccfw"
# TODO need to update path
fw_create_crontab="/usr/lib/cc-cfw/framework/lib/create_crontab_entry"

CNS_SWUP_SERVICE_NAME=swupPortalMgmt
CNS_SWUP_COMPONENT_NM=swupagent

createcronentry()
{
    ###
    ##  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

    ###
    ##  Check for the presence of the registration function of the Configuration Framework Package
    ###

    if [ ! -f  $fw_create_crontab ] ; then
        $log "enable ERROR : Enabling capability not provided by the Configuration Package framework $config_fw_pkg"
        exit 1
    fi

    ###
    ## Invoking the create_init entry function with the executable script
    ###
    $fw_create_crontab  $user "$entry" $CNS_SWUP_SERVICE_NAME $CNS_SWUP_COMPONENT_NM

}


user="root"

# make a crontab entry to run the swup agent every hour
# NOTE: set the time so the agent runs within 15 minutes and from then
# on, once a day.

# this should randomize when it runs a bit
min=`date +%M`
#hrs=`date +%H`

#echo "min:$min hrs:$hrs"

newmin=`echo "( $min + 15 ) % 60" | /usr/bin/bc`
min=$newmin
 
#echo $min

#if [ $hrs -lt 11 ] ; then
#	hrs="0,2,4,6,8,10,12,14,16,18,20,22"
#else
#	hrs="1,3,5,7,9,11,13,15,17,19,21,23"
#fi

min=$newmin

# the entry to be made in the user's crontab
# run config provider once a day. 

SWUP_AGENT_START=/usr/lib/patch/swupas

command="$SWUP_AGENT_START > /dev/null 2>&1"

#entry="$min $hrs * * *"
entry="$min * * * *"
entry="$entry $command"

createcronentry

exit 0

