#!/bin/sh

#
# $Id: perirev,v 1.7.4.1 2005/08/25 17:34:13 akeenan Exp $
# This program reports information on installed peripackages
#	and components
#
# $Log: perirev,v $
# Revision 1.7.4.1  2005/08/25 17:34:13  akeenan
# Changes for Linux Tux
#
# Revision 1.7  2001/09/24 17:43:39  jrumplik
# collect pkg info for multiple instance of pkgs
#
# Revision 1.5.4.1  2001/08/24 21:07:47  jrumplik
# mods for patch globl2.0.1.8
#
# Revision 1.5  2001/01/17 23:48:18  tneary
# Merged globl2x into head
#
# Revision 1.2.4.3  2000/05/18 20:31:33  jrumplik
# added changes from globl1.1.1.39
#
# Revision 1.2.4.2  2000/03/29 17:06:16  jrumplik
# log
#
# MOD	DATE		WHO	WHAT
# ---	----		---	----
#...........update comments prior to rel5.3 removed..........................
#...........refer to prior vpsrev comments to see them......................
# 13    10-jul-97       jar     add recognition of rel5.3.0 dir structure
#				moved funtions back into script since they are
#				executed only once now.
# 14    25-sep-97       jar     list patches even if they don't start with 5
# 15	30-sep-97	jar	change name to perirev and update for oscar
#				diskless component listing

#=============================================================================

#=============================================================================
# start (main) of script
#=============================================================================

case $1 in
	-l) 	long=yes
		;;
esac

if [ "${VPSHOME:-undefined}" = undefined ]; then
        if [ -f /etc/vpshome ]; then
                VPSHOME=`cat /etc/vpshome`
        else
                VPSHOME=`ckpath -a -y -d /opt/vps -p "VPS home directory"` || \
                        exit $?
        fi
fi

# force BASEDIR to be /opt/vps
BASEDIR=/opt/vps
SysType=`uname -s`

node=`uname -n`

cd $VPSHOME
/bin/echo "======================================================================="
/bin/echo "-----------------------------------------------------------------------"
uname -a
/bin/echo "-----------------------------------------------------------------------"
/bin/echo "Hostname: $node"
if [ "$SysType" = "Linux" ]; then
	if [ -f "/opt/vps/PERIbcmivr/BCM_IVR" ]; then
		cat /opt/vps/PERIbcmivr/BCM_IVR
		components=`awk '{ if ($1 >= 1 && ( $2 == "-" || $2 == "'$node'" ) && $3 != "" ) { print( $3$1 ) } }' "$VPSHOME/common/etc/vpshosts" | sort`
		if [ -f ${VPSHOME}/common/etc/vpshosts ]; then
			cmphere=`cat ${VPSHOME}/common/etc/vpshosts | egrep -v "#" | egrep "\-|$node" | cut -f1`
			# list components
			if [ "${components:-nocmp}" != nocmp ]; then
        			/bin/echo "        Components:"
				for c in $components; do
	        			echo "          $c"
				done
			fi
		fi
	fi
	exit 0
fi
/bin/echo "Linked packages:"
echo "" 

# check for pre-rel5.3 common dir format
# list PERIvps release
#
if [ -h common/rbin ]; then
	link=`/bin/ls -ld common/rbin | sed "s/.*v5\(.*\)\/bin/v5\1/"`
        /bin/echo "\t   Common Release: $link"
else
	if [ -h ${BASEDIR}/PERIvps ]; then
		# extract vps release, sedding till /vps is reached, then using vps to eol.
		link=`/bin/ls -ld ${BASEDIR}/PERIvps | sed "s/.*\/vps\(.*\)/vps\1/"`
        	/bin/echo "   PERIvps Release: $link"
	fi
	if [ -h ${BASEDIR}/PERImps ]; then
	        link=`/bin/ls -al ${BASEDIR}/PERImps | awk ' { print $11 } '`
	        pkg_rel=`basename $link`
        	/bin/echo "   PERImps Release: $pkg_rel"
	fi
fi

#
# if vpshosts was created and no components are listed on this node
# if so, then this is a periview workstation
#
# list components info
components=`awk '{ if ($1 >= 1 && ( $2 == "-" || $2 == "'$node'" ) && $3 != "" ) { print( $3$1 ) } }' "$VPSHOME/common/etc/vpshosts" | sort`
if [ -f ${VPSHOME}/common/etc/vpshosts ]; then
	cmphere=`cat ${VPSHOME}/common/etc/vpshosts | egrep -v "#" | egrep "\-|$node" | cut -f1`
	if [ "$cmphere" = "" ]; then
	        vpschk=`ls ${VPSHOME} | grep release.vps`
	        if [ "$vpschk" = "" ]; then
	                /bin/echo "     PERIVIEW Workstation"
	        fi
	fi
fi


# list pre-rel5.3.0 structure for old pkgs , if they still exists....
#
for vpslist in $cmphere; do
if [ -d vps$vpslist -a -h release.vps$vpslist ]; then 
	link=`/bin/ls -ld release.vps$vpslist | sed "s/.*v5\(.*\)/v5\1/"`
                vpsnum=`expr vps$vpslist : "vps\(.*\)"`
                /bin/echo "     VPS Number: $vpsnum\t\t\tRelease: $link"
        fi
