#!/bin/bash

# This bit of nonsense fixes up posix_time.h to either define
# sturct itimerspec or not, depending on if the library headers
# have already defined it.  It does a test by compiling a trivial
# program and testing if there are errors.  If there are errors
# it inverts the inclusion of the definition.  That is it does not
# matter if posix_time.h starts with the struc defined or not, if
# the assumption is wrong we just change it.

# The interesting thing is that even if the lib does not define it
# the definition is still there, it is just not exposed because of
# some dumb rules having to do with what flags are defined. 

FILE="posix_time.h"
OUTFILE="tmp"

tf="t--st"
        rm -f $tf.c
	echo "#include <linux/unistd.h>" >$tf.c
	echo "#include \"$FILE\"">>$tf.c
	echo "int main(struct itimerspec *t){t->it_interval.tv_sec++;return 0;}">>$tf.c
        foo=`$* $tf.c -o $tf`&>/dev/null
	result="$?"
#	echo $result
#       echo "from file"$foo
	if [ "$result" != "0" ] ; then 
	    awk '$0 == "#ifdef NEED_ITIMERSPEC" {$0 = "#ifndef NEED_ITIMERSPEC";print;next} $0 == "#ifndef NEED_ITIMERSPEC" {$0="#ifdef NEED_ITIMERSPEC"}{print}' $FILE >$OUTFILE
	    rm $FILE
	    mv $OUTFILE $FILE
	 fi
         rm -f $tf*
