#!/bin/sh
# $Id: postupgrade.sh 107511 2012-10-30 13:38:57Z ww161634 $
#  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information 
#  and should be treated as confidential.
#
#  Component: RPM post upgrade script
#  Author: Bill Weir

INSTDIR="$1"

# Sanity check.  This script potentially deletes a lot of stuff, so we want
# to take care.
OK=""
case "${INSTDIR}" in
/?*) [ -f "${INSTDIR}/bin/bin/ttaauxserv" -a -d "${INSTDIR}/etc/pkg" -a -d "${INSTDIR}/var/ens" ] && OK="t"
esac
[ -z "${OK}" ] && exit 1

[ -s "${INSTDIR}/bin/scripts/en-us/tta_setup" ] && . "${INSTDIR}/bin/scripts/en-us/tta_setup"	# Yes, this needs i18n

CONTENTS=/var/sadm/install/contents
LC_ALL=C LC_COLLATE=C export LC_ALL LC_COLLATE

RPMS=""
for rpmfile in ${INSTDIR}/etc/pkg/tta*/rpm.pkg; do
    RPM=`cat ${rpmfile} 2>/dev/null`
    case "${RPM}" in
    tta*-[0-9]*.[0-9]*-[0-9][0-9][0-9]) RPMS="${RPMS} ${RPM}"
    esac
done
[ -z "${RPMS}" ] && exit 1

tmpbase=${TMPDIR:-/tmp}/postupg; tmp1=${tmpbase}1$$ tmp2=${tmpbase}2$$ tmp3=${tmpbase}3$$ tmp4=${tmpbase}4$$

CleanExit()
{
    rm -f ${tmp1} ${tmp2} ${tmp3} ${tmp4} 2>/dev/null
    exit $1
}

trap 'CleanExit 1' 1 2 3 15

# Get all tta* files from the PKG contents database.
:>${tmp1}
[ -s "${CONTENTS}" ] && {
    sed '/^#/d;/ tta/!d;s/ .*//' ${CONTENTS} | sort -u >${tmp1}
}

# Get all files from the installed RPMs
rpm -ql ${RPMS} 2>/dev/null | sort -u >${tmp2}

# Build a list of files to be deleted (files that belong to one of the old
# packs, but not to the RPMs).  The exclusion list (files that we won't
# delete) is possibly too wide-ranging, but better safe than sorry.
# It's a set of egrep patterns, which get ${INSTDIR}/ prepended to them.
#
EXCLUSIONS=\
'.*=
bin/scripts
bin/lp
etc/pkg/datadir
etc/templates
var/server'

# Apache/Tomcat exclusions
#
# - if there's only one version subdirectory (ie Apache/Tomcat is the same
#   version as in the previous SGD installation), don't touch it.
#
# - if more than one version subdirectory, exclude just the top-level
#   subdirectory from the purge.  That will cause all lower level
#   package-owned stuff to be purged, but will preserve things we have
#   created ourselves such as backed-up conf directories
#
HowManyApacheDirs=`ls -d ${INSTDIR}/webserver/apache/[0-9]* 2>/dev/null | wc -l`
if [ ${HowManyApacheDirs} -eq 1 ]; then
    APACHE_EXCLUSIONS='webserver/apache'
else
    APACHE_EXCLUSIONS='webserver/apache/[^/][^/]*$'
fi
HowManyTomcatDirs=`ls -d ${INSTDIR}/webserver/tomcat/[0-9]* 2>/dev/null | wc -l`
if [ ${HowManyTomcatDirs} -eq 1 ]; then
    TOMCAT_EXCLUSIONS='webserver/tomcat'
else
    TOMCAT_EXCLUSIONS='webserver/tomcat/[^/][^/]*$'
fi
EXCLUSIONS="${EXCLUSIONS}
${APACHE_EXCLUSIONS}
${TOMCAT_EXCLUSIONS}"

FULL_PATH_EXCLUSIONS=`echo "${EXCLUSIONS}" | sed "s|^|${INSTDIR}/|"`
comm -23 ${tmp1} ${tmp2} | egrep -v "${FULL_PATH_EXCLUSIONS}" >${tmp3}

# Remove the files to be purged
echo "${l_removingoldpacks}"
grep "^${INSTDIR}/....*/" ${tmp3} | xargs rm -rf 2>/dev/null

# Clear the PKG contents database, and call pkgrm to tidy up any other
# package-related data
[ -s "${CONTENTS}" -a -x "/usr/bin/pkginfo" -a -x "/usr/bin/pkgrm" ] && {
    (egrep -v '^(#|.* tta)' ${CONTENTS}; grep '^#' ${CONTENTS}) >${tmp4} && cp ${tmp4} ${CONTENTS}
cat >${tmp4} <<_EOF
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=quit
rdepend=quit
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=default
_EOF

    pkginfo | awk '$2~/^tta.*/{print $2}' | while read pkg; do
	rm -f "/var/sadm/pkg/${pkg}/install/preremove" "/var/sadm/pkg/${pkg}/install/postremove" 2>/dev/null
	pkgrm -n -a ${tmp4} ${pkg}
    done
}

CleanExit 0
