# -----------------------------------------------------------------------------
# This is the makefile for Level-Editor (c) Henrik Stokseth, 1999.

.PHONY: baddjgpp baddjdev badgcc badbnu badfil badmake badpath badleditor

# check that the DJGPP environment variable is set
ifndef DJDIR
baddjgpp:
	@echo Your DJGPP environment variable is not set correctly! It should
	@echo point to the djgpp.env file: see the djgpp readme.1st for details.
endif

# check that the djdev package is installed
ifeq ($(wildcard $(DJDIR)/bin/djasm.exe),)
baddjdev:
	@echo Missing djgpp package! You need to install djdev202.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif

# check that the gcc package is installed
ifeq ($(wildcard $(DJDIR)/bin/gcc.exe),)
badgcc:
	@echo Missing djgpp package! You need to install gcc2721b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif

# check that the binutils package is installed
ifeq ($(wildcard $(DJDIR)/bin/ld.exe),)
badbnu:
	@echo Missing djgpp package! You need to install bnu27b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif

# check that the fileutils package is installed
ifeq ($(wildcard $(DJDIR)/bin/rm.exe),)
badfil:
	@echo Missing djgpp package! You need to install fil316b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif

# check that the make package is installed
ifeq ($(wildcard $(DJDIR)/bin/make.exe),)
badmake:
	@echo Missing djgpp package! You need to install mak3761b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif

# check that djgpp/bin is pathed
ifeq ($(wildcard $(addsuffix /djasm.exe,$(subst ;, ,$(PATH)))),)
badpath:
	@echo Your PATH is not set correctly! This must include the
	@echo djgpp bin directory: see the djgpp readme.1st for details.
endif

# check that Level-Editor has a good directory structure
ifeq ($(wildcard src/leditor.c),)
badleditor:
	@echo Bad Level-Editor installation! You did not preserve the directory structure
	@echo while unzipping it: did you remember to use the -d flag with pkunzip?
endif

# -----------------------------------------------------------------------------
# Set up default directories and flags

INCLUDE_DIR  = include
EXAMPLE_DIR  = examples
MANIFEST_DIR = manifest
DOCS_DIR     = docs
OBJ_DIR      = obj
BIN_DIR      = bin
SRC_DIR      = src

CFLAGS = -Wall -Wno-unused -m486 -O3 -I$(INCLUDE_DIR)
LFLAGS = -s

# -----------------------------------------------------------------------------
# Set up the various sections and default make

default   : copy_manifest editor docs examples install
editor    : $(BIN_DIR)/leditor.exe
docs      : $(BIN_DIR)/htm2txt.exe $(DOCS_DIR)/readme.txt $(DOCS_DIR)/faq.txt $(DOCS_DIR)/tricks.txt $(DOCS_DIR)/tilemap.txt
install   : copy_stuff
examples  : $(EXAMPLE_DIR)/test1.exe $(EXAMPLE_DIR)/test2.exe $(EXAMPLE_DIR)/test3.exe $(EXAMPLE_DIR)/test4.exe
clean     : remove_obj
veryclean : remove_obj remove_exe remove_ascii remove_install

# -----------------------------------------------------------------------------
# This is the dependencies for the editor

$(OBJ_DIR)/leditor.o : $(SRC_DIR)/leditor.c
	@echo making editor ...
	@gcc $(CFLAGS) -o $(OBJ_DIR)/leditor.o -c $(SRC_DIR)/leditor.c

$(BIN_DIR)/leditor.exe : $(OBJ_DIR)/leditor.o
	@gcc $(LFLAGS) -o $(BIN_DIR)/leditor.exe $(OBJ_DIR)/leditor.o -lalleg

# -----------------------------------------------------------------------------
# This is the dependencies for the docs

$(OBJ_DIR)/htm2txt.o : $(SRC_DIR)/htm2txt.c
	@echo making htm2txt utility ...
	@gcc $(CFLAGS) -o $(OBJ_DIR)/htm2txt.o -c $(SRC_DIR)/htm2txt.c

$(BIN_DIR)/htm2txt.exe : $(OBJ_DIR)/htm2txt.o
	@gcc $(LFLAGS) -o $(BIN_DIR)/htm2txt.exe $(OBJ_DIR)/htm2txt.o

