#!/bin/sh

# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.


for i
do 
case $i in
-U*) undefine=`echo $i | sed 's/-U//'`
	;;
*)
nawk '
BEGIN {
	inif = 0;
	noprint = 0;
	egrep = "![ ]*defined.";
	pattern = sprintf("%s%s",egrep,undefine);
}

#  Find the proper ifdefs
/^#if/ {
	if (index($0,undefine)) {
#		Handle #ifndef and ! defined()
		if ((index($1,"#ifndef")) || (match($0,pattern))) {
			inif = 1;
			next;
		} else {
			inif = 1;
			noprint = 1;
			next;
		}
	} else if (inif) {
		inif++;
	}
}

/^#else/ {
	if (inif == 1) {
		if (noprint)
			noprint = 0;
		else
			noprint = 1;
		next;
	}
}

/^#endif/ {
	if (inif == 1) {
		inif--;
		noprint = 0;
		next;
	} else if (inif) {
		inif--;
	}
}

# go to the next.
{ 	
	if (! noprint) print $0;
	next;	
}
' undefine=$undefine < $i
;;
esac
done