done
#	peripro
if [ -h peri ]; then
	link=`/bin/ls -ld peri | sed "s/.*v5\(.*\)\/peri/v5\1/"`
	/bin/echo "        PeriPro\t\t\t\tRelease: $link"
fi
#	perioscar
for rel in `/bin/ls | grep "v5."`
        do
          if [ -d ${VPSHOME}/${rel}/oscar ]; then
          /bin/echo "        PeriOscar\t\t\tRelease: $rel"
          fi
done


#
# check for links to other installed distributions and list them
#
genericpkgs=`ls -d ${BASEDIR}/*`
bunchoflinksTOLIST=`ls -ald ${BASEDIR}/* | egrep -v 'PERIvps|PERImps'`
#echo "bunchoflinksTOLIST is $bunchoflinksTOLIST"
for lk in $genericpkgs; do
	if [ -h "$lk" ]; then
	p=`basename $lk`
	multiInst=`ls -d /var/sadm/pkg/$p*`
	for lks in $multiInst; do
	pname=`basename $lks`
	if [ -f "/var/sadm/pkg/$pname/pkginfo" ]; then
		isthisours=`egrep 'Nortel|NORTEL|Periphonics' /var/sadm/pkg/$pname/pkginfo`
		if [ "${isthisours:-no}" != "no" ]; then
			installedpkgs="$installedpkgs $pname"
			pkg_rel=`grep "^VERSION" /var/sadm/pkg/$pname/pkginfo | sed 's/VERSION=//'`
			#echo "pkg_rel is $pkg_rel"
			# if the version is in the linked pkgs, list it
			pkgrelLINKED=`echo $bunchoflinksTOLIST | grep $p | grep $pkg_rel`
			#echo "pkgrelLINKED is $pkgrelLINKED"
			if [ "$pkgrelLINKED" != "" ]; then
				/bin/echo "        ${p} Release: $pkg_rel"
			fi
		fi
	fi
	done
	fi
done

/bin/echo ""

# list components
if [ "${components:-nocmp}" != nocmp ]; then
        /bin/echo "        Components:"
	for c in $components; do
	        echo "          $c"
	done
fi

/bin/echo ""
/bin/echo "======================================================================="
/bin/echo "Installed Nortel Packages"
/bin/echo "-----------------------------------------------------------------------"
for package in $installedpkgs; do
# do pkginfo -l if -l option was on vpsrev command line
if [ "${long:-notspec}" != notspec ]; then
	pkginfo -l "${package}"  2> /dev/null
fi
	if [ -f /var/sadm/pkg/${package}/pkginfo ]; then
	# determine version & reldir of other installed pkgs
	link=`cat /var/sadm/pkg/${package}/pkginfo | grep "^VERSION" | cut -d= -f2`
	if [ "${package}" = "PERIstats" ]; then
	        RELDIR=`cat /var/sadm/pkg/${package}/pkginfo | grep "^PROBASEDIR" | cut -d= -f2`
        else
		RELDIR=`cat /var/sadm/pkg/${package}/pkginfo | grep "^RELDIR" | cut  -d= -f2`
	fi
	/bin/echo "$package \tRelease: $link"
	package=`echo $package | cut -f1 -d. | cut -c5-`
	# display patches
	# determine patch dir and
	#  if patches were installed for this version
	if [ -d ${RELDIR}/patches ]; then
		cd ${RELDIR}/patches
		patches=`/bin/ls -d * 2> /dev/null | egrep -v 'Un|Obs'`
		#don't process patchinfo if pre v5.2.1
		if [ "$package" = vps ]; then
			link1=`echo $link | cut -c4`
			link2=`echo $link | cut -c6`
		else 
			#force other pkgs to seem like 5.2.1 or above patchinfo
			link1="2"
			link2="1"
		fi
		if [ "$link1" -ge 2 -a "$link2" -ge 1 ]; then 
		patchlst=$patches
		patches=""
	        for p in $patchlst; do
			# choose patches from this package only
			ppkg=`grep pkg= ${RELDIR}/patches/${p}/patchinfo | awk -F"=" '{ print $2 }'`
			if [ "$ppkg" = "$package" -o "$ppkg" = "PERI$package" ]; then
				patches="$patches $p"
				pdate=`grep Date= ${RELDIR}/patches/${p}/patchinfo | awk -F"=" '{ print $2 }'`
				if [ "$pdate" = "" ]; then
					patches="$patches(pre-rel)"
				else
					patches="$patches($pdate)"
				fi
			fi
		done
		patches=`echo $patches | tr " " "\012" | sort -t\( +1`
		#list the patches, if any were found
		fi
		if [ "$patches" != "" ]; then
			if [ "$link1" -ge 2 -a "$link2" -ge 1 ]; then 
			/bin/echo "\t\tPatches:"
				for p in $patches; do
					pnum=`echo $p | cut -d\( -f1`
					pdt=`echo $p | cut -d\( -f2`
					/bin/echo "\t\t\t\t\t$pnum ($pdt"
				done
			else
			if [ "$package" = vps ]; then
			/bin/echo "\t\tPatches:\t\t\c"
	                /bin/echo "$patches" |  tr "\012" ":" | tr " " ":" | awk '
				{
				np = split($0, patch, ":");
				for (i = 1; i <= np; i += 2)
					{
					print patch[i], "  ", patch[i + 1];
	                                printf "\t\t\t\t\t"
					}
				}'
			/bin/echo ""
			fi
			fi
		fi
	fi

        fi
done