$(DOCS_DIR)/readme.txt : $(DOCS_DIR)/readme.htm
	@echo generating the documentation ...
	@$(BIN_DIR)/htm2txt $(DOCS_DIR)/readme

$(DOCS_DIR)/faq.txt : $(DOCS_DIR)/faq.htm
	@$(BIN_DIR)/htm2txt $(DOCS_DIR)/faq

$(DOCS_DIR)/tricks.txt : $(DOCS_DIR)/tricks.htm
	@$(BIN_DIR)/htm2txt $(DOCS_DIR)/tricks

$(DOCS_DIR)/tilemap.txt : $(DOCS_DIR)/tilemap.htm
	@$(BIN_DIR)/htm2txt $(DOCS_DIR)/tilemap

# -----------------------------------------------------------------------------
# This is the install section

copy_manifest:
	@cp $(MANIFEST_DIR)/ledit102.mft $(DJDIR)/manifest
	@cp $(MANIFEST_DIR)/ledit102.ver $(DJDIR)/manifest

copy_stuff:
	@echo installing ...
	@cp $(INCLUDE_DIR)/tilemap.h $(DJDIR)/include
	@cp $(BIN_DIR)/leditor.exe $(DJDIR)/bin

# -----------------------------------------------------------------------------
# This is the dependencies for the examples

$(OBJ_DIR)/test1.o : $(EXAMPLE_DIR)/test1.c
	@echo making example one ...
	@gcc $(CFLAGS) -o $(OBJ_DIR)/test1.o -c $(EXAMPLE_DIR)/test1.c

$(EXAMPLE_DIR)/test1.exe : $(OBJ_DIR)/test1.o
	@gcc $(LFLAGS) -o $(EXAMPLE_DIR)/test1.exe $(OBJ_DIR)/test1.o -lalleg

$(OBJ_DIR)/test2.o : $(EXAMPLE_DIR)/test2.c
	@echo making example two ...
	@gcc $(CFLAGS) -o $(OBJ_DIR)/test2.o -c $(EXAMPLE_DIR)/test2.c

$(EXAMPLE_DIR)/test2.exe : $(OBJ_DIR)/test2.o
	@gcc $(LFLAGS) -o $(EXAMPLE_DIR)/test2.exe $(OBJ_DIR)/test2.o -lalleg

$(OBJ_DIR)/test3.o : $(EXAMPLE_DIR)/test3.c
	@echo making example three ...
	@gcc $(CFLAGS) -o $(OBJ_DIR)/test3.o -c $(EXAMPLE_DIR)/test3.c

$(EXAMPLE_DIR)/test3.exe : $(OBJ_DIR)/test3.o
	@gcc $(LFLAGS) -o $(EXAMPLE_DIR)/test3.exe $(OBJ_DIR)/test3.o -lalleg

$(OBJ_DIR)/test4.o : $(EXAMPLE_DIR)/test4.c
	@echo making example four ...
	@gcc $(CFLAGS) -o $(OBJ_DIR)/test4.o -c $(EXAMPLE_DIR)/test4.c

$(EXAMPLE_DIR)/test4.exe : $(OBJ_DIR)/test4.o
	@gcc $(LFLAGS) -o $(EXAMPLE_DIR)/test4.exe $(OBJ_DIR)/test4.o -lalleg

# -----------------------------------------------------------------------------
# This is the cleanup section

remove_obj:
	@echo cleaning up (removing object files) ...
	@rm -f $(OBJ_DIR)/*.o

remove_exe:
	@echo cleaning up (removing executables) ...
	@rm -f $(BIN_DIR)/*.exe
	@rm -f $(EXAMPLE_DIR)/*.exe

remove_ascii:
	@echo cleaning up (removing ascii docs) ...
	@rm -f $(DOCS_DIR)/*.txt

remove_install:
	@echo uninstalling ...
	@rm -f $(DJDIR)/manifest/ledit102.mft
	@rm -f $(DJDIR)/manifest/ledit102.ver
	@rm -f $(DJDIR)/include/tilemap.h
	@rm -f $(DJDIR)/bin/leditor.exe

# -----------------------------------------------------------------------------
