#!/bin/sh
#ptfinstall -- is a cover function program which checks usages, finds an
#              appropariate installation version and calls it to install
#	       the valid specified ptfs.

PATH=:/bin:/usr/bin:/usr/ucb:/sys/support:.:
export PATH
cmd=`basename $0`

PTF_DIR=/sys/support/ptfdir
UUCP_DIR=/usr/spool/uucppublic
DEFAULTDIR=/sys/support/ptftest

# if $UUCP_DIR  doesn't exist, then we couldn't have received any ptfs
if [ ! -d $UUCP_DIR ]
then
	echo "$cmd: $UUCP_DIR directory does not exist" ; exit 1
fi

# check number of arguments
if [ $# -lt 1 ]
then
	echo "usage: $cmd [ -dinstall_path ] P<ptf#> . . ." ; exit 1
fi

# find out if the user has specified a directory in which to install the
# ptf.  If not, then install it into DEFAULTDIR.
case "$1" in	
-d[a-zA-Z0-9/#@_.]*)	
	if [ $# -lt 2 ]
	then
		echo "usage: $cmd [ -dinstall_path ] P<ptf#> . . ." ; exit 1
        else
		DESTDIR=`expr "$1" : '-d\(.*\)'`
	fi
	if [ ! -d $DESTDIR ]
	then
		echo "$DESTDIR -- directory does not exist."
		echo "Shall I create it(yn)? [Y]" 
		read response
		case "$response" in
		y | Y)  echo "Creating $DESTDIR . . ."
			mkdir $DESTDIR  
			if [ $? -eq 0 ]
			then
				chmod 755 $DESTDIR
			else
				echo "$cmd : unable to make $DESTDIR "
				exit 1
			fi
			;;
		*)	echo "$DESTDIR does not exist."
			echo "$cmd : Terminated" ; exit 1
			;;
		esac
	fi
	shift
	;;
-*) 	
	echo "$cmd: Invalid option $1"
	echo  "usage: $cmd [ -dinstall_path ] P<ptf#> . . ." 
	exit 1
	;;
[Pp]* | [0-9]*)	
	DESTDIR=${DEFAULTDIR}
	if [  ! -d $DESTDIR ]
	then
		mkdir $DESTDIR
		chmod 755 $DESTDIR
	fi
	;;
*)	
	echo "$cmd: Invalid argument $1"
	echo "usage: $cmd [ -dinstall_path ] P<ptf#> . . ." ; exit 1
	;;
esac

# make a list of the valid ptfs
for i
do
	case "$i" in
	[pP][0-9]*[sb]_[a-zA-Z]* | [0-9]*[sb]_[a-zA-Z]*)
		PTF=`expr $i : '[pP]*\([0-9][0-9]*[sb]_[a-zA-Z]*\)'`
		VALIDPTFS="$VALIDPTFS $PTF"
		;;
	[pP][0-9]*_[a-zA-Z]* | [0-9]*_[a-zA-Z]*)
		PORT=`expr "$i" : '[pP]*[0-9][0-9]*_\([a-zA-Z]*\)'`
		NUM=`expr "$i" : '[pP]*\([0-9][0-9]*\).*'`
		VALIDPTFS="$VALIDPTFS ${NUM}s_$PORT ${NUM}b_$PORT"
		;;
	[pP][0-9]* | [0-9]*) 	
		VALIDPTFS="$VALIDPTFS `expr "$i" : '[pP]*\([0-9][0-9]*\)'`"
		;;
	*) 
		echo "$cmd: $i ignored -- improper ptf number"
		;;
	esac
done

request="Send mail to ibmsupt\!ibmptf to request"
# call the appropriate version of ptfinstall for each valid ptf
for v in $VALIDPTFS
do
	PTFNUM=`expr "$v" : '\([0-9][0-9]*\).*'`
	PKG=ptf$PTFNUM
	if [ -r $PTF_DIR/$PKG ] 
	then
		cd $PTF_DIR
		if [ -d $PKG ] 
		then
			cd $PKG
			if [ -f ${PKG}.version ]
			then	
				ptfinstall.v`cat ${PKG}.version` -d$DESTDIR $v
			else 
				echo "$PKG was improperly transmitted."
	 			echo "$request $PKG."
				continue
			fi
		else
			ptfinstall.v1 -d$DESTDIR $PTFNUM
		fi
	else
		cd $UUCP_DIR
		if [ -f $PKG.version ]
		then 
			ptfinstall.v`cat ${PKG}.version` -d$DESTDIR $v
		else
			if [ -f $PKG.list ]
			then
				ptfinstall.v1 -d$DESTDIR $PTFNUM
			else
				echo "$PKG has not been received yet."
	 			echo "$request $PKG."
			fi
		fi
	fi
done
