#!/bin/sh

#
# Amanda, The Advanced Maryland Automatic Network Disk Archiver
# Copyright (c) 1991 University of Maryland
# All Rights Reserved.
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of U.M. not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission.  U.M. makes no representations about the
# suitability of this software for any purpose.  It is provided "as is"
# without express or implied warranty.
#
# U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Author: James da Silva, Systems Design and Analysis Group
#			  Computer Science Department
#			  University of Maryland at College Park
#

# The point of this little script is to run cpp on Makefiles and shell 
# scripts, but without clobbering the target file if an error occurs.
# This prevents strange make problems caused by up-to-date, but truncated
# targets.

# Customize CPP to point to your system's C preprocessor.

# if cpp is on your path:
CPP=cpp

# if cpp is not on your path, try one of these:
# CPP=/lib/cpp			# traditional
# CPP=/usr/lib/cpp		# also traditional
# CPP=/usr/ccs/lib/cpp		# Solaris 2.x


# -------- you shouldn't have to modify anything below this line --------

CFLAGS="-I../config -I../common-src"

sh=0
while :
do
    case "$1" in
	-sh) sh=1 shift;;
	*)   break;
    esac
done

if [ $# -ne 2 ] ; then
    echo "Usage: $0 [-sh] input-file output-file"
    exit 1
fi

input=$1
output=$2

tmp=/tmp/munge.$$
trap 'rm -f $tmp; exit 1' 1 2 3 13 15

$CPP $CFLAGS $input >$tmp
if [ \( $? -ne 0 \) -o \( ! -s $tmp \) ] ; then
	echo $0: cpp failed
	rm -f $tmp
	exit 1
fi

if [ $sh = 1 ]; then
	echo '#!/bin/sh' >$output
	cat $tmp >>$output
	chmod +x $output
	rm $tmp
else
	mv $tmp $output
fi

exit 0
