#!/bin/sh

#
# listpatches - List all installed patches
#
# MOD	DATE		WHO	WHAT
# ---	----		---	----
# 0	29-mar-96	sab	Originate.
# 1	10-jul-97	jar	added ability to find patch dir from pkginfo,
#				for rel5.3 or pre-rel5.3 dir structure.

##############################################################################
# Define functions the body of the script will be calling.
##############################################################################

#
# Function to print all patches for a given release.
# The function assumes that the variable $rel contains
# the release.
#
printPatches() {
#	if [ ! -d ${RELDIR}/patches ]; then
#		/bin/echo "No patches in ${RELDIR}"
#	else
	if [ -d ${RELDIR}/patches ]; then
		origDir=`pwd`
		cd ${RELDIR}/patches
		patchList=*[0-9]*
		if [ "$patchList" = "" ]; then
			/bin/echo "No patches in ${RELDIR}"
		else
		echo ""
		/bin/echo "\nPatches in ${version}:"
			for p in $patchList; do
				title=`grep Title= $p/patchinfo`
				if [ "$title" = "" ]; then
					title="*** MISSING TITLE ***"
				else
					title=`expr "$title" : "Title=\(.*\)"`
				fi
				/bin/echo "\t$p\t$title"
			done
		fi	
		cd $origDir
	fi
}

##############################################################################
# The main body of the script.
##############################################################################

#
# Make a list of PERIpkgs which are actually installed.
#
pkglist=`ls -d /var/sadm/pkg/PERI*`

#
# Iterate through releases and show patches where appropriate.
#
for PERIpkg in $pkglist; do
	#  get release dir from pkginfo
	if [ -f ${PERIpkg}/pkginfo ]; then
	RELDIR=`cat ${PERIpkg}/pkginfo | grep RELDIR | cut  -d= -f2`
	version=`cat ${PERIpkg}/pkginfo | grep VERSION | cut -d= -f2`
	reldirs="$reldirs ${RELDIR}:${version}"
	fi
done

# get rid of duplicate patch dirs---old PERIvps's and PERIpro's may have
# same reldirs....
RELDIRS=`echo $reldirs  | tr " " "\012" | sort | uniq | tr "\012" " "`
for pair in $RELDIRS; do
	#
	# Go print the patches.
	#
	RELDIR=`echo $pair | cut -d: -f1`
	version=`echo $pair | cut -d: -f2`
	printPatches
done

#
# Exit cleanly.
#
exit 0
