#!/bin/csh
#
# (c) Copyright 1989, 1990, 1991, 1992 OPEN SOFTWARE FOUNDATION, INC.
# ALL RIGHTS RESERVED
#
#
# Motif Release 1.2
#

if ($#argv < 1) then  
Help:
	echo "usage: "$0" -DMOTIF1_1 ... -DMotif1_n <TestName>.dat ||"
	echo "usage: "$0" -DMOTIF1_1 ... -DMotif1_n <TestName>.scr"
	exit 1
endif

if ($argv[1] == "-help") then
	goto Help
endif

set extension = $argv[$#argv]:e

if ($extension == "dat") then
	set output_extension = ".Dat"
else
	if ($extension == "scr") then
		set output_extension = ".Scr"
	else
		echo "usage: "$0" -DMOTIF1_1 ... -DMotif1_n <TestName>.dat ||"
		echo "usage: "$0" -DMOTIF1_1 ... -DMotif1_n <TestName>.scr"
		exit 1
	endif
endif

set tmp_script = /tmp/$$.sed
set tmp_file = /tmp/$$.pp1
set tmp_file2 = /tmp/$$.pp2
set root = $argv[$#argv]:r

touch $tmp_script
chmod 777 $tmp_script

if (-e $root$output_extension) then
	rm $root$output_extension
endif

# create sed file to delete ifdef's and endif's corresponding to 
# the defines passed in from DEFINES.

foreach define ($argv[*])
	if ($define == "-DMOTIF1_1") then
		echo "/^#ifdef[	 ]*MOTIF1_1/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_1/d" >> $tmp_script
	endif
	if ($define == "-DMOTIF1_2") then
		echo "/^#ifdef[	 ]*MOTIF1_2/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_2/d" >> $tmp_script
	endif
	if ($define == "-DMOTIF1_3") then
		echo "/^#ifdef[	 ]*MOTIF1_3/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_3/d" >> $tmp_script
		echo "/^#ifdef[	 ]*MOTIF1_2/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_2/d" >> $tmp_script
	endif
	if ($define == "-DMOTIF1_4") then
		echo "/^#ifdef[	 ]*MOTIF1_4/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_4/d" >> $tmp_script
		echo "/^#ifdef[	 ]*MOTIF1_3/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_3/d" >> $tmp_script
		echo "/^#ifdef[	 ]*MOTIF1_2/d" >> $tmp_script
		echo "/^#endif[	 ]*MOTIF1_2/d" >> $tmp_script
	endif
end


if (-z $tmp_script) then
        cat $argv[$#argv] > $tmp_file
else
        sed -f $tmp_script $argv[$#argv] > $tmp_file
endif

# remove remaining #ifdef and #endif lines.

sed "/^#ifdef[	 ]*.*/,/^#endif[	 ]*.*/d" $tmp_file > $tmp_file2

# remove C Style comment lines from pp file.

sed -e "/\/\*.*\*\//d" \
    -e "/\/\*/,/\*\//d" $tmp_file2 > $root$output_extension

# clean up all temporary files.

rm -f $tmp_script $tmp_file $tmp_file2
