#########################################################################
# Apple II emulator by Alexander Jean-Claude Bottema (C) 1994		#
#									#
# Makefile for Linux apple2 emulator					#
#									#
# $Id: Makefile,v 1.10 1997/06/29 22:49:31 chernabog Exp $		#
#									#
# MODIFICATION HISTORY							#
#   v0.3 by Aaron Culliney <chernabog@baldmountain.bbn.com>, Jan 1997.	#
#   v0.4 by Aaron Culliney <chernabog@baldmountain.bbn.com>, Jun 1997.  #
#									#
# ASSUMPTIONS:								#
#	GNU gcc, GNU make, GNU install					#
#									#
#########################################################################


default:	apple2
all:		depend default
install:	_install

#################
# main settings #
#################

CC =		gcc
CFLAGS =	-I$(VGADIR) -Wall $(DEBUGGER) $(JOYSTICK) $(DEBUG)
LDFLAGS =	-L$(VGADIR) $(CFLAGS)
LDLIBS =	$(VGALIB) $(LEXLIB)
PROGS =		apple2
SRCS =		$(apple2.c) $(apple2.S)
INSTALL =	install

# include the config file
ifeq (.config,$(wildcard .config))
include .config
endif

#####################
# graphics settings #
#####################

VGADIR =	/usr/local/include
VGALIB =	-lvga


###############################
# regenerate lex dependencies #
###############################
ifeq ($(DO_FLEX),y)
	FLEX_SUFFIXES = .l .c
	LEX = flex
else
	LEX =
endif

########################################
# apple2 debugger (defined in .config) #
########################################
ifeq ($(DO_DEBUGGER),y)
	DEBUGGER = -DDEBUGGER
#	DEBUGGER.l = debug.l
#	DEBUGGER.c = debugger.c $(DEBUGGER.l:.l=.c)
	DEBUGGER.c = debugger.c debug.c
endif

####################################
# pc joystick (defined in .config) #
####################################
ifeq ($(DO_PCJOYSTICK),y)
	JOYSTICK = -DPC_JOYSTICK
	JOYSTICK.c = joystick.c
endif

####################################
# program debugging / optimization #
####################################
# optimization causes debugger to choke
#DEBUG =	-DNDEBUG -O2
#DEBUG =	-ggdb
#DEBUG +=	-pg


#####################
# compilation rules #
#####################

.SUFFIXES:
.SUFFIXES:	.c .S .o $(FLEX_SUFFIXES)

%.o : %.c
		$(CC) $(CPPFLAGS) $(CFLAGS) -c $*.c
%.o : %.S
		$(CC) $(CPPFLAGS) $(CFLAGS) -c $*.S
ifeq ($(DO_FLEX),y)
%.c : %.l
		$(LEX) -i -P$* -o$*.c $*.l
endif

##################
# apple2 program #
##################

apple2.man =	apple2.6
#apple2.l =	prefs.l
#apple2.c =	$(apple2.l:.l=.c) disk.c keys.c interface.c misc.c
apple2.c =	prefs.c disk.c keys.c interface.c misc.c
apple2.S =	apple2.S diskio.S
apple2.o =	$(apple2.S:.S=.o) $(apple2.c:.c=.o) \
		$(DEBUGGER.c:.c=.o) $(JOYSTICK.c:.c=.o)

apple2:		$(apple2.o)
		$(CC) $(LDFLAGS) -o $@ $(apple2.o) $(LDLIBS)

#####################
# installation suid #
#####################
_install:
		$(INSTALL) --mode=4755 $(PROGS) $(BINDIR);
		$(INSTALL) --mode=0644 $(apple2.man) $(MANDIR)


#################
# other targets #
#################
.PHONY:		all clean realclean dep depend TAGS check remake

clean:
		$(RM) *.o core .depend Makefile.bak apple2
		$(RM) TAGS
		if [ -d checkdir ] ; then cd checkdir; $(MAKE) clean; fi

realclean:	clean
		$(RM) $(PROGS) .depend prefs.c debug.c
		$(RM) -r autogen/*
		if [ -d checkdir ] ; then cd checkdir; $(MAKE) realclean; fi

dep:		depend
depend:		$(SRCS)
		$(CC) -MM *.c *.S > .depend
		if [ -d checkdir ] ; then cd checkdir; $(MAKE) depend; fi

TAGS:
		etags -t *.[hcly]
		if [ -d checkdir ] ; then cd checkdir; $(MAKE) TAGS; fi

remake:		realclean TAGS depend all


########################
# include dependencies #
########################
ifeq (.depend, $(wildcard .depend))
include .depend
endif
