# Makefile, ilibc_64.a
# Byron Young, 2002
# Version 01
# BASH required.

include  Makefile.g

# a list of all the directories to recurse into
# order is critical, and must match LIBNAMESAA
DIRS=   string\
	misc

DEBUG_DIRS=$(DIRS:%=debug/%)
RELEASE_DIRS=$(DIRS:%=release/%)

LIBNAME_DEBUG:=libilibcd64.a
LIBNAME_RELEASE:=libilibc_64.a

#LIBNAMESA:=$(DIRS:%=%/%.lib)
LIBNAMESAA:=	string/string\
		misc/misc

# easy way to change library ending
LIBNAMESA:=$(LIBNAMESAA:%=%.a)

LIBNAMES_DEBUG:=$(LIBNAMESA:%=debug/%)
LIBNAMES_RELEASE:=$(LIBNAMESA:%=release/%)

# #######################################
# ## project files

# ## include files
INCLUDE_FILES_A:=ilibc_64.inc ilibc_64_ctypes.h ilibc_64.h
INCLUDE_FILES:=$(INCLUDE_FILES_A:%=include/ilibc_64/%)

INCLUDE_FILES_ASM_A:=ilibc_64.inc ilibc_64.h
INCLUDE_FILES_C_A:=ilibc_64.h ilibc_64_ctypes.h
INCLUDE_FILES_ASM:=$(INCLUDE_FILES_ASM_A:%=include/ilibc_64/%)
INCLUDE_FILES_C:=$(INCLUDE_FILES_C_A:%=include/ilibc_64/%)

# ## document files
DOC_FILES_A:=ilibc_64.txt
DOC_FILES:=$(DOC_FILES_A:%=doc/%)

# ## Makefiles
TOP_MKFILES:=Makefile Makefile.g

# tar.gz version file
VERSION:= 01
TARRELEASENAME:=ilibc_64_$(VERSION).tar.gz

PROJ_FILES = $(TOP_MKFILES) $(DOC_FILES) $(INCLUDE_FILES) template.S

.PHONY: debug release
.PHONY: $(DEBUG_DIRS) $(RELEASE_DIRS)

all : debug release

debug: $(LIBNAMES_DEBUG) debug/$(LIBNAME_DEBUG)
	echo debug

release: $(LIBNAMES_RELEASE) release/$(LIBNAME_RELEASE)
	echo releasee


$(LIBNAMES_DEBUG):
	$(MAKE) -C $(@F:.a= ) DIR_PROJ=$(CURDIR) DEBUG=1 ../$(@)

$(LIBNAMES_RELEASE):
	$(MAKE) -C $(@F:.a= ) DIR_PROJ=$(CURDIR) DEBUG=0 ../$(@)

debug/$(LIBNAME_DEBUG) :
	@echo Creating $(@)
	rm -f $(@)
	@for xfile in $(LIBNAMES_DEBUG); do ar -x $$xfile; done
	ar -src $(@) *.o
	rm -rf *.o
	@echo Done.

release/$(LIBNAME_RELEASE) :
	@echo Creating $(@)
	rm -f $(@)
	@for xfile in $(LIBNAMES_RELEASE); do ar -x $$xfile; done
	ar -src $(@) *.o
	rm -rf *.o
	@echo Done.

clean:
	@echo Cleaning project directory
	@rm -f setup.sh
	@rm -f *~
	@echo Deleting release directory
	@rm -rf release
	@mkdir release
	@for xfile in $(DIRS); do mkdir release/$$xfile; done
	@echo Deleting debug directory
	@rm -rf debug
	@mkdir debug
	@for xfile in $(DIRS); do mkdir debug/$$xfile; done
	@echo Cleaning include directory
	@rm -f include/ilibc_64/*~
	@for xfile in $(DIRS); do rm -f $$xfile/*~; done
	@rm -f doc/*~
	$(MAKE) -C test clean

# creates a setup.sh file in project directory, which defines PF
# PF contains all the files in the project.
# usage: emacs $PF&

setup:
	@echo Creating setup.sh file
	@echo "#"!/bin/sh > setup.sh
	@echo PF="\""$(PROJ_FILES)"\"" >> setup.sh
	@for xfile in $(DIRS); do cd $$xfile; $(MAKE) setup; cd .. ; done
	@$(MAKE) -C test setup
	@echo export PF >> setup.sh
	@chmod +x setup.sh

echo_vars:
	@echo $(CFLAGS_G) - $(CFLAGS)
	@echo $(LIBNAMES_DEBUG)
	@echo $(LIBNAMES_RELEASE
	@echo $(DEBUG_DIRS)

# clean up any installation files
install_clean:
	rm -rf $(DIR_C_INCLUDE)/ilibc_64
	rm -rf $(DIR_ASM_INCLUDE)/ilibc_64
	rm -rf $(DIR_LIB)/$(LIBNAME_DEBUG)
	rm -rf $(DIR_LIB)/$(LIBNAME_RELEASE)

# install debug and release libraries into specified directory.
# install include files into specified directory.
install:
	@echo Verifying $(DIR_C_INCLUDE) exists.
	@[ -d $(DIR_C_INCLUDE) ]
	@echo Verifying $(DIR_ASM_INCLUDE) exitst.
	@[ -d $(DIR_ASM_INCLUDE) ]
	@echo Verifying $(DIR_LIB) exists.
	@[ -d $(DIR_LIB) ]
	@if [ ! -d $(DIR_C_INCLUDE)/ilibc_64 ]; then mkdir $(DIR_C_INCLUDE)/ilibc_64; fi
	@if [ ! -d $(DIR_ASM_INCLUDE)/ilibc_64 ]; then mkdir $(DIR_ASM_INCLUDE)/ilibc_64; fi
	@echo Copying debug/$(LIBNAME_DEBUG) to $(DIR_LIB)
	@cp debug/$(LIBNAME_DEBUG) $(DIR_LIB)
	@echo Copying release/$(LIBNAME_RELEASE) to $(DIR_LIB)
	@cp release/$(LIBNAME_RELEASE) $(DIR_LIB)
	@echo Copying *.h files to $(DIR_C_INCLUDE)/ilibc_64
	@for xfile in $(DIRS); do cp -f include/ilibc_64/$$xfile.h $(DIR_C_INCLUDE)/ilibc_64; done
	@for xfile in $(INCLUDE_FILES_C); do cp -f $$xfile $(DIR_C_INCLUDE)/ilibc_64; done
	@echo Copying *.inc files to $(DIR_ASM_INCLUDE)/ilibc_64
	@for xfile in $(DIRS); do cp -f include/ilibc_64/$$xfile.inc $(DIR_ASM_INCLUDE)/ilibc_64; done
	@for xfile in $(INCLUDE_FILES_ASM); do cp -f $$xfile $(DIR_ASM_INCLUDE)/ilibc_64; done


.PHONY: test
test:
	@$(MAKE) -C test all

zip:
	rm -rf ../ilibc_64.zip
	(cd ..; zip -r ilibc_64.zip ilibc_64 -x *CVS/*; cd ilibc_64)
