#!/bin/sh
prog=CPP_FILE
SYNTAX="Syntax: $prog [-b] [sourcefile [destfile]]"
# $CPP_FLAGS is honored.

if [ x"$1" = x"-b" ]
then
	blanks="-e '/^[ 	]*$/d'"; shift
else
	blanks="-e '1,/[^ 	]/{
/^[ 	]*$/d
}'"
fi

case $# in
	2) rm -f $2.before
	   test -r $2 && mv $2 $2.before ;;
	0|1) ;;
	*) echo $SYNTAX >&2
	   exit 1 ;;
esac

machtype=`machtype`
case x$machtype in
	xi586|xi686|xDOS386|xDOSNET|xWIN386|xWIN32)
		source=${1+"< $1"}
		dest=${2+"> $2"}
		libcpp=cpp
		if [ ! -z "$MSVC" ]; then
			source=$1
			libcpp='cl -EP -DPOUND## '
		fi ;;
	xSUNOS5_SPARC)
		source=${1+"< $1"}
		dest=${2+"> $2"}
		libcpp=/usr/ccs/lib/cpp ;;
	xOSF1_DECALPHA)
		source=$1
		dest=${2+"> $2"}
		libcpp='cc -E' ;;
	xIBMRS6000|xHP700|xHP800|xIRIS4D)
		source=${1+"< $1"}
		dest=${2+"> $2"}
		libcpp='/lib/cpp ' ;;
	*)
		source=${1+"< $1"}
		dest=${2+"> $2"}
		libcpp='/lib/cpp -traditional' ;;
esac

dest="| Sperl "$SHOME"/cmd/removeBlankLinesBeforeBinShell.pl $dest"

if [ x"$machtype" = xNEXT ]; then
	# forget the NeXT CPP -- it's got problems with quotes, problems with
	# pound signs, etc .  Use some other machine's CPP instead.
	# Bad, but there's no good way around it.
	machinecpp="spud" export machinecpp
	myshomeinc=`echo ${SHOME}/include | sed -e "s:/private::" -e "s:/Net::"` export myshomeinc
	eval "(rsh ${machinecpp} /lib/cpp -Usun -Usparc -DNEXTM_CPP_FILE -P -I${myshomeinc} ${CPP_FLAGS} | sed -e '/^$/d' -e '/^	$/d' $blanks ) $source $dest"
else
# redirect stdin of subshell, not that of cpp, otherwise we exit 0 if infile 
# doesn't exist, strip out empty, single-tab (and blank?) lines:
   if [ x"$machtype" = xOSF1_DECALPHA ]; then
        eval "($libcpp -P -I${SHOME}/include $CPP_FLAGS $source | sed -e '/^$/d' -e '/^ $/d' $blanks ) $dest"
   elif [ x"$machtype" = xi586 ]; then
        eval "($libcpp -I'$($SHOME/slocal/win32/bin/dospath.pl $SHOME)/include' $CPP_FLAGS $source | nocrnl | sed -e '/^$/d' -e '/^ $/d' $blanks -e '/^	$/d') $dest"
   elif [ ! -z "$MSVC" ]; then
        eval "($libcpp -I${SHOME}/include $CPP_FLAGS $source | sed -e '/^$/d' -e '/^	$/d' $blanks ) $dest"
   else
	eval "($libcpp -P -I${SHOME}/include $CPP_FLAGS | sed -e '/^$/d' -e '/^	$/d' $blanks ) $source $dest"
   fi
fi
