#
#	ppwview image-file
#
#	View an image on a Procomm Plus for Windows 3.1 terminal to UNIX.
#
#	The image file may be any of the types of images that the
#	"imagecvt" program can read if the file is suffixed with
#	one of the recognized files types.  Otherwise, the image is
#	assumed to be a DigiFAX FAX file.
#

#
#
#
usage() {
	echo "Usage: ppwview [-w width-in-pixels] image-file"
}

#
#
#
help() {
	cat <<EOF

	Commands:
	n	Show next page
	p	Show previous page
	.	Re-show current page
	w <N>	Set Width to N pixels
	i	Invert image
	cw	Rotate image 90 degrees clockwise
	ccw	Rotate image 90 degrees clockwise
	<N>	Show page <N>
EOF
}

#
#	Download file $1 to Procomm
#
download() {
	echo "	\c"
	echo sz -y -b $1
	sz -y -b $1
}

#
#	Wait for page $1 to be written (completely).
#
wait_for_page() {
	_np=`expr $1 + 1`
	_waiting=true
	_warn=false
	while [ $_waiting = true ]
	do
		if [ -f "$tmpdir/page$_np.gif" ]; then
			return 0;
		elif [ -f "$tmpdir/DONE" ]; then
			if [ -f "$tmpdir/page$1.gif" ]; then
				return 0;
			else
				return 1;
			fi
		else
			if [ $_warn = false ]; then
				echo "	Please wait..." 
				_warn=true
			fi
			sleep 2
		fi
	done
}

#
#	Main script starts here
#
echo
echo "View FAX file '$@' with Procomm Plus for Windows"
echo
echo "	Set Procomm for Z-Modem protocol,"
echo "	and Z-Modem advanced option Crash Recovery (Receiver) to Negotiate."
echo "	Set System File Transfer option to View *.GIF files during download."
echo

#
#	Process arguments
#
set -- `getopt ?w: $*`
if [ $? != 0 ]; then usage; exit 2; fi

width=576
for i in $*
do
	case $i in
	-w)		width="$2"; shift; shift;;
	--)		shift; break;;
	-?)		usage; exit 0;;
	esac
done

if [ $# != 1 ]; then usage; exit 2; fi

image=$1
imageopt=
case "$image" in
*.fax|*.tif|*.gif|*.pcx|*.bmp|*.p[bgp]m)	# We know about these
	;;
*)						# No way to know file type
	imageopt="-i fax"
	;;
esac

#
#	Create temp directory, and arrange to remove it
#	when this shell is exited
#
logname=`logname`
tmpdir=/tmp/$logname$$
mkdir $tmpdir
trap "rm -rf $tmpdir; exit" 0 1 2 15
# log usage for now
echo "$logname" >> /tmp/.ppwview

#
#	Begin image conversion in background and create DONE file
#	to let foreground know when process has completely finished
#
(
	imagecvt -w $width -a $imageopt $image $tmpdir/page%d.gif
	touch $tmpdir/DONE
) 2>/dev/null &
ipid=$!

#
#	Loop, accepting commands
#
done=false
opage=-1
page=0
npage=1
lpage=0
while [ $done = false ]
do
	echo
	echo "Page $page: Type 'n' for next page, 'p' for previous page: \c"
	read ans parm extra
	case "$ans" in
	q)	# Quit
		done=true
		;;

	n)	# Next Page
		opage=$page
		page=$npage
		npage=`expr $npage + 1`
		waiting=true
		warn=false
		while [ $waiting = true ]
		do
			# If page+1 or DONE exists, page must be done
			if [ -f "$tmpdir/page$npage.gif" ]; then
				download $tmpdir/page$page.gif
				waiting=false
			elif [ -f "$tmpdir/DONE" ]; then
				if [ -f "$tmpdir/page$page.gif" ]; then
					download $tmpdir/page$page.gif
				else
					echo "	No more pages"
					npage=$page
					page=$opage
					opage=`expr $opage - 1`
					lpage=$page
				fi
				waiting=false
			else
				if [ $warn = false ]; then
					echo "	Please wait..." 
					warn=true
				fi
				sleep 2
			fi
		done
		;;

	p)	# Previous Page
		if [ $page -le 1 ]; then
			echo "	On first page"
		else
			npage=$page
			page=$opage
			opage=`expr $opage - 1`
			download $tmpdir/page$page.gif
		fi
		;;

	.)	# Re-show current page
		download $tmpdir/page$page.gif
		;;

	i)	# Invert image
		echo "Not implemented yet"
		;;

	cw)	# Rotate cw
		echo "Not implemented yet"
		;;

	ccw)	# Rotate ccw
		echo "Not implemented yet"
		;;

	w)	# Set image width
		echo "Not implemented yet"
		width="$parm"
		;;

	[1-9]*)	# Go to page N
		echo "Not implemented yet"
		;;

	*)	# Help
		help
		;;
	esac
done

#
#	Attempt to kill off background processes
#
kill $ipid 2>/dev/null
