
# wpmerge
# usage:  wpmerge  [-c] <form file> <data file> <output file>
# 
# NOTE:  WPFORMAT will overwrite the output file.

# INITIALIZE VARIABLES
USAGE1="wpmerge"
USAGE2="Corel WordPerfect 8.1"
USAGE3="Copyright Corel Corp. 1999"
USAGE4=""
USAGE5="Usage: wpmerge [-c, -h, -help] <form_file> <data_file> <output_file>"
USAGE6="Where:"
USAGE7="    -c : to convert the data_file to WordPerfect format"
USAGE8="       before merging."
USAGE9="    -h, -help : This description. Type \"man wpmerge\" for more"
USAGEa="       detailed help."
USAGEb="Note: The output_file will be overwritten if it exists."

# This section is to process the command line arguments.
# TEST FOR HELP FLAG
if [ "$1" = "-h" -o "$1" = "-help" ]
then
		echo "$USAGE1"						# Just send usage statement
		echo "$USAGE2"
		echo "$USAGE3"
		echo "$USAGE4"
		echo "$USAGE5"
		echo "$USAGE6"
		echo "$USAGE7"
		echo "$USAGE8"
		echo "$USAGE9"
		echo "$USAGEa"
		echo "$USAGEb"
		exit
fi

# TEST FOR CONVERT FLAG
if test "$1" = "-c"
then
		CONVERT="True"
		shift
fi

# READ COMMANDLINE OPTIONS
if [ $# -lt 3 ]						# Must have all three commandline options
then
		echo "$USAGE1"						# Just send usage statement
		echo "$USAGE2"
		echo "$USAGE3"
		echo "$USAGE4"
		echo "$USAGE5"
		echo "$USAGE6"
		echo "$USAGE7"
		echo "$USAGE8"
		echo "$USAGE9"
		echo "$USAGEa"
		echo "$USAGEb"
		exit
fi

FORMFILE=$1
DATAFILE=$2
OUTPUTFILE=$3

# Find the default directory for wprint
WPPWD=`pwd`
if   [ "$0" = wpmergec ]; then
	for Dir in `echo $PATH | sed "y/:/ /"`; do		# part of path 
		if [ -f $Dir/wp -a -x $Dir/wp ]; then
			Work=$Dir/$0; break;
		fi;
	done;
elif [ `echo $0 | sed 's|/.*$|/|'` = "/" ]; then	# root path
	Work=$0;
else Work=`echo $WPPWD/$0 | sed 's|/\./|/|'`;		# relative path
fi;

while [ "`echo $Work | grep '\.\.'`" != "" ]; do	# simplify it
	Work=`echo $Work | sed 's|[^/]*/\.\./||'`; done;

while [ ! -f "$Work" ]; do						# ask if bad
	echo;
	echo $N "  Enter WordPerfect Installation Directory: $C"; read Work;
	Work=$Work/wpbin/wp;
done

DIRECTORY=`echo $Work | sed 's|/[^/]*$||' | sed 's|/[^/]*$||'`;

WPRINT=$DIRECTORY/shbin10/wprint		# Path to WPRINT
WPFORMAT=$DIRECTORY/shbin10/wpformat	# Path to WPFORMAT		
CVT=$DIRECTORY/shbin10/cvt				# Path to CVT
cd $WPPWD

if [ "$CONVERT" = "True" ]
then
	if [ -f $CVT ]
	then
		CVTOUT=/tmp/wpmerge_`date +%y%m%d_05/04/98M`
		$CVT $DATAFILE $CVTOUT wp60 -s -i=DTXT
			#NOTE: DTXT refers to "Delimited Text" type.
			#	   To change the delimiters, start WordPerfect (xwp)
			#	   and chose "Preferences" and then "Convert"
		ERROR=$?
		if [ $ERROR != 0 ]				# Did it convert OK?
		then
        	echo "wpmerge: $CVT failed"
			echo
			exit $ERROR
 		fi

		#Now use converted file
		DATAFILE=$CVTOUT

	else
		echo "wpmerge: $CVT not found"
		echo 
		exit 2
	fi
fi

if [ -f $WPRINT ]						# Make sure executable exists
then
	if [ -f $WPFORMAT ]					# Make sure executable exists
	then
		# Merge (-m) the files 
		# and wait (-w) until submitted to spooler before continuing
		$WPRINT -w -m  $FORMFILE $DATAFILE $OUTPUTFILE 
		rm -f $CVTOUT
	else
		rm -f $CVTOUT
		echo "wpmerge: $WPFORMAT not found"
		echo
		exit 1
	fi
else
	rm -f $CVTOUT
	echo "wpmerge: $WPRINT not found"
	echo
	exit 2
fi

echo

