#!/bin/csh -f

rm Makefile
set cfound = 0
set clibs = ""
if ($#argv > 0) then
  set cfound = 1
  set cc = $argv[1]
  set cflags = "$argv[2-]"
endif

echo Building the Makefile for koth.
echo " "
echo Looking for an Ansi C compiler

cat <<EOF >ansi_test.c
main(int argc, char *argv[])  {
	printf("Hello," " world");
	exit(0);
}
EOF

if (! $cfound) then
  # Find your own.
  echo '  Trying for "gcc"...'
  set cc = gcc
  set cflags = "-O2"
  ${cc} ${cflags} ansi_test.c -c -o ansi_test >& /dev/null
  if ($status == 0) then
    echo "  gcc found."
    set cfound = 1
  else
    echo '  gcc not found.  Trying for "cc"...'
    set cc = "cc"
    set cflags = "-O"
    if (`uname` == "HP/UX") then
      set cflags = ($cflags "-Aa -D_HPUX_SOURCE")
      set clibs = ($clibs "-lBSD")
    endif
  endif
endif

${cc} ${cflags} ansi_test.c -c -o ansi_test >& /dev/null
if ($status == 0) then
  echo ' ' \"$cc\" is ANSI C.
else
  echo ' ' \"$cc\" is either not found or is not Ansi C.  To compile koth
  echo "    you must have an ANSI C compiler.  Please see the README file"
  echo "    for directions on how to specify the location of an Ansi C"
  echo "    compiler."
  exit 1
endif

echo " "
echo Looking for a lexer generator.
echo '  Trying for "flex"...'
set lex = flex
set lexfound = `which $lex`
if ($#lexfound > 1) then
  set cflags = ($cflags "-DLEX")
  echo '  Trying for "lex"...'
  set lex = lex
  set lexfound = `which $lex`
  if ($#lexfound > 1) then
    echo "  No lexer found.  To compile koth you must have a lexer."
    exit 1
  else
    set clibs = ($clibs "-ll")
  endif
else
  set cflags = ($cflags "-DFLEX")
endif
echo "  $lex found."

if ($lex == "flex") then
  set lex = "flex -i"
endif

echo " "
echo Looking for a parser generator.
echo '  Trying for "bison"...'
set parse = bison
set pfound = `which $parse`
if ($#pfound > 1) then
  set cflags = ($cflags "-DYACC")
  echo '  Trying for "yacc"...'
  set parse = yacc
  set pfound = `which $parse`
  if ($#pfound > 1) then
    echo "  No parser found.  To compile koth you must have a parser."
    exit 1
  endif
else
  set cflags = ($cflags "-DBISON")
endif
echo "  $parse found."

echo " "
echo "Looking for X11."
${cc} ${cflags} ansi_test.c -o ansi_test -lX11 >& /dev/null
if ($status == 0) then
  set cflags = ($cflags "-DX11_DISP")
  echo "  X11 found."
  set clibs = ($clibs "-lX11" "-lm")
else
  set cflags = ($cflags "-DNO_DISP")
  echo "  X11 not found.  No display code will be built."
endif

rm ansi_test.c ansi_test

cat <<EOF >Makefile

# This makefile was built using the mkbuild script in this directory
# Run this script instead of using Make on a new platform
# The only thing you may have to change is to add -DNOTANSI
# if strstr is not defined (ie. vaxen and RTS
# -- gsstark


# This program uses Flex, Bison, and Gnu CC to build the assembler.
# All are available free of charge from the Free Software Foundation.
# Get them by anonymous FTP to prep.ai.mit.edu in the directories
#   /pub/gnu/...
CC = $cc
CFLAGS = $cflags

PROGNAME = koth

OBJS = cwmain.o asm_parse.o sim.o clparse.o disp.o di_timer.o di_recalc.o  \\
       di_draw.o di_input.o ldaddr.o di_load.o siminstr.o asm_lex.o gbut.o \\
       di_dis.o di_but.o

SOURCES = corewar.h sim.c clparse.c asm_lex.flex asm_parse.y clparse.h     \\
          ldaddr.c di_recalc.c di_draw.c cwdisp.h di_load.c build_siminstr \\
          sim.h asm.h koth.1 README mkbuild disp.c cwmain.c di_timer.c     \\
          di_input.c di_dis.c gbut.c di_but.c gbut.h

\$(PROGNAME):	\$(OBJS)
	\$(CC) -o \$(PROGNAME) \$(CFLAGS) \$(OBJS) ${clibs}

\$(PROGNAME).shar:	\${SOURCES}
	shar \${SOURCES} > \$(PROGNAME).shar

asm_lex.c:	asm_lex.flex
	${lex} asm_lex.flex
	mv lex.yy.c asm_lex.c

EOF

if ($parse == "bison") then
  cat <<EOF >>Makefile
asm_parse.h asm_parse.c:	asm_parse.y
	bison asm_parse.y -d -o asm_parse.c

EOF
else
  cat <<EOF >>Makefile
asm_parse.h asm_parse.c:	asm_parse.y
	yacc -d asm_parse.y
	mv y.tab.h asm_parse.h
	mv y.tab.c asm_parse.c

EOF
endif

cat <<EOF >>Makefile
siminstr.c:	build_siminstr sim.c
	@echo Building siminstr.c: This may take a long time.
	build_siminstr > siminstr.c

.c.o:
	\$(CC) -c \$(CFLAGS) \$<

cwmain.o:	corewar.h clparse.h
asm_parse.o:	corewar.h asm.h
asm_lex.o:	corewar.h asm_parse.h asm.h
sim.o:	corewar.h sim.h
clparse.o:	corewar.h clparse.h
disp.o:	corewar.h cwdisp.h gbut.h
di_timer.o:	corewar.h cwdisp.h gbut.h
di_recalc.o:	corewar.h cwdisp.h gbut.h
di_draw.o:	corewar.h cwdisp.h gbut.h
di_input.o:	corewar.h cwdisp.h gbut.h
ldaddr.o:	corewar.h
di_load.o:	corewar.h cwdisp.h gbut.h
siminstr.o:	corewar.h sim.h
di_dis.o:	corewar.h cwdisp.h gbut.h
di_but.o:	corewar.h cwdisp.h gbut.h
gbut.o:	gbut.h

clean:
	rm -f \$(OBJS) asm_parse.c asm_lex.c \$(PROGNAME).shar asm_parse.h \\
		siminstr.c
EOF

echo 'Makefile built.  Running make.'

make clean >& /dev/null
make
