# TI-99/sim Makefile for Linux

ifndef DATA_DIR
DATA_DIR  := /opt/ti99sim
endif

ifndef BIN_DIR
BIN_DIR   := $(DATA_DIR)/bin
endif

ifndef SYS_BIN
SYS_BIN   := /usr/local/bin
endif

ifneq ($(BIN_DIR),$(DATA_DIR)/bin)
	TI_DATA_DIR := TI_DATA_DIR=$(DATA_DIR)
endif

INSTALL   := /usr/bin/install

ITARGETS  = \
	bin/catalog \
	bin/convert-ctg \
	bin/decode \
	bin/disk \
	bin/dumpcpu \
	bin/dumpgrom \
	bin/dumpspch \
	bin/list \
	bin/mkcart \
	bin/mkspch \
	bin/say \
	bin/ti99sim-console \
	bin/ti99sim-sdl

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)/console
	@$(INSTALL) -d $(DATA_DIR)/disks
	@$(INSTALL) -d $(BIN_DIR)
	@for target in $(ITARGETS); do \
	   $(INSTALL) -s $$target $(BIN_DIR); \
	 done
	@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
	@if [ -x bin ]; then \
	   rmdir --ignore-fail-on-non-empty bin; \
	 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 $(TI_DATA_DIR); \
	  done \
	fi
