#!/bin/sh
#
# strip_bw - 27/5/93 - dietmar theobald
#
# strip_bw <file>...
#
# Expects a number of XF generated files as input and removes definitions for
# the fore-/background colors black/white from them.
#
# Additionally, all '-state {active}' options are removed.
#
tmpfile=/tmp/strip_bw
   self=strip_bw
 status=0
whitesp='[ 	][ 	]*'

for f
do
   [ -w "$f" ] || { echo >&2 "*** $self: $f not writeable"; status=1; break; }
   [ -d "$f" ] && continue

   cat "$f"					\
   | sed -e "/^$whitesp-....ground {black}.$/d"	\
         -e "/^$whitesp-....ground {white}.$/d"	\
         -e "/^$whitesp-....ground {black}$/d"	\
         -e "/^$whitesp-....ground {white}$/d"	\
         -e "s|$whitesp-....ground {black}||g"	\
         -e "s|$whitesp-....ground {white}||g"	\
	 -e "s|$whitesp-state {active}||g"	\
   | awk 'BEGIN  { last_is_buffered = 0
		 }
	  /^ *[a-z]+ +\.[.a-zA-Z0-9_]+ \\$/ { 
		   buffered_line    = "  " $1 " " $2
		   last_is_buffered = 1
		   next
		 }
	  /^ *-[a-z]+ {.*}\\$/ {
		   if (last_is_buffered)
		      print buffered_line " \\"

		   buffered_line    = substr ($0, 1, length($0)-1)
		   last_is_buffered = 1
		   next
		 }
		 { attach = ""
		 }
	  /^ *-/ { attach = " \\"
		 }
		 { if (last_is_buffered)
		   {  printf ("%s%s\n", buffered_line, attach)
		      last_is_buffered = 0
		   }
		   print
		 }
	  END    { if (last_is_buffered)
		      print buffered_line " \\"
		 }'				\
   > $tmpfile

   cmp -s "$f" $tmpfile || {
      cp $tmpfile "$f"
      echo "$self: modified $f"
   }
done

rm -rf $tmpfile
exit $status

