# TI-99/sim Makefile for Linux

DATA_DIR  = /opt/ti99sim
BIN_DIR   = $(DATA_DIR)/bin
SYS_BIN   = /usr/local/bin
INSTALL   = /usr/bin/install

ITARGETS  = \
	src/console/$(CFG)/ti99sim-console \
	src/sdl/$(CFG)/ti99sim-sdl \
	src/util/$(CFG)/convert-ctg \
	src/util/$(CFG)/decode \
	src/util/$(CFG)/disk \
	src/util/$(CFG)/dumpcpu \
	src/util/$(CFG)/dumpgrom \
	src/util/$(CFG)/dumpspch \
	src/util/$(CFG)/list \
	src/util/$(CFG)/mkspch \
	src/util/$(CFG)/say

SRC_DIRS = \
	src/core \
	src/console \
	src/sdl \
	src/util

install: ti99sim
	@$(INSTALL) -d $(DATA_DIR)
	@$(INSTALL) -d $(DATA_DIR)/cartridges
	@$(INSTALL) -d $(DATA_DIR)/disks
	@$(INSTALL) -d $(DATA_DIR)/roms
	@$(INSTALL) -d $(BIN_DIR)
	@if [ -x src ]; then \
	  for target in $(ITARGETS); do \
	    $(INSTALL) -s $$target $(BIN_DIR); \
	  done \
	else \
	  for target in $(notdir $(ITARGETS)); do \
	    $(INSTALL) -s $$target $(BIN_DIR); \
	  done \
	fi
	@if [ $(BIN_DIR) != $(SYS_BIN) ]; then \
	  for target in $(notdir $(ITARGETS)); do \
	    ln -f -s $(BIN_DIR)/$$target $(SYS_BIN)/$$target; \
	  done \
	fi

uninstall:
	@for target in $(notdir $(ITARGETS)); do \
	  rm -f $(BIN_DIR)/$$target; \
	  rm -f $(SYS_BIN)/$$target; \
	done
	@rm -Rd $(DATA_DIR)

clean:
	@if [ -x src ]; then \
	  rm -f {.,include,doc}/*~; \
	  for dir in $(SRC_DIRS); do \
	    $(MAKE) -C $$dir clean; \
	  done \
	fi

dep:
	@if [ -x src ]; then \
	  for dir in $(SRC_DIRS); do \
	    $(MAKE) -C $$dir dep; \
	  done \
	fi

ti99sim:
	@if [ -x src ]; then \
	  if ! [ -r /usr/include/endian.h ]; then \
	    if [ -r /usr/include/machine/endian.h ]; then \
	      rm -f include/endian.h; \
	      ln -s /usr/include/machine/endian.h include/endian.h; \
	    fi \
	  fi; \
	  for dir in $(SRC_DIRS); do \
	    $(MAKE) -C $$dir; \
	  done \
	fi
