#!/bin/sh

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

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

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}
}

get_patchrev()
{
          pkginfo="/var/sadm/pkg/$1/pkginfo"
          if [ ! -f $pkginfo ]; then
                  # Package not installed
                  return 0
          fi

          # See if the patch is installed
          x=`grep "PATCHLIST" $pkginfo | \
                  grep $2 | sed "s/.*$2-//" | sed 's/ .*//'`
          if [ "$x" -ne 0 ]; then
                  return $x
          fi

          # See if the patch has been obsoleted by another patch
          maxrev=0
          for x in `grep "^PATCH_INFO" $pkginfo | grep Obsoletes: | \
                  sed 's/.*Obsoletes://' |  sed 's/Requires.*//' | \
                  grep $2 | sed "s/.*$2-//" | sed 's/ .*//'`
          do
                  if [ $maxrev -lt $x ]; then
                          maxrev=$x
                  fi
          done
          if [ "$maxrev" -ne 0 ]; then
                  return $maxrev
          fi

          # Patch is not installed or obsoleted
          return 0
}

###########################################
#
# add entries to /var/spool/cron/crontabs/root
#
###########################################
update_root_crontab()
{
        update_file=${FS_ROOTDIR}/var/spool/cron/crontabs/root

        # remove the old entries
        remove_SUNWscr_edits ${update_file}
        
        # add the new entries to the file
${CAT} << EOF >> ${update_file}
# ${STARTTAG}
20 4 * * 0 /usr/cluster/lib/sc/newcleventlog /var/cluster/logs/eventlog
20 4 * * 0 /usr/cluster/lib/sc/newcleventlog /var/cluster/logs/DS
# ${ENDTAG}
EOF

        if [ $? -ne 0 ]; then
                echo "${PROG}: unable to update ${update_file}"
                return 1
        fi

        return 0
}


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

        endpat=\$

        # 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

#
# Add back the crontab entries removed by the postpatch script
# if we are backing out to rev 2 or less.
#

# See what version of the patch remains installed after this one
# was backedout.

get_patchrev SUNWscr 120501
patchrev_remain=$?

if [ $patchrev_remain -lt 03 ]; then
   update_root_crontab     || errs=`expr ${errs} + 1`
fi

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