############################################################
#                                                          #
#      Textdata makefile, by Grzegorz Adam Hankiewicz      #
#                                                          #
#      This will only work if you preserved the di-        #
#      rectory tree when installing Textdata (ie. if       #
#      you used Winzip or the -d flag of pkunzip).         #
#                                                          #
############################################################

# -------- variables and definitions --------------
LIB = lib/libtdata.a
INC = src/textdata.h
OBJ = obj/

OBJS = obj/textdata.o
SRC = src/

TOOLS = tools/maketext.exe
HANGMAN = hangman/hangman.exe

LIBDEST = $(DJDIR)/$(LIB)
BIN = $(DJDIR)/bin/maketext.exe
BINDEST = $(subst /,\,$(BIN))
CPLIBSRC = $(subst /,\,$(LIB))

CPIN = $(subst $(SRC),,$(INC))
INCDEST = $(DJDIR)/include/$(CPIN)

CPINCSRC = $(subst /,\,$(INC))
CPLIBDEST = $(subst /,\,$(LIBDEST))
CPINCDEST = $(subst /,\,$(INCDEST))
CP = copy
RM = rm -v -f
FLAGS =-m486 -O3 -s -ffast-math -fomit-frame-pointer

# -------- Rules start here -----------------------

all : msg install tools hangman
	@echo
	@echo ͻ
	@echo  ALL DONE !!! 
	@echo ͼ
	@echo
	@echo To use Textdata, #include textdata.h and link with libtdata.a
	@echo Example command line: gcc foobar.c -o foobar.exe -ltdata
	@echo Enjoy!
	@echo
	@echo If you would like to delete this lib, type first: make uninstall to remove all
	@echo files copied to your djgpp directory, then erase the textdata directory

install : $(LIBDEST)

makedoc : $(DOCE)

uninstall :
	$(RM) $(LIBDEST) $(INCDEST) $(BIN)

$(LIBDEST) : $(LIB)
	@echo
	@echo Copying lib and header to DJGPPs directory...
	@$(CP) $(CPLIBSRC) $(CPLIBDEST)
	@$(CP) $(CPINCSRC) $(CPINCDEST)

$(LIB) : $(OBJS)
	@echo
	@echo Linking all the lib together in one file...
	ar rs $(LIB) $(OBJS)

$(OBJ)%.o : $(SRC)%.c
	@echo
	@echo Compiling source files. Please wait...
	gcc $(FLAGS) -c $< -o $@

tools: $(TOOLS)

tools/%.exe : tools/%.c
	@echo
	@echo Compiling tool $<
	gcc $(FLAGS) -o $@ $<
    ifneq ($(wildcard $(DJDIR)/bin/djp.exe),)

	@echo
	@echo Compressing tool $@
	$(wildcard $(DJDIR)/bin/djp.exe) -s $@
    endif

	@echo Copying tool to djgpp bin directory...
	@$(CP) tools\maketext.exe $(BINDEST)

hangman : $(HANGMAN)

hangman/%.exe : hangman/%.o
	gcc $(FLAGS) -o $@ $< -ltdata -lalleg

hangman/%.o : hangman/%.c
	gcc $(FLAGS) -o $@ -c $<

clean :
	@echo
	@echo Cleaning rebuildable files...
	@$(RM) $(OBJS) $(LIB) $(TOOLS) $(HANGMAN)

msg :
	@echo Ok, I will try to create the text library. Please wait...
