###############################################################################
# BRLTTY - A background process providing access to the Linux console (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2003 by The BRLTTY Team. All rights reserved.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation.  Please see the file COPYING for details.
#
# Web Page: http://mielke.cc/brltty/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

include $(BLD_TOP)config.mk

all: all-brltty brltty-install all-brltest scrtest tunetest $(BUILD_API)
all-brltty: brltty $(BRAILLE_DRIVERS) $(BRAILLE_HELP) $(SPEECH_DRIVERS)
all-brltest: brltest $(BRAILLE_DRIVERS)

###############################################################################

options.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/options.c

###############################################################################

ctb_compile.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/ctb_compile.c

ctb_translate.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/ctb_translate.c

###############################################################################

SCREEN_OBJECTS = scr.o scr_base.o scr_real.o scr_frozen.o scr_help.o $(SCREEN_OBJECT)

scr.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr.c

scr_base.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_base.c

scr_real.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_real.c

scr_frozen.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_frozen.c

scr_help.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_help.c

scr_null.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_null.c

scr_linux.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_linux.c

scr_shm.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scr_shm.c

###############################################################################

TUNE_OBJECTS = tunes.o $(BEEPER_OBJECTS) $(PCM_OBJECTS) $(MIDI_OBJECTS) $(FM_OBJECTS)

tunes.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/tunes.c

beeper.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/beeper.c

pcm.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/pcm.c

midi.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/midi.c

fm.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/fm.c

adlib.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/adlib.c

###############################################################################

API_LIB_OBJS = api_client.o api_common.o
API_NAME = $(LIB_PFX)brlapi
API_LIB = $(API_NAME).$(LIB_EXT)
API_ARC = $(API_NAME).$(ARC_EXT)

api: $(API_LIB) $(API_ARC)

$(API_LIB): $(API_LIB_OBJS)
	$(MKLIB:SONAME=$(API_LIB).$(API_VERSION)) $@ $(API_LIB_OBJS)

$(API_ARC): $(API_LIB_OBJS)
	$(AR) rc $@ $(API_LIB_OBJS)
	$(RANLIB) $@

api_common.o:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/api_common.c

api_client.o:
	$(CC) $(LIBCFLAGS) -c $(SRC_DIR)/api_client.c

api_server.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/api_server.c

rangelist.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/rangelist.c

###############################################################################

BRLTTY_OBJECTS = main.o config.o options.o misc.o sysmisc.o $(SYSTEM_OBJECT) route.o cut.o $(CTB_OBJECTS) brl.o $(SPEECH_OBJECT) $(API_OBJECTS) $(SCREEN_OBJECTS) $(TUNE_OBJECTS) $(BRAILLE_DRIVER_OBJECT) $(SPEECH_DRIVER_OBJECT)

brltty: $(BRLTTY_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(BRLTTY_OBJECTS) $(LDLIBS)

main.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/main.c

config.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/config.c

misc.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/misc.c

sysmisc.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/sysmisc.c

sys_linux.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/sys_linux.c

sys_solaris.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/sys_solaris.c

sys_hpux.o:
	$(CC) $(CFLAGS) -I/opt/audio/include -c $(SRC_DIR)/sys_hpux.c

route.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/route.c

cut.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/cut.c

brl.o: text.auto.h attrib.auto.h cmds.auto.h
	$(CC) $(CFLAGS) -c $(SRC_DIR)/brl.c

spk.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/spk.c

###############################################################################

BRLTEST_OBJECTS = brltest.o options.o misc.o sysmisc.o $(SYSTEM_OBJECT) brl.o $(BRAILLE_DRIVER_OBJECT)

brltest: $(BRLTEST_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(BRLTEST_OBJECTS) $(LDLIBS)

brltest.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/brltest.c

###############################################################################

SCRTEST_OBJECTS = scrtest.o options.o misc.o route.o

scrtest: $(SCRTEST_OBJECTS) $(SCREEN_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(SCRTEST_OBJECTS) $(SCREEN_OBJECTS) $(LDLIBS)

scrtest.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/scrtest.c

###############################################################################

TUNETEST_OBJECTS = tunetest.o options.o misc.o $(SYSTEM_OBJECT)

tunetest: $(TUNETEST_OBJECTS) $(TUNE_OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(TUNETEST_OBJECTS) $(TUNE_OBJECTS) $(LDLIBS)

tunetest.o:
	$(CC) $(CFLAGS) -c $(SRC_DIR)/tunetest.c

###############################################################################

braille-drivers: txt2hlp
	-rm -f $(BLD_TOP)$(DRV_DIR)/brltty-brl.lst
	for target in $(BRL_TARGETS); \
	do (cd $(BLD_TOP)Drivers/$$target && $(MAKE) braille-driver braille-help brl-lib-name) || exit 1; \
	done

braille-help: txt2hlp
	cd $(BLD_TOP)Drivers/$(BRAILLE_DRIVER_NAME) && $(MAKE) braille-help

###############################################################################

speech-drivers:
	-rm -f $(BLD_TOP)$(DRV_DIR)/brltty-spk.lst
	for target in $(SPK_TARGETS); \
	do (cd $(BLD_TOP)Drivers/$$target && $(MAKE) speech-driver spk-lib-name) || exit 1; \
	done

###############################################################################

text.auto.h: $(SRC_TOP)$(TBL_DIR)/$(TEXTTRANS) tbl2hex
	./tbl2hex <$(SRC_TOP)$(TBL_DIR)/$(TEXTTRANS) >$@

attrib.auto.h: $(SRC_TOP)$(TBL_DIR)/$(ATTRTRANS) tbl2hex
	./tbl2hex <$(SRC_TOP)$(TBL_DIR)/$(ATTRTRANS) >$@

cmds.auto.h: $(SRC_DIR)/brldefs.h $(SRC_DIR)/cmds.awk
	$(AWK) -f $(SRC_DIR)/cmds.awk $(SRC_DIR)/brldefs.h >$@

###############################################################################

HOSTOPTS_OBJECTS = host_misc.o hostopts.o

hostopts.o:
	$(HOSTCC) $(HOSTCFLAGS) -o $@ -c $(SRC_DIR)/options.c

host_misc.o:
	$(HOSTCC) $(HOSTCFLAGS) -o $@ -c $(SRC_DIR)/misc.c

###############################################################################

TXT2HLP_OBJECTS = txt2hlp.o $(HOSTOPTS_OBJECTS)

txt2hlp: $(TXT2HLP_OBJECTS)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $(TXT2HLP_OBJECTS) $(LDLIBS)

txt2hlp.o:
	$(HOSTCC) $(HOSTCFLAGS) -c $(SRC_DIR)/txt2hlp.c

###############################################################################

TBL2HEX_OBJECTS = tbl2hex.o $(HOSTOPTS_OBJECTS)

tbl2hex: $(TBL2HEX_OBJECTS)
	$(HOSTCC) $(HOSTLDFLAGS) -o $@ $(TBL2HEX_OBJECTS) $(LDLIBS)

tbl2hex.o:
	$(HOSTCC) $(HOSTCFLAGS) -c $(SRC_DIR)/tbl2hex.c

###############################################################################

brltty-install: $(SRC_DIR)/brltty-install.in
	sed -e 's%=F%$(EXECUTE_ROOT)%g' \
	    -e 's%=P%$(PROGRAM_DIRECTORY)%g' \
            -e 's%=L%$(LIBRARY_DIRECTORY)%g' \
	    -e 's%=D%$(DATA_DIR)%g' \
	    -e 's%=C%$(sysconfdir)%g' \
            $(SRC_DIR)/brltty-install.in >$@
	chmod a+rx $@

###############################################################################

check-braille: brltty dynamic-braille
	for lib in $(BRL_LIBS); \
	do test $$lib = no && continue; \
	   ls -l $(BLD_TOP)$(DRV_DIR)/libbrlttyb$$lib.$(LIB_EXT) || exit 10; \
	   ./brltty -v -q -N -e -f /dev/null -b $$lib -s no -L "$(BLD_TOP)$(DRV_DIR)" 2>&1 || exit 11; \
	done

check-speech: brltty dynamic-speech
	for lib in $(SPK_LIBS); \
	do test $$lib = no && continue; \
	   ls -l $(BLD_TOP)$(DRV_DIR)/libbrlttys$$lib.$(LIB_EXT) || exit 10; \
	   ./brltty -v -q -N -e -f /dev/null -b no -s $$lib -L "$(BLD_TOP)$(DRV_DIR)" 2>&1 || exit 11; \
	done

###############################################################################

install:: install-programs install-help install-tables $(INSTALL_DRIVERS) install-manpages $(INSTALL_API)

install-programs: brltty brltty-install
	$(INSTALL_DIRECTORY) $(INSTALL_ROOT)$(PROGRAM_DIRECTORY) 
	$(INSTALL_PROGRAM) brltty $(INSTALL_ROOT)$(PROGRAM_DIRECTORY) 
	$(INSTALL_SCRIPT) brltty-install $(INSTALL_ROOT)$(PROGRAM_DIRECTORY) 

install-help: $(BRAILLE_DRIVERS) install-data-directory
	$(INSTALL_DATA) $(BLD_TOP)$(HLP_DIR)/* $(INSTALL_ROOT)$(DATA_DIR) 

install-tables: $(INSTALL_TEXT_TABLES) $(INSTALL_ATTRIBUTES_TABLES) $(INSTALL_CONTRACTION_TABLES)

install-text-tables: install-data-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/text.*.tbl $(INSTALL_ROOT)$(DATA_DIR)

install-attributes-tables: install-data-directory
	$(INSTALL_DATA) $(SRC_TOP)$(TBL_DIR)/attr*.tbl $(INSTALL_ROOT)$(DATA_DIR)

install-contraction-tables: install-data-directory
	$(INSTALL_DATA) $(SRC_TOP)$(CTB_DIR)/*.ctb $(INSTALL_ROOT)$(DATA_DIR)
	$(INSTALL_DATA) $(SRC_TOP)$(CTB_DIR)/*.cti $(INSTALL_ROOT)$(DATA_DIR)

install-data-directory:
	$(INSTALL_DIRECTORY) $(INSTALL_ROOT)$(DATA_DIR) 

install-drivers: brltty
	$(INSTALL_DIRECTORY) $(INSTALL_ROOT)$(LIBRARY_DIRECTORY)
	$(INSTALL_PROGRAM) $(BLD_TOP)$(DRV_DIR)/*.$(LIB_EXT) $(INSTALL_ROOT)$(LIBRARY_DIRECTORY)
	$(INSTALL_DATA) $(BLD_TOP)$(DRV_DIR)/*.lst $(INSTALL_ROOT)$(LIBRARY_DIRECTORY)

install-manpages:
	-for page in $(SRC_TOP)$(DOC_DIR)/*.[1-9]; do \
	   section=$${page##*.} && \
	   directory=$(INSTALL_ROOT)$(MANPAGE_DIRECTORY)/man$${section} && \
	   $(INSTALL_DIRECTORY) $${directory} && \
	   $(INSTALL_DATA) $${page} $${directory}; \
	done

install-api: install-api-libraries install-api-headers install-api-key

install-api-libraries: $(API_LIB) $(API_ARC)
	$(INSTALL_DIRECTORY) $(INSTALL_ROOT)$(libdir)
	$(INSTALL_PROGRAM) $(API_LIB) $(INSTALL_ROOT)$(libdir)/$(API_LIB).$(API_RELEASE)
	$(SYMLINK) -f $(API_LIB).$(API_RELEASE) $(INSTALL_ROOT)$(libdir)/$(API_LIB)
	$(CONFLIBDIR) $(INSTALL_ROOT)$(libdir)
	$(INSTALL_DATA) $(API_ARC) $(INSTALL_ROOT)$(libdir)

install-api-headers:
	$(INSTALL_DIRECTORY) $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)
	$(INSTALL_DATA) brlapi.h $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)
	$(INSTALL_DATA) brldefs.h $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)

install-api-key:
	file=$(sysconfdir)/$(API_KEYFILE) && \
	if test ! -f $$file -a -w $(sysconfdir) -a -z "$(INSTALL_ROOT)"; \
	then umask 077 && mcookie >$$file; fi

###############################################################################

uninstall:: uninstall-programs uninstall-data uninstall-libraries uninstall-manpages uninstall-headers

uninstall-programs:
	-rm -f $(INSTALL_ROOT)$(PROGRAM_DIRECTORY)/brltty
	-rm -f $(INSTALL_ROOT)$(PROGRAM_DIRECTORY)/brltty-install

uninstall-data:
	-rm -f $(INSTALL_ROOT)$(DATA_DIR)/*.tbl
	-rm -f $(INSTALL_ROOT)$(DATA_DIR)/*.ctb
	-rm -f $(INSTALL_ROOT)$(DATA_DIR)/*.cti
	-rm -f $(INSTALL_ROOT)$(DATA_DIR)/brltty*
	-rmdir $(INSTALL_ROOT)$(DATA_DIR)

uninstall-libraries:
	-rm -f $(INSTALL_ROOT)$(LIBRARY_DIRECTORY)/$(LIB_NAME)*
	-rm -f $(INSTALL_ROOT)$(LIBRARY_DIRECTORY)/brltty-*.lst
	-rmdir $(INSTALL_ROOT)$(LIBRARY_DIRECTORY)
	-rm -f $(INSTALL_ROOT)$(libdir)/$(API_LIB)
	-rm -f $(INSTALL_ROOT)$(libdir)/$(API_LIB).$(API_RELEASE)
	$(CONFLIBDIR) $(INSTALL_ROOT)$(libdir)
	-rm -f $(INSTALL_ROOT)$(libdir)/$(API_ARC)

uninstall-manpages:
	-for page in $(SRC_TOP)$(DOC_DIR)/*.[1-9]; do \
	   section=$${page##*.} && \
	   directory=$(INSTALL_ROOT)$(MANPAGE_DIRECTORY)/man$${section} && \
	   rm -f $${directory}/$${page##*/}; \
	done

uninstall-headers:
	-rm -f $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)/brlapi.h
	-rm -f $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)/brldefs.h
	-rm -f $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)/brldefs-*.h
	-rmdir $(INSTALL_ROOT)$(INCLUDE_DIRECTORY)

###############################################################################

distclean::
	-rm -f brlapi.h

clean::
	-rm -f brltty brltty-install txt2hlp tbl2hex *test *-static
	-rm -f $(API_LIB) $(API_ARC)
	-rm -f $(BLD_TOP)$(DRV_DIR)/* $(BLD_TOP)$(HLP_DIR)/*

distclean clean install uninstall::
	-for driver in $(CONFIGURED_DRIVERS); do (cd $(BLD_TOP)Drivers/$$driver && $(MAKE) $@); done
