#
# Makefile for cthugha-L 
# source directory
#
##############################################################################
# You should not need to change anything in this file
#
# Use the file ../configure to set your preferences.
##############################################################################

include ../configure

#
# source-files for the different versions of cthugha.
#

TARGETS = ../tabheader ../tabinfo

# source files used by all programs
GEN_SRC = info_title_usage.c  server.c	snd-net.c snd-play.c		\
	sound.c network.c snd-random.c	cthugha.c options_ini.c		\
	interface-cd.c	imath.c snd-exec.c credits.c

# source files ued by cthugha and xcthugha 
DISP_SRC = ${GEN_SRC} translate.c sound_tables.c action.c flames.c	\
	display_pcx.c information.c waves.c				\
	sound_process.c display.c display_text.c			\
	display_palettes.c ini_write.c feature.c			\
	interface.c opt-load.c						\
	interface-main.c interface-sel.c interface-opt.c

# source files used by cthugha (svgalib version)
ifeq ($(USE_SVGA),1)
SVGA_SRC = ${DISP_SRC} svga/main.c svga/disp-pal.c			\
	nonx/keys.c nonx/options.c					\
	disp-ncurses.c svga/display.c svga/options.c
TARGETS  += ../cthugha
endif

# source files used by xcthugha
ifeq ($(USE_XWIN),1)
XWIN_SRC = ${DISP_SRC} xwin/main.c  xwin/keys.c				\
	xwin/disp-pal.c  xwin/display.c xwin/options.c 
TARGETS += ../xcthugha
endif

# additional source files for CDROM, DSP, ...
ifeq ($(USE_CDROM),1)
	GEN_SRC += cd_player.c
endif
ifeq ($(USE_DSP),1)
	GEN_SRC += snd-dsp.c
endif
ifeq ($(USE_MIXER),1)
	GEN_SRC += snd-mixer.c
endif
ifeq ($(USE_DGA),1)
	XWIN_SRC += xwin/dga.c
endif	

ifeq ($(USE_SERVER),1)
# source files used by cthugha-server
SERV_SRC = ${GEN_SRC} nonx/keys.c serv/main.c serv/options.c		\
	disp-ncurses.c serv/stubs.c nonx/options.c			\
	serv/display.c
TARGETS += ../cthugha-server
endif

ifeq ($(USE_NSPMOD),1)
TARGETS += ../cth-nspmod
endif

ifeq ($(USE_AMP),1)
TARGETS += ../cth-amp
endif

# object files
SVGA_OBJ = $(SVGA_SRC:.c=.o)
XWIN_OBJ = $(XWIN_SRC:.c=.o)
SERV_OBJ = $(SERV_SRC:.c=.o)

# everything together
ALL_SRC = $(sort $(SVGA_SRC) $(XWIN_SRC) $(SERV_SRC))
ALL_OBJ = $(ALL_SRC:.c=.o)


all: message1 .depend $(TARGETS) 

message1:
	@echo ""
	@echo "Generating" $(TARGETS)
	@echo ""

#
# console (svgalib) version
#
../cthugha: $(SVGA_OBJ) ../configure
	$(CC) -o $@ $(SVGA_OBJ) $(LDFLAGS) $(LIB_CTHUGHA)

#
# X11-version
#
../xcthugha: $(XWIN_OBJ) ../configure
	$(CC) -o $@ $(XWIN_OBJ) $(LDFLAGS) $(LIB_XCTHUGHA)

#
# sound server
#
../cthugha-server: $(SERV_OBJ) ../configure
	$(CC) -o $@ $(SERV_OBJ) $(LDFLAGS) $(LIB_CTHUGHA_SERVER)

#
# Mod file player nspmod
#
.PHONY: ../cth-nspmod
../cth-nspmod: 
	$(MAKE) -C nspmod-0.1
	-rm ../cth-nspmod
	cp nspmod-0.1/nspmod ../cth-nspmod

#
# MPEG Layer3 player amp
#
.PHONY: ../cth-amp
../cth-amp: 
	(cd amp-0.7.3; ./configure)
	$(MAKE) -C amp-0.7.3 dep
	$(MAKE) -C amp-0.7.3
	-rm ../cth-amp
	cp amp-0.7.3/amp ../cth-amp

#
# utilities for translation files
#
../tabheader: tabheader.c cth_buffer.h translate.h
	$(CC) tabheader.c $(LDFLAGS) -o ../tabheader
../tabinfo: tabinfo.c cth_buffer.h translate.h
	$(CC) tabinfo.c $(LDFLAGS) -o ../tabinfo

config.h: ../configure
	@echo "/* Cthugha configuration file"		> config.h
	@echo " * This file is generated automatically" >> config.h
	@echo " * Please do not edit"			>> config.h
	@echo " */"					>> config.h
	@echo "#define USE_CDROM " $(USE_CDROM)		>> config.h
	@echo "#define USE_DGA " $(USE_DGA)		>> config.h
	@echo "#define USE_SAVER " $(USE_SAVER)		>> config.h
	@echo "#define USE_OLDSND " $(USE_OLDSND)	>> config.h
	@echo "#define USE_DSP " $(USE_DSP)		>> config.h
	@echo "#define USE_MIXER " $(USE_MIXER)		>> config.h
	@echo "#define INST_LIB " \"${INST_LIB}\"	>> config.h
	@echo "#define USE_GETOPT_LONG " ${USE_GETOPT_LONG} >> config.h
	@echo "#define DEV_DSP " \"${DEV_DSP}\"         >> config.h
	@echo "#define DEV_MIXER " \"${DEV_MIXER}\"     >> config.h
	@echo "#define DEV_CDROM " \"${DEV_CDROM}\"     >> config.h

%.s: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -S -fverbose-asm $< -o $@

#
# remove some junk
#
.PHONY: clean
clean:
	-rm -f ${ALL_OBJ} *.s config.h .depend
	-make clean -C nspmod-0.1
	-make clean -C amp-0.7.3

.PHONY: dist-clean
dist-clean:
	-rm -f ${ALL_OBJ} *.s config.h .depend
	-make clean -C nspmod-0.1
	-make cleandist -C amp-0.7.3

#
# generated dependencies
#
ifeq ($(USE_DEPEND), 1)

.depend: config.h
	./makedepend.sh $(CC) $(NC_INC) $(X11_INC)


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

else

.depend: config.h
	-rm .depend

endif



