# Makefile for Unix Frotz
# GNU make is required.

SOURCES = ux_audio.c ux_audio_none.c ux_audio_oss.c ux_blorb.c ux_init.c \
	ux_input.c ux_pic.c ux_screen.c ux_text.c

HEADERS = ux_blorb.h ux_frotz.h ux_setup.h

DEFINES = ux_defines.h

OBJECTS = $(SOURCES:.c=.o)

TARGET = frotz_curses.a

ARFLAGS = rc

SOUND_TYPE ?= ao

UNAME_S := $(shell uname -s)

ifeq ($(SOUND_TYPE), ao)
ifneq ($(UNAME_S),Haiku)
CFLAGS += -pthread
ifeq ($(UNAME_S),Darwin)
HOMEBREW_PREFIX ?= $(shell brew --prefix)
CFLAGS += -I$(HOMEBREW_PREFIX)/include
endif
endif
else ifeq ($(SOUND_TYPE), none)

else ifndef SOUND_TYPE

else
$(error Invalid sound choice $(SOUND_TYPE))
endif

$(TARGET): $(DEFINES) $(OBJECTS) | $(DEFINES)
	$(AR) $(ARFLAGS) $@ $(OBJECTS)
	$(RANLIB) $@
	@echo "** Done with curses interface."

$(DEFINES):
	@echo "** Generating $@"
	@echo "/* This file was automatically generated by the build process. */" > $@
	@echo "/* These are defines specific to the curses interface. */" >> $@
	@echo >> $@
	@echo "#ifndef UX_CURSES_DEFINES_H" >> $@
	@echo "#define UX_CURSES_DEFINES_H" >> $@
	@echo "#define $(CURSES_DEFINE)" >> $@
	@echo "#define CONFIG_DIR \"$(SYSCONFDIR)\"" >> $@

ifeq ($(SOUND_TYPE),none)
	@echo "#define NO_SOUND" >> $@
else
	@echo "#define SOUND_TYPE \"$(SOUND_TYPE)\"" >> $@
	@echo "#define SAMPLERATE $(SAMPLERATE)" >> $@
	@echo "#define BUFFSIZE $(BUFFSIZE)" >> $@
	@echo "#define DEFAULT_CONVERTER $(DEFAULT_CONVERTER)" >> $@
endif

ifdef COLOR
	@echo "#define COLOR_SUPPORT" >> $@
endif
ifdef ITALIC
	@echo "#define ITALIC_SUPPORT" >> $@
endif
	@echo "#endif /* UX_CURSES_DEFINES_H */" >> $@



clean:
	rm -f $(TARGET) $(OBJECTS) $(DEFINES)

%.o: %.c $(DEFINES)
	$(CC) $(CFLAGS) $(CURSES_CFLAGS) $(NO_SOUND) -fPIC -fpic -o $@ -c $<

.PHONY: clean $(DEFINES)
.DELETE_ON_ERROR:
