#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: set_agent_frequency
# Description: script to change the frequency of the Inventory agent

# pragma ident	"$Id: set_agent_frequency.sh,v 1.2 2004/12/01 20:48:46 sd148285 Exp $"
##############################################################################

#!/bin/sh

if [ -z "$1" ] ; then
  echo "Usage: $0 <frequency in hours>"
  exit 1
fi

agentfrequency=`expr $1 - 0`
if [ $? -ne 0 ] ; then
    echo "ERROR: Please enter a frequency of the agent in hours"
    exit 1
fi

if [ $agentfrequency -eq 24 ] ; then
   echo "Default value is specified for the agent frequency.  No changes made."
   exit 0
fi

provider_name="InventoryAgent"
real_user=root

###
##  Check the frequency set in the InventoryAgent.properties file and if it is 24 hours
## exit without modifying the cron entry.
###

# make sure the data directory exists for manipulation of cron files
tmp_dir="$PKG_INSTALL_ROOT/tmp/$provider_name"
if [ ! -d $tmp_dir ] ; then
	echo "WARNING: $tmp_dir does not exist; creating it now."
	mkdir -m 0755 $tmp_dir
	if [ $? -ne 0 ] ; then
		$log "ERROR: could not create $tmp_dir"
		exit 1
	fi
fi

# create tmp files of crontab entries
# old file is before the removal of crontab entry
# new file is after the removal of crontab entry
old_crontab="$tmp_dir/old_crontab.$$"
new_crontab="$tmp_dir/new_crontab.$$"

# read the crontab into this file
crontab -l $real_user > $old_crontab
if [ $? -ne 0 ] ; then
	echo "ERROR: could not get crontab for $real_user"
	exit 1
fi

# Find if there is an entry for the inventory agent and if found,
# remove it.

crontab_entry=`fgrep $provider_name $old_crontab 2>/dev/null`
if [ -n "$crontab_entry" ] ; then
   
    # edit the data to remove the line with the entry
    sed /${provider_name}/d $old_crontab > $new_crontab
    if [ $? -ne 0 ]; then
            echo "ERROR: sed edit of crontab file failed"
            exit 1
    fi

    # 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
    hrsentry=$hrs
    thrs=$hrs

    while [ $hrs -ge $thrs ] ; 
    do  
        
        hrs=`expr $hrs + $agentfrequency` 
        if [ $hrs -ge 24 ] ; then
            hrs=0
            break;
        fi
        hrsentry="$hrsentry,$hrs"
    done
    
    while [ $hrs -lt $thrs ] ;
    do
       hrsentry="$hrs,$hrsentry"
       hrs=`expr $hrs + $agentfrequency` 
    done

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

    PX_AGENT_JAR=/opt/SUNWcctpx/lib/cctagent.jar
    INV_AGENT_JAR=/opt/SUNWccinv/lib/ccinv.jar
    LIB_PATH="/usr/lib"
    CLASSPATH="$PX_AGENT_JAR:$INV_AGENT_JAR"

    command="java -cp $CLASSPATH -Djava.library.path=$LIB_PATH com.sun.cc.transport.agent.inventory.InventoryAgent"

    entry="$min $hrsentry * * *"
    entry="$entry $command"
    echo "$entry" >> $new_crontab
    ##############

    # update the user's crontab file
    su $real_user -c "crontab < $new_crontab"
    if [ $? -ne 0 ] ; then
            echo "ERROR: installation of edited crontab failed"
            exit 1
    fi
fi

# remove the data directory as it is no longer needed
rm -rf $tmp_dir

exit 0

