#
# Useful ./configure options with respect to this Makefile:
#
# --prefix=PREFIX
# --exec_prefix=EXEC_PREFIX
# --bindir=BINDIR
# --mandir=MANDIR
# --with-sharedir=SHAREDIR

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
datarootdir = @datarootdir@
sharedir = @sharedir@
mandir = @mandir@

CC = @CC@

CFLAGS :=@CFLAGS@ '-DROOTDIR="$(sharedir)"'
LDFLAGS :=@LDFLAGS@
LOADLIBES :=@LOADLIBES@

#
# End of configure section
#

OBJS = \
tworld.o series.o play.o encoding.o solution.o res.o lxlogic.o mslogic.o \
unslist.o messages.o help.o score.o random.o cmdline.o fileio.o err.o \
liboshw.a

RESOURCES = tworldres.o

#
# Binaries
#

tworld: $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LOADLIBES)

tworld.exe: $(OBJS) $(RESOURCES)
	$(CC) $(LDFLAGS) -o $@ $^ $(LOADLIBES)

#
# Object files
#

tworld.o   : tworld.c defs.h gen.h err.h fileio.h series.h res.h play.h \
             score.h solution.h messages.h help.h oshw.h cmdline.h ver.h
series.o   : series.c series.h defs.h gen.h err.h fileio.h solution.h \
             messages.h unslist.h
play.o     : play.c play.h defs.h gen.h err.h state.h oshw.h fileio.h \
             res.h logic.h encoding.h solution.h random.h
encoding.o : encoding.c encoding.h defs.h gen.h err.h state.h
solution.o : solution.c solution.h defs.h gen.h err.h fileio.h series.h
res.o      : res.c res.h defs.h gen.h err.h oshw.h fileio.h unslist.h
lxlogic.o  : lxlogic.c logic.h defs.h gen.h err.h state.h random.h
mslogic.o  : mslogic.c logic.h defs.h gen.h err.h state.h random.h
messages.o : messages.c messages.h defs.h gen.h err.h fileio.h
unslist.o  : unslist.c unslist.h gen.h err.h fileio.h res.h solution.h
help.o     : help.c help.h defs.h gen.h state.h oshw.h ver.h comptime.h
score.o    : score.c score.h defs.h gen.h err.h play.h
random.o   : random.c random.h defs.h gen.h
cmdline.o  : cmdline.c cmdline.h
fileio.o   : fileio.c fileio.h defs.h gen.h err.h
err.o      : err.c err.h gen.h oshw.h

#
# Generated files
#

comptime.h:
	echo \#define COMPILE_TIME \"`date '+%Y %b %e %T %Z'`\" > comptime.h

#
# Libraries
#

liboshw.a: oshw.h defs.h gen.h state.h err.h oshw/*.c oshw/*.h
	(cd oshw && $(MAKE))

#
# Resources
#

tworldres.o: tworld.ico
	echo SDL_app ICON $^ | windres -o $@

#
# Other
#

install: tworld
	mkdir -p $(bindir)
	mkdir -p $(sharedir)/sets
	mkdir -p $(sharedir)/data
	mkdir -p $(sharedir)/res
	mkdir -p $(mandir)/man6
	cp -i ./tworld $(bindir)/.
	cp -i sets/*.dac $(sharedir)/sets/.
	cp -i data/*.dat $(sharedir)/data/.
	cp -i res/rc $(sharedir)/res/.
	cp -i res/*.bmp $(sharedir)/res/.
	cp -i res/*.txt $(sharedir)/res/.
	cp -i res/*.wav $(sharedir)/res/.
	cp -i docs/tworld.6 $(mandir)/man6/.

all: tworld

clean:
	rm -f $(OBJS) tworld comptime.h config.*
	rm -f tworldres.o tworld.exe
	(cd oshw && $(MAKE) clean)

spotless:
	rm -f $(OBJS) tworld comptime.h config.* configure
	rm -f tworldres.o tworld.exe
	(cd oshw && $(MAKE) spotless)
	rm -f Makefile
