########################################################################
#                                                                      #
#      RAGMAAN Makefile                                                #
#                                                                      #
#      "make ragmaan" run as root will:                                #
#                                                                      #
#      - build everything                                              #
#      - copy everything to /usr/local/ragmaan-x.y.z/                  #
#      - create a link /usr/local/bin/ragmaan to the startup script    #
#                                                                      #
#      "make" run as a mortal user will:                               #
#                                                                      #
#      - build everything (run bin/ragmaan to see if it worked)        #
#                                                                      #
#      other targets:                                                  #
#                                                                      #
#      make clean:          cleanup object files and temp files        #
#      make veryclean:      also cleanup scripts and executables       #
#      make install:        just copy & symlink (only after a build)   #
#      make uninstall:      remove installed files                     #
#                                                                      #
########################################################################

export INSTALL_PREFIX = /usr/local/
export INSTALL_DIR    = $(INSTALL_PREFIX)ragmaan-1.0.3/
export INSTALL_BIN    = $(INSTALL_PREFIX)bin/

export WISH      = $(shell which wish)
export TCLSH     = $(shell which tclsh)
export TOP_DIR   = $(shell pwd)/
export GZIP_EXE  = $(shell which gzip)
export DB        = nl2.txt.gz

# EXPLICIT_RESOURCE_READ makes ragmaan read X resources from 
# db/ragmaan-defaults and $HOME/.ragmaan-defaults at startup.
# Remove this if you really want to use xrdb

export EXPLICIT_RESOURCE_READ

export LIB_PREFIX = lib/
export DOC_PREFIX = doc/
export SRC_PREFIX = source/
export BIN_PREFIX = bin/
export DB_PREFIX  = db/

export LIB_DIR = $(TOP_DIR)$(LIB_PREFIX)
export DOC_DIR = $(TOP_DIR)$(DOC_PREFIX) 
export SRC_DIR = $(TOP_DIR)$(SRC_PREFIX)
export BIN_DIR = $(TOP_DIR)$(BIN_PREFIX)
export DB_DIR  = $(TOP_DIR)$(DB_PREFIX)

export LIB_NAME    = librm.so.2.0.1
export SERVER_NAME = rm_server
export SUP_NAME    = ragmaan
export STARTUP     = $(BIN_DIR)$(SUP_NAME)

export SPLICE_EXE = $(BIN_DIR)rm_filter
export LIB        = $(LIB_DIR)$(LIB_NAME)
export SERVER_EXE = $(BIN_DIR)$(SERVER_NAME)
export SUP_SCRIPT = $(STARTUP)

DEFS += -DNDEBUG
DEFS += -DDB=\"$(DB_DIR)$(DB)\"
DEFS += -DGZIP_EXE=\"$(GZIP_EXE)\"
CFLAGS  = -I. -I/usr/include/tcl -Wall $(DEFS)
CFLAGS += -O3 -fomit-frame-pointer
#CFLAGS += -g

CC      = gcc
LD      = ld
LDFLAGS = -s

########################################################################
#                                                                      #
#       No need to change anything below, unless I                     #
#       made a boo-boo ...                                             #
#                                                                      #
########################################################################

all: $(SERVER_EXE) $(LIB) $(SPLICE_EXE) scripts

.PHONY : all clean veryclean scripts

SERVER_OBJ    = server.o ipc.o cache.o hash_func.o search.o db_files.o
FRONTEND_OBJ  = server_frontend.o ipc.o glue.o hash_func.o state_stack.o
SPLICE_OBJ    = rm_filter.o 

VPATH = $(SRC_DIR)

$(LIB): $(FRONTEND_OBJ)
	$(LD) -shared $(LDFLAGS) -o $(LIB) $(FRONTEND_OBJ)

$(SERVER_EXE): $(SERVER_OBJ)
	$(CC) $(LDFLAGS) -o $(SERVER_EXE) $(SERVER_OBJ) 

$(SPLICE_EXE): $(SPLICE_OBJ)
	$(CC) $(LD_LDFLAGS) -o $(SPLICE_EXE) $(SPLICE_OBJ)

scripts:
	$(TCLSH) $(LIB_DIR)make_sup.tcl

veryclean: clean
	-rm -rf $(LIB) $(SPLICE_EXE) $(SUP_SCRIPT) $(SRC_DIR)/tags \
	       $(SERVER_EXE)
clean:
	-rm -rf $(LIB_OBJ) $(SERVER_OBJ) $(SPLICE_OBJ) $(FRONTEND_OBJ) core

ragmaan: all clean install

install: 
	mkdir -p $(INSTALL_DIR); \
	cp -R $(TOP_DIR)* $(INSTALL_DIR); \
	rm -rf $(INSTALL_DIR)$(BIN_PREFIX)$(SUP_NAME); \
	cat $(STARTUP) | sed -e \
	 s@^RM_TOP_DIR.\*\$$@RM_TOP_DIR=$$INSTALL_DIR@ > \
	 $(INSTALL_DIR)$(BIN_PREFIX)$(SUP_NAME); \
	chmod +x $(INSTALL_DIR)$(BIN_PREFIX)$(SUP_NAME); \
	chmod -R og-w $(INSTALL_DIR) ; \
	mkdir -p $(INSTALL_BIN); \
	rm -rf $(INSTALL_BIN)$(SUP_NAME); \
	ln -s  $(INSTALL_DIR)$(BIN_PREFIX)$(SUP_NAME) $(INSTALL_BIN)

uninstall:
	-rm -rf $(INSTALL_DIR) $(INSTALL_BIN)$(SUP_NAME);

client.o: ipc.h server_commands.h
ipc.o: ipc.h server_commands.h
server.o: cache.h db_files.h hash_func.h ipc.h ragmaan.h search.h \
	  server_commands.h
db_files.o: db_files.h hash_func.h ragmaan.h search.h
hash_func.o: hash_func.h ragmaan.h
rm_filter.o: 
search.o: hash_func.h ragmaan.h search.h
timer.o: ragmaan.h timer.h
ragmaan_test.o: db_files.h hash_func.h ragmaan.h search.h timer.h
server_frontend.o: glue.h hash_func.h ipc.h ragmaan.h search.h \
	           server_commands.h server_frontend.h
glue.o: glue.h hash_func.h ipc.h ragmaan.h search.h server_commands.h \
	server_frontend.h
cache.o: cache.h ragmaan.h
state_stack.o: glue.h search.h hash_func.h ragmaan.h
