#!/bin/ksh

Prog=${0##*/}

#
# Options
#
integer ShipList=0

ProdFile=/afs/austin/aix/ptf.images/320/prod/ptfinfo
ShipFile=/afs/austin/aix/ptf.images/320/ship/shipped_ptfs

typeset -u srch

#
# Outputs info about an APAR or PTF in product test
#
function ProdPTF {
	typeset comp j
	typeset -l obj
	typeset -u ptf apar defect build srch

	srch=$1
	while read ptf comp apar defect build prod j; do
		if [[ $srch = $ptf || $srch = $apar ]]; then
			echo "\nPTF\tAPAR\tDEFECT\tBUILD\tPROD"
			echo "$ptf\t$apar\t$defect\t$build\t$prod"
		fi
	done < $ProdFile
}

#
# Outputs info about an APAR or PTF shipped to ISMD
#
function ShipPTF {
	typeset -u apar apars ptf srch

	srch=$1
	echo "\nPTF\tAPAR"
	grep -v "^#" $ShipFile | grep -i $srch |\
	while read ptf apars; do
		echo "$ptf\c"
		for apar in $apars; do
			echo "\t$apar"
		done
	done

}

#
# Outputs PTF list for an APAR shipped to ISMD
#
function ShipList {
	typeset -u apar apars ptf srch

	srch=$1
	grep -v "^#" $ShipFile | grep -i $srch |\
	while read ptf apars; do
		if [[ ${srch#U4} = $srch ]]; then
			echo "$ptf"
		else
			for apar in $apars; do
				echo "$apar"
			done
		fi
	done

}

#
# Output some usage
#
function Usage {
	cat <<!

Usage: $Prog [-p] APAR|PTF

	-l = output only PTF/APAR list
!
	exit 1
}


##########
#  MAIN  #
##########
#
#  Get the parameters
#
set -- `getopt "l?" $*`
if (( $? != 0 )); then
	Usage
fi

while (( $# > 0 )); do
	case $1 in
		-l)
			ShipList=1
			shift
		;;

		--)
			shift
			break
		;;

		-?)
			Usage
		;;
	esac
done
if (( $# == 0 )); then
	Usage
fi

for srch in $*; do
	if (( $(grep -ic $srch $ShipFile) > 0 )); then
		if (( ShipList > 0 )); then
			ShipList $srch
		else
			echo "$srch has been shipped to ISMD"
			ShipPTF $srch
		fi
	else
		if (( $(grep -ic $srch $ProdFile) > 0 )); then
			echo "$srch is in product test"
			ProdPTF $srch
		else
			echo "$srch was not found"
		fi
	fi
done
