#! /bin/ksh
#
# Host Publisher Problem Determination Bundling Tool
#
# This tool is designed to gather pertinent files to assist IBM Service
# in resolving a problem found with Host Publisher.
#
# The tool takes two arguments, the Host Publisher Server installation
# directory and the WebSphere Application Server directory. Logs and
# application files are collected from these directories and temporarily
# placed in a subdirectory under the current directory. The files are gathered 
# and bundled into a JAR file for delivery back to IBM Service. The subdirectory is
# removed after the JAR file is created.
#
# Please refer to Host Publisher's support Web site: 
#
#
HPDIR=/var/HostPublisher
JAR=/usr/lpp/HostPublisher/common/jdk/bin/jar
CURRENTDIR=`pwd`
HPPDDIR=hppd
TEMPDIR=$CURRENTDIR/$HPPDDIR

if [ ! -d $HPDIR ]
then 
	echo "Host Publisher does not appear to be installed."
	echo
	exit 1
fi

if [ -d  /usr/WebSphere/AppServer ]
then
	# This must be AIX
	WASDIR=/usr/WebSphere/AppServer
	OSTYPE=aix
	HPLPPDIR=/usr/lpp/HostPublisher
elif [ -d  /usr/IBMWebAS ]
then
	# This must be AIX
	WASDIR=/usr/IBMWebAS
	OSTYPE=aix
	HPLPPDIR=/usr/lpp/HostPublisher
elif [ -d  /usr/lpp/IBMWebAS ]
then
	# This must be AIX
	WASDIR=/usr/lpp/IBMWebAS
	OSTYPE=aix
	HPLPPDIR=/usr/lpp/HostPublisher
elif [ -d /opt/IBMWebAS ]
then
	# This must be Solaris
	WASDIR=/opt/IBMWebAS
	OSTYPE=sol
	JAR=/usr/java/bin/jar
	HPLPPDIR=/opt/HostPublisher
elif [ -d /usr/lpp/WebSphere/AppServer/config ]
then
	# This must be MVS, we need to find the actual server root
	# Check to see if this is WAS 1.2
	# This is WAS 1.2, we need to find the actual server root in use; 
	# check the Web server's configuration file
	WASDIR=`grep "/usr/lpp/WebSphere/AppServer/lib/libadpter.so" /etc/httpd.conf | grep ServerInit | awk '{print $3}'`
	OSTYPE=mvs
	HPLPPDIR=/usr/lpp/HostPublisher
	JAR=jar
	if [ ! -d $WASDIR ]
	then
			echo "Could not locate WebSphere alternate server root directory."
			echo
			exit 1
	fi
elif [ -d /usr/lpp/WebSphere/AppServer ]
then
	# This is WAS 1.1, we know where the WebSphere directory is
	WASDIR=/usr/lpp/WebSphere/AppServer
	OSTYPE=mvs
	HPLPPDIR=/usr/lpp/HostPublisher
	JAR=jar
else
	echo WebSphere cannot be found on this system.
	echo
	exit 1
fi

# Setup exclusion list
echo HPAdmin >hppd.in
echo service_defaults >>hppd.in
echo adminservice >>hppd.in
echo httpservice >>hppd.in
echo xmlLegacyPortal.jar >>hppd.in
echo xmlLegacyGWS.jar >>hppd.in
echo RIOServlet.jar >>hppd.in
echo xmlLegacyGateway.application >>hppd.in
echo xmlLegacyPortal.application >>hppd.in
echo RIOServlet.application >>hppd.in
echo xmlLegacyGateway >>hppd.in
echo xmlLegacyPortal >>hppd.in

# Make temporary directory
if [ -d $TEMPDIR ] 
then
	echo Removing old temporary directory...
	rm -fr $TEMPDIR
fi
mkdir $TEMPDIR

echo Collecting...
# Gather HostPublisher application files
echo Host Publisher application files...
cp -r $HPDIR/Server/production/appmanifests $TEMPDIR 2>/dev/null
cp -r $HPDIR/Server/production/poolspecs $TEMPDIR 2>/dev/null
cp -r $HPDIR/Server/production/beans $TEMPDIR 2>/dev/null
cp -r $HPDIR/Server/production/documents $TEMPDIR 2>/dev/null
cp $HPDIR/Server/production/documents/xmlLegacyPortal/hPubPortalData.xml $TEMPDIR 2>/dev/null

# Gather HostPublisher logs
echo Host Publisher logs...
cp -r $HPDIR/log $TEMPDIR 
cp $HPDIR/Server/server.properties $TEMPDIR

# Gather WebSphere logs and files, depending on level of WebSphere. Look for the "hosts"
# directory under AppServer for V3
if [ -d $WASDIR/hosts ] 
then
	# this is WAS V3
	# We can't get the active configuration from a batch file, so the best we can do is
	# grab the configuration XML file under the Host Publisher directory. Log files are all
	# under HostPub/log, which we've already grabbed.
	echo WebSphere configuration files...
	cp $HPLPPDIR/HostPubWASConfig.xml $TEMPDIR
	mkdir $TEMPDIR/WAS
	cp $WASDIR/bin/admin.config $TEMPDIR/WAS/

	# Rewrite hostPubWAS.sh to perform an export of WAS configuration
	cd $HPLPPDIR
	sed 's/-import/-export/g' HostPubWAS.sh > HostPubWASexp.sh
	chmod +x HostPubWASexp.sh
	./HostPubWASexp.sh $TEMPDIR/WAS/xmlConfig.out > /dev/null
	cd - > /dev/null
else
	# this is WAS V2 (or WAS 1.x)
	echo WebSphere logs...
	mkdir $TEMPDIR/WAS
	cp -r $WASDIR/logs $TEMPDIR/WAS
	echo WebSphere properties files...
	cp -r $WASDIR/properties $TEMPDIR/WAS
fi

# now delete unwanted files
cd $TEMPDIR
for i in `cat $CURRENTDIR/hppd.in`
do
	find . -name $i -exec rm -r {} ";" 2> /dev/null
done

cd $CURRENTDIR
rm hppd.in
echo done.
echo Creating JAR file...
# Create PD jar file of the results
cd $CURRENTDIR
$JAR cMf $HPDIR/hppd.$OSTYPE.jar $HPPDDIR/*
rm -fr $TEMPDIR
echo done.
echo The hppd.$OSTYPE.jar file has been created in $HPDIR. Please send this
echo file to IBM Service when requested.
exit 0
