#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: preremove
# Description: postinstall for SUNWupdatemgrr package
#
##############################################################################

################################################################################
#  Function of the preremove 
#  1.  Unregister this package with the Client Configuration Services registry, by
#      removing the registration entries from the registry
#  2.  Remove the cron entry for the swup agent that runs in auto analysis mode
#################################################################################

################################################################################
#  1.  Unregister this package with the Client Configuration Services registry, by
#      removing the registration entries from the registry
################################################################################

SCRIPT_DIR="${PKG_INSTALL_ROOT}/usr/lib/cc-cfw/swupPortalMgmt/swupagent"
DISABLE_SCRIPT="${SCRIPT_DIR}/disable"

#  Run the disable script to remove the cron entries installed as part of the 
#  CNS Framework Services
$DISABLE_SCRIPT

#################################################################################
#  2.  Remove the cron entry for the swup agent that runs in auto analysis mode
#################################################################################

CRONTAB_FILE="${PKG_INSTALL_ROOT}/var/spool/cron/crontabs/root"
CRON_ENTRY=swupAuto

# create temp dir for manipulation of cron files
TMP_DIR="/tmp/tmp_crons.$$"
if [ ! -d $TMP_DIR ] ; then
    /usr/bin/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 a temp file
if test X"$PKG_INSTALL_ROOT" = X; then
    /usr/bin/crontab -l > $OLD_CRONTAB
    if [ $? -ne 0 ] ; then
            $log "Error: could not execute crontab -l"
            exit 1
    fi
else
    /usr/bin/cp $CRONTAB_FILE $OLD_CRONTAB
    if [ $? -ne 0 ] ; then
            $log "Error: could not copy $CRONTAB_FILE"
            exit 1
    fi
fi

# Find out if there is an entry for the swup agent to run auto analysis,
# and if found, remove it.

CUR_CRON_ENTRY=`/usr/bin/fgrep $CRON_ENTRY $OLD_CRONTAB 2>/dev/null`

if [ -n "$CUR_CRON_ENTRY" ] ; then

    # edit the data to remove the line with the entry

    /usr/bin/sed /"${CRON_ENTRY}"/d  $OLD_CRONTAB > $NEW_CRONTAB
    if [ $? -ne 0 ]; then
        $log "ERROR: sed edit of crontab file failed"
        exit 1
    fi

    # update the crontab file
    if test X"$PKG_INSTALL_ROOT" = X; then
        /usr/bin/crontab $NEW_CRONTAB
        if [ $? -ne 0 ] ; then
           $log "ERROR: installation of edited crontab failed"
           exit 1
        fi
    else
        /usr/bin/cp $NEW_CRONTAB $CRONTAB_FILE
        if [ $? -ne 0 ] ; then
           $log "Error: could not copy $NEW_CRONTAB to $CRONTAB_FILE"
           exit 1
        fi
    fi
fi

# remove the data directory as it is no longer needed
/usr/bin/rm -rf $TMP_DIR

exit 0
