#!/bin/sh

PATH=/usr/bin:/usr/sbin
export PATH

PROG=postpatch

TMPFILES=${FS_ROOTDIR}/tmp/postpatch.$$

###########################################
#
# Cleanup any temporary files on exit/interrupt
#
###########################################
cleanup()
{
        exit_code=$1

        if [ "${TMPFILES}" ]; then
                rm -f ${TMPFILES}
        fi

        exit ${exit_code}
}

###########################################
#
# remove_ascii_edits <filename>
#
#       Backout all line from the given ascii <filename>
#       indicated by the SUNWscr comment tag lines.
#
###########################################
remove_ascii_edits()
{
        PKGNAME=$1
        update_file=$2

        endpat=\$

        STARTTAG="Start of lines added by ${PKGNAME}"
        ENDTAG="End   of lines added by ${PKGNAME}"

        # If the file does not exist, return without error
        if [ ! -f "${update_file}" ]; then
                return 0
        fi

        while :
        do
                ${GREP} "${STARTTAG}$endpat" ${update_file} >/dev/null 2>&1
                case $? in
                2)      # error reading file
                        echo "${PROG}:  error reading ${update_file}"
                        return 1
                        ;;

                0)      # grep found the string, so get rid of the entries
                        ${ED} -s ${update_file} << EOF >/dev/null 2>&1
/${STARTTAG}$endpat/,/${ENDTAG}$endpat/d
w
q
EOF
                        if [ $? -ne 0 ]; then
                                echo "${PROG}: problem updating ${update_file}"
                                return 1
                        fi
                        ;;

                *)
                        break
                        ;;
                esac
        done

        return 0
}


################################################################
# Main
################################################################
errs=0

# catch common signals
trap 'echo "${PROG}: caught signal";  cleanup 3' 1 2 3 15

CAT=/bin/cat
CP=/bin/cp
EGREP=/bin/egrep
GREP=/bin/grep
EXPR=/bin/expr
HEAD=/bin/head
MV=/bin/mv
NAWK=/bin/nawk
RM=/bin/rm
SED=/bin/sed
ED=/bin/ed
SORT=/bin/sort
TAIL=/bin/tail
UNAME=/bin/uname
WC=/bin/wc

# Set rootdir for relocatability and alternate install locations.
# All file system access should be relative to this root.
if [ $ROOTDIR = "/" ]; then
   FS_ROOTDIR=""
else 
   FS_ROOTDIR="${ROOTDIR}"
fi

#
# Remove the crontab entries added by SUNWscr in the FCS pkgs for S10.
#
remove_ascii_edits SUNWscr ${FS_ROOTDIR}/var/spool/cron/crontabs/root || errs=`expr ${errs} + 1`

if [ ${errs} -ne 0 ]; then
        cleanup 1
else
        cleanup 0
fi

  
exit 0
