#!/bin/sh
###############################################################################
#
#	Project:   Sun(SM) Net Connect
#	Subsystem: netconnect Scripts
#	Module:	   destroy_crontab_entry
#
#	Usage:
#		destroy_crontab_entry <program name>
#
#	Returns:
#		0 - success
#		1 - failure
#
# Copyright (C) 2004 by Sun Microsystems, Inc.
# All rights reserved.
###############################################################################

log="echo"

if [ $# -ne 4 ] ; then
    echo "Usage : `basename $0` <user> <command> <service_name> <component_name>" 
    exit 1
fi

user="$1"
command="$2"
service_name="$3"
component_name="$4"
disabled_status=2

#
# Validate the service and component for registration with the framework.
#
if [ $PKG_INSTALL_ROOT ] && [ -n $PKG_INSTALL_ROOT ] ; then
    if [ ! -d "${PKG_INSTALL_ROOT}/usr/lib/cc-cfw/${service_name}" ] || [ ! -d "${PKG_INSTALL_ROOT}/usr/lib/cc-cfw/${service_name}/${component_name}" ]; then
        $log "ERROR : Either ${service_name}, or ${component_name} are not properly registered with the framework."
        exit 1
    fi
else
    if [ ! -d "/usr/lib/cc-cfw/${service_name}" ] || [ ! -d "/usr/lib/cc-cfw/${service_name}/${component_name}" ]; then
        $log "ERROR : Either ${service_name}, or ${component_name} are not properly registered with the framework."
        exit 1
    fi
fi

# make sure the data directory exists for manipulation of cron files
tmp_dir="/tmp/tmp_crons"
if [ ! -d $tmp_dir ] ; then
    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 temp file
if [ $PKG_INSTALL_ROOT ] && [ -n $PKG_INSTALL_ROOT ] ; then
    /usr/bin/cat ${PKG_INSTALL_ROOT}/var/spool/cron/crontabs/${user} > $old_crontab
    if [ $? -ne 0 ] ; then
        $log "ERROR: Cannot access ${PKG_INSTALL_ROOT}/var/spool/cron/crontabs/${user}"
        exit 1
    fi
else
    crontab -l "$user" > $old_crontab
    if [ $? -ne 0 ] ; then
        $log "ERROR: could not get crontab for $user"
        exit 1
    fi
fi

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

crontab_entry=`fgrep "$command" $old_crontab 2>/dev/null`

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

    # update the user's crontab file
    if [ $PKG_INSTALL_ROOT ] && [ -n $PKG_INSTALL_ROOT ] ; then
        /usr/bin/cp $new_crontab ${PKG_INSTALL_ROOT}/var/spool/cron/crontabs/${user}
        if [ $? -ne 0 ] ; then
                $log "ERROR: Edit of ${PKG_INSTALL_ROOT}/var/spool/cron/crontabs/${user} failed"
                exit 1
        fi
    else
        su $user -c "crontab $new_crontab"
        if [ $? -ne 0 ] ; then
                $log "ERROR: installation of edited crontab failed"
                exit 1
        fi
    fi
fi

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

### load the ENUM file ##
### Get the int values of the enabled status here.. ###
enum_file=/usr/lib/cc-cfw/framework/lib/status_enum
if [ -f $enum_file ] ; then
    . $enum_file
else
    $log status_enum interface not found. 
    exit 1
fi

`/usr/lib/cc-cfw/framework/lib/get_interface SET_COMPONENT_STATUS` $service_name $component_name $DISABLE_STATUS_VALUE


exit 0
