
# ------------------------------------ Initialization -----------------

# -- Programs && Libraries
CC      = gcc
AR      = ar
RM      = rm -rf
CP      = cp -r
LN      = ln -sf
MKDIR   = mkdir -p
TERM    = -lncurses
#TERM    = -lcurses
#TERM   = -ltermcap   # if you don't have ncurses

ifndef PROGRAM
PROGRAM = bastard
endif


# -- Directories
ifndef TOP
TOP = ..
endif
ifndef BASTARD_VERSION
BASTARD_VERSION = 0.16
endif

BINDIR   = $(TOP)/bin
DOCDIR   = $(TOP)/doc
LIBDIR   = $(TOP)/lib
INCDIR   = $(TOP)/include
SHAREDIR = $(TOP)/share
UTILDIR  = $(TOP)/utils


# -- libbastard.so compile flags
LIB_CFLAGS = -fPIC -I$(INCDIR) -ggdb -I/usr/local/include -O3 -DBASTARD_VERSION=$(BASTARD_VERSION)
LIB_ARFLAGS = -L$(LIBDIR)
LIB_SONAME  = libbastard.so
LIB_SOFLAGS =  -I$(INCDIR) -shared -lc -lm -ldl -Wl,-soname,$(LIB_SONAME)


# -- libbastard.so source files
LIB_SRCS    =       			\
	 api/api_address.c		\
	 api/api_code.c			\
	 api/api_disasm.c			\
	 api/api_env.c			\
	 api/api_functions.c		\
	 api/api_importexport.c		\
	 api/api_intcode.c		\
	 api/api_loadsave.c		\
	 api/api_macros.c			\
	 api/api_namecomment.c		\
	 api/api_search.c			\
	 api/api_section.c		\
	 api/api_strings.c		\
	 api/api_structconst.c		\
	 api/api_system.c			\
	 api/api_xref.c			\
	 db.c          			\
	 db/db_query.c         		\
	 eic_wrappers.c     		\
	 extension.c            	\
	 macro.c            		\
	 vm.c            			\
	 util.c


# -- libbastard.so object files
LIB_OBJS    =       		\
	api/api_address.o		\
	api/api_code.o		\
	api/api_disasm.o		\
	api/api_env.o		\
	api/api_functions.o	\
	api/api_importexport.o	\
	api/api_intcode.o		\
	api/api_loadsave.o	\
	api/api_macros.o		\
	api/api_namecomment.o	\
	api/api_search.o  	\
	api/api_section.o		\
	api/api_strings.o		\
	api/api_structconst.o	\
	api/api_system.o		\
	api/api_xref.o		\
	db.o          		\
	db/db_query.o     	\
	eic_wrappers.o		\
	extension.o       	\
	macro.o           	\
	vm.o              	\
	util.o            



# -- Extensions
ARCHDIR   = arch
ASMDIR    = asm
COMPDIR   = comp
ENGINEDIR = engines
FORMATDIR = formats
LANGDIR   = lang
OSDIR     = os
PLUGINDIR = plugins




# ------------------------------------ Actual Makefile Rulesets ------
all:            third_party $(BINDIR)/$(PROGRAM) extras

install: extras-install

uninstall:

clean: eic-clean typhoon-clean ext-clean extras-clean
	@echo Cleaning directories...
	@cd cli && make clean
	@$(RM) *~ *core $(LIBDIR)/$(LIB_SONAME)
	@$(RM) $(LIB_OBJS)
	@echo _____________________________________________________________
	@echo Done.

distclean:
	@cd typhoon; make distclean


# -- Rules to make "extra" binaries and utilities
extras: 
	@cd bdbdump && make all
	@cd bobjdump && make all
#	@cd libbsql && make all
#	@cd bdbview && make all
#	@cd bda && make all

extras-install: 
	@cd bdbdump && make install
	@cd bobjdump && make install
#	@cd libbsql && make install
#	@cd bdbview && make install
#	@cd bda && makeinst all

extras-clean: 
#	@cd libbsql && make clean
#	@cd bdbview && make clean
	@cd bdbdump && make clean
	@cd bobjdump && make clean
#	@cd bda && make clean

$(BINDIR)/sob:
	@echo _____________________________________________________________
	@echo Attempting to make SOB...
	@cd sob && make install
	@echo Done with SOB

sob: $(BINDIR)/sob

sob-install: $(BINDIR)/sob
	@echo _____________________________________________________________
	@echo  Installing SOB
	@[ -d $(INSTALL_DIR)/bin ] || mkdir $(INSTALL_DIR)/bin
	@[ -f sob/src/sob ] && cp sob/src/sob $(INSTALL_DIR)/bin/
	@ ln -sf  $(INSTALL_DIR)/bin/sob $(INSTALL_BIN)/sob
	@[ -d $(INSTALL_DIR)/share/sob/pixmaps ] || \
	      mkdir -p $(INSTALL_DIR)/share/sob/pixmaps
	@cp -r sob/pixmaps/*.xpm $(INSTALL_DIR)/share/sob/pixmaps
	@echo Done installing SOB

sob-uninstall: 
	@echo _____________________________________________________________
	@echo  Uninstalling SOB
	@[ -f $(INSTALL_BIN)/sob ] && rm -f $(INSTALL_BIN)/sob
	@[ -d $(INSTALL_DIR)/share/sob ] && rm -rf $(INSTALL_DIR)/share/sob
	@echo Done uninstalling SOB

sob-clean:
	@echo _____________________________________________________________
	@echo Cleaning SOB
	@$(RM) $(BINDIR)/sob
	@cd sob && make clean



# -- Rules to make Typhoon and EiC
third_party: typhoon eic

# ---- Typhoon:
typhoon: $(LIBDIR)/libtyphoon.a $(DOCDIR)/typhoon.txt

$(TOP)/doc/typhoon.txt:
	@cp typhoon/man/manual.asc $(LIBDIR)/typhoon.txt
	@cp typhoon/man/*.1 $(LIBDIR)/man

$(LIBDIR)/libtyphoon.a:
	@echo _____________________________________________________________
	@echo "   Compiling Typhoon embedded relational database..."
	@cd typhoon; ./configure; make install
	@cp typhoon/util/dbdview  $(UTILDIR)
	@cp typhoon/util/ddlp $(UTILDIR)
	@cp typhoon/util/tyexport $(UTILDIR)
	@cp typhoon/util/tyimport $(UTILDIR)
	@echo _____________________________________________________________
	@echo Done with Typhoon

typhoon-clean:
	@$(RM) $(LIBDIR)/libtyphoon.a
	@$(RM) $(UTILDIR)/dbdview $(UTILDIR)/ddlp
	@$(RM) $(UTILDIR)/tyexport $(UTILDIR)/tyimport
#	@cd    typhoon; make clean

# ---- EiC:
eic: $(LIBDIR)/libeic.a $(LIBDIR)/libstdClib.a $(DOCDIR)/EiC.ps
	@cp EiC/eic.man* $(DOCDIR)/man/eic.1

$(TOP)/doc/EiC.ps: 
	@([ -f "`which latex`" ] && cd EiC/doc && make EiC.dvi) || [ 1 ]
	@([ -f EiC/doc/EiC.ps ] && cp EiC/doc/EiC.ps $(DOCDIR)/EiC.ps) || [ 1 ]
	@([ -f "`which pstotext`" ] && pstotext EiC/doc/EiC.ps > \
		$(DOCDIR)/EiC.txt ) || [ 1 ]

$(TOP)/include/script/eic:
	@$(MKDIR) $(TOP)/include/script/eic

$(TOP)/lib/libeic.a:  $(TOP)/include/script/eic
	@echo _____________________________________________________________
	@echo "   Compiling EiC embedded interpreter..."
	#@touch EiC/module/stdClib/src/instH EiC/module/stdClib/src/instSysH
	@cd EiC; config/makeconfig; make; make install
	@cp EiC/lib/libeic.a $(LIBDIR)
	@cp EiC/include/eic* $(INCDIR)/script/
	@cp -r  EiC/include/* $(INCDIR)/script/eic/
	@echo _____________________________________________________________
	@echo Done with EiC
	

$(LIBDIR)/libstdClib.a: $(LIBDIR)/libeic.a 
	@cp EiC/lib/libstdClib.a $(LIBDIR)

eic-clean:
	@$(RM) $(LIBDIR)/libeic.a  $(LIBDIR)/libstdClib.a 
	@$(RM) -r $(INCDIR)/script/eic
	@cd    EiC; rm -f lib/*.a ; make clobber


# -- Rules to make Extensions
extensions:
	@cd $(ARCHDIR) && make
	@cd $(ASMDIR) && make 
	@cd $(COMPDIR) && make 
	@cd $(ENGINEDIR) && make 
	@cd $(FORMATDIR) && make 
	@cd $(LANGDIR) && make 
	@cd $(OSDIR) && make 
	@cd $(PLUGINDIR) && make 

ext-clean:
	@cd $(ARCHDIR) && make clean
	@cd $(ASMDIR) && make clean
	@cd $(COMPDIR) && make clean
	@cd $(ENGINEDIR) && make clean
	@cd $(FORMATDIR) && make clean
	@cd $(LANGDIR) && make clean
	@cd $(OSDIR) && make clean
	@cd $(PLUGINDIR) && make clean


# -- Rules to make libbastard
$(LIBDIR)/libbastard.a: $(LIB_OBJS)
	$(AR) rcs $@ $^ 
	
$(LIBDIR)/$(LIB_SONAME): $(LIB_OBJS) $(LIBDIR)/libbastard.a \
                          $(LIBDIR)/libeic.a $(LIBDIR)/libstdClib.a \
				  $(LIBDIR)/libtyphoon.a
	@echo _____________________________________________________________
	@echo Compiling libbastard.so...
	$(CC) $(LIB_SOFLAGS) -o $@ $^
	@echo Done with libbastard.so

.c.o: $(SRCS) 
	$(CC) $(LIB_CFLAGS) -o $@ -c $<


# -- Rules to make the bastard
$(BINDIR)/$(PROGRAM):  $(LIBDIR)/$(LIB_SONAME) extensions 
	@cd cli && make
	@ [ -f /etc/debian_version ]  || echo "Real Bastards use Debian!"
	
