#!/bin/sh

# This program will configure the makefile for various OSes.
#
# Config version 1.2
SYS=

# Heading

cat > Makefile <<EoF
# Makefile for HEYU, a program to control an X10 CM11A computer interface.
# This makefile is generated by the Configure program.
#
EoF

# paths:
cat >> Makefile <<EoF
BIN = /usr/local/bin
MAN = /usr/local/man/man1
MAN5 = /usr/local/man/man5
GROUP = sys
OWNER = root

#       set DFLAGS equal to:
#          -DSYSV       if using SYSTEM V
#          -DVOID       if compiler doesn't understand 'void'

EoF

while [ $# -ge 1 ] ; do
    case "$1" in
	help|-h|-help)
	    echo "Usage: $0 [ system type ]"
	    echo " Valid system types are aix, freebsd, generic, linux, netbsd,"
	    echo " openbsd, osf, sco, sunos and sysv"
	    echo "A guess will be made if no arguements are given."
	    rm Makefile 
	    exit
	    ;;
	*)
	    SYS="$1"
	    ;;
    esac
    shift
done

cat <<EoF
This script will create a valid Makefile based on the results of the uname(1) 
output.  Please stand-by a moment.
EoF


if [ "$SYS" = "" ] ; then
    SYS=`uname -s | tr '[A-Z]' '[a-z]'`
fi

CC=gcc	#Set default for later

echo "#This makefile is built for $SYS" >> Makefile
case "$SYS" in
    linux)
        cat >> Makefile <<-EoF
	CC = gcc
	CFLAGS = -g -O \$(DFLAGS) -Wall
	DFLAGS = -DSYSV -DPOSIX -DHAS_ITIMER -DLINUX -DHASSELECT
	LIBS = -lm -lc
EoF
	;;
    sunos*|solaris)
    	type gcc > /dev/null
    	if [ $? = 0 ] ; then
    	    echo "CC = gcc" >> Makefile
    	    CC=gcc
	else
	    echo "CC = cc" >> Makefile
	    CC=cc
	fi
	cat >> Makefile <<-EoF
		DFLAGS = -DSYSV -DPOSIX -DSOLARIS -DLOCKDIR=\"/var/spool/locks\" -DHASSELECT
		CFLAGS = -g -O \$(DFLAGS) -Wall
		LIBS = -lm -lc
EoF
	;;
    freebsd|openbsd)
	cat >> Makefile <<-EoF
		CC = gcc
		CFLAGS = -g -O \$(DFLAGS) -Wall
		LIBS = -lm -lc
		DFLAGS= -DHASSELECT -DNEEDGTIME -DFREEBSD
EoF
	;;
    sco*)
	cat >> Makefile <<-EoF
		CC = cc
		MAN = /usr/local/man/man.1
		MAN5 = /usr/local/man/man.5
		CFLAGS = -O \$(DFLAGS)
		LIBS = -lm -lc -lsocket
		DFLAGS= -DSYSV -DHASSELECT -DSCO
EoF
	CC=cc
	;;
    generic)
    	cat >> Makefile <<-EoF
		CC = gcc
		CFLAGS = -g -O \$(DFLAGS) -Wall
		LIBS = -lm -lc
EoF
	;;
    aix|sysv)
	cat >> Makefile <<-EoF
		CC = gcc
		CFLAGS = -g -O \$(DFLAGS) -Wall
		LIBS = -lm -lc
		DFLAGS = -DSYSV -DPOSIX 
EoF
	;;
    netbsd)
	cat >> Makefile <<-EoF
		DFLAGS = -DPOSIX -DLOCKDIR=\"/var/spool/lock\"
		CC = gcc
		CFLAGS = -g -O \$(DFLAGS) -Wall
		LIBS = -lm -lc
EoF
	;;
    nextstep)
	cat >> Makefile <<-EoF
		CC = gcc
		DFLAGS =  -DPOSIX
		CFLAGS = -g \$(DFLAGS) -posix
		LDFLAGS = -posix
		LIBS = -lm -lposix

EoF
	;;
    osf)
	cat >> Makefile <<-EoF
		GROUP = uucp
		CC = gcc
		CFLAGS = -g \$(DFLAGS)
		LIBS = -lm -lc
		DFLAGS = -DSYSV -DHAS_ITIMER -DOSF1 -DLOCKDIR=\"/var/spool/locks\"
EoF
	;;
    *)
    	echo "Your system type was not understood.  Please try one of "
    	echo "the following".
    	$0 -help
    	exit
    	;;
esac

echo "checking for siginterrupt()"

rm -f testsigi.c testsigi
cat > testsigi.c <<EoF
#include <signal.h>
main()
{
   int x;
   x=siginterrupt(SIGALRM,1);
}
EoF
$CC -o testsigi testsigi.c  >/dev/null 2>&1
if [ $? = 0 ] ; then
    echo "#define USESIGINT 1" >local.h
    echo "Using siginterrupt()"
else
    echo "Not using siginterrupt()"
fi
rm -f testsigi.c testsigi
if [ ! -f local.h ] ; then
    touch local.h
fi

cat >> Makefile <<EoF
# # The rest of the makefile should need no changes

EoF

cat Makefile.in >> Makefile

echo ""
echo "The Makefile has been created for $SYS."
echo ""
