#
# FILE:
# src/webbot/Makefile - makefile for the webbots
#
###############################################################################
DIR_ROOT = ../..#                # Set relative path to build root
include ../Make.common                 # include common makefile setups

##############################################################
INC_SSLEAY := $(DIR_SSLEAY_INC)
LIB_SSLEAY := -L$(DIR_SSLEAY_LIB) -lssl -lcrypto
LIB_SOCK := -L$(DIR_SOCK_LIB) -lsocks
##############################################################

# uncomment these to do an SSLeay compile for AIX
# C_INCL := $(DIR_SRC) $(INC_SSLEAY)
# LD_LIBS := $(LIB_SSLEAY) $(LIB_SOCK) -lbsd -lm -lc_r
# DEFS	= -DHAVE_CONFIG_H -DUSE_SSLEAY
# CC := xlC
# CFLAGS  := -g -DAIX  -DXP_UNIX -qinfo -qflag=I:I
# CFLAGS  := $(CFLAGS) -qinlglue -qarch=com -qassert=typ $(DEFS) 

# uncomment these to do a SSLeay compile for Linux
C_INCL := $(C_INCL) $(INC_SSLEAY)
# LD_LIBS := $(LIB_SSLEAY) $(LIB_SOCK) -lpthread -lm
LD_LIBS := -lpthread -lm
# DEFS	= -DHAVE_CONFIG_H -DUSE_SSLEAY -DOTHER_UNIX -D_GNU_SOURCE
DEFS	= -DHAVE_CONFIG_H -DOTHER_UNIX -D_GNU_SOURCE
CC := c++
CFLAGS  := $(CFLAGS) -g -O2 -Wall $(DEFS) 

LD := $(CC)


# common c source files that all of the binaries link with
SRCS_WEBLOAD =				\
		checksum.C		\
		cookie.C		\
		errexit.c		\
		fetchpage.C		\
		fetchurl.C		\
		generic.c		\
		header.C		\
		parse.C			\
		pool.C			\
		request.C		\
		shhopt.c		\
		skit_error.c		\
		socket.C		\
		sslopts.C		\
		super.C			\
		sysdep.c		\
		timefunc.c

SRCS_UPDSHM = 			\
		updshm.c

# My apologies for the complex makefile; this is a rather difficult 
# build environment with a very low level of build automation.

S_CLTOPT = clt-webopts.C  
S_MONOPT = mon-webopts.C  

O_CLTOPT := $(S_CLTOPT:.C=.o)
O_MONOPT := $(S_MONOPT:.C=.o)

R_CLTOPT = rclt-webopts.o  
R_MONOPT = rmon-webopts.o  

S_WEBCLT = webclient.C

O_WEBCLT := $(S_WEBCLT:.C=.o)

R_WEBCLT = rwebclient.o

SRCS_WEBCLT := $(S_WEBCLT) $(SRCS_WEBLOAD)
SRCS_WEBMON := webmon.C $(SRCS_WEBLOAD)

# The "r" versions of the object files are just the usual files,
# but compiled with the -DUSE_SOCKS flag set.  

OBJS_WEBLOAD := $(SRCS_WEBLOAD:.c=.o)
OBJS_WEBLOAD := $(OBJS_WEBLOAD:.C=.o)
OBJS_RWEBLOAD := $(OBJS_WEBLOAD:%=r%)

OBJS_WEBCLT = webclient.o  clt-webopts.o $(OBJS_WEBLOAD)
OBJS_WEBMON = webmon.o     mon-webopts.o $(OBJS_WEBLOAD)

OBJS_RWEBCLT := $(OBJS_WEBCLT:%=r%)
OBJS_RWEBMON := $(OBJS_WEBMON:%=r%)

# create a special suffix to trick the makefile
# into build two objects from a single source
.SUFFIXES: .tag

TAGS_RWEBCLT := $(OBJS_WEBCLT:.o=.tag)
TAGS_RWEBMON := $(OBJS_WEBMON:.o=.tag)

T_WEBCLT := $(O_WEBCLT:.o=.tag)

T_CLTOPT := $(O_CLTOPT:.o=.tag)
T_MONOPT := $(O_MONOPT:.o=.tag)

OBJS_UPDSHM := $(SRCS_UPDSHM:.c=.o)

# the "r" versions are the socks-ified versions, following the 
# usual socks client naming conventions
TARGETS = 						\
			$(DIR_WL_BIN)/webclient		\
			$(DIR_WL_BIN)/webmon		\
			$(DIR_WL_BIN)/updshm		

#			$(DIR_WL_BIN)/rwebmon		\
#			$(DIR_WL_BIN)/rwebclient	\


.PHONY: all clean realclean depend

all:	$(TARGETS)

# A special target, should probably be generalized & put in
# Make.common  This target is special because of the target directory.
$(DIR_WL_BIN)/%: %.o
	$(fancy_line)
	$(LD) $(LDFLAGS) $^ $(LD_LIBS) -o $@

$(DIR_WL_BIN)/rwebclient:	$(OBJS_RWEBCLT)

$(DIR_WL_BIN)/rwebmon:		$(OBJS_RWEBMON)

$(DIR_WL_BIN)/webclient:	$(OBJS_WEBCLT)

$(DIR_WL_BIN)/webmon:		$(OBJS_WEBMON)

$(DIR_WL_BIN)/updshm:		$(OBJS_UPDSHM)


$(O_WEBCLT):
	$(CC) -c webclient.C $(CFLAGS) $(C_INCL:%=-I%) -o $(O_WEBCLT)

$(O_CLTOPT):
	$(CC) -c webopts.C -DWEBCLIENT $(CFLAGS) $(C_INCL:%=-I%) -o $(O_CLTOPT)

$(O_MONOPT):
	$(CC) -c webopts.C -DWEBMON $(CFLAGS) $(C_INCL:%=-I%) -o $(O_MONOPT)


$(T_WEBCLT):
	$(CC) -c webclient.C -DUSE_SOCKS $(CFLAGS) $(C_INCL:%=-I%) -o $(R_WEBCLT)
	touch $@

$(T_CLTOPT):
	$(CC) -c webopts.C -DUSE_SOCKS -DWEBCLIENT $(CFLAGS) $(C_INCL:%=-I%) -o $(R_CLTOPT)
	touch $@

$(T_MONOPT):
	$(CC) -c webopts.C -DUSE_SOCKS -DWEBMON $(CFLAGS) $(C_INCL:%=-I%) -o $(R_MONOPT)
	touch $@


# The "TAG" is a trick to force a second set of objects to be
# built from the same source files, but using different compiler flags.
# The tag is required to get all of the paths to all of the dependency
# and target directories worked out just right.

$(OBJS_RWEBCLT): $(TAGS_RWEBCLT)
$(OBJS_RWEBMON): $(TAGS_RWEBMON)

fancy_line=@echo ------------------------------------------------ target = $(@F)

%.o: %.c
	$(fancy_line)
	$(CC) -c $(CFLAGS) $(C_INCL:%=-I%) -o $(@F) $<


%.tag: %.c
	$(fancy_line)
	$(CC) -c -DUSE_SOCKS $(CFLAGS) $(C_INCL:%=-I%) -o r$(*F).o $<
	touch $@

%.o: %.C
	$(fancy_line)
	$(CC) -c $(CFLAGS) $(C_INCL:%=-I%) -o $(@F) $<

%.tag: %.C
	$(fancy_line)
	$(CC) -c -DUSE_SOCKS $(CFLAGS) $(C_INCL:%=-I%) -o r$(*F).o $<
	touch $@


%.tag: %.C
	$(fancy_line)
	$(CC) -c -DUSE_SOCKS $(CFLAGS) $(C_INCL:%=-I%) -o r$(*F).o $<
	touch $@


clean:
	$(fancy_line)
	$(RM) *.o core tmp junk glop gorp
	$(RM) $(OBJS_UPDSHM) 
	$(RM) $(OBJS_WEBCLT) $(OBJS_WEBMON)
	$(RM) $(OBJS_RWEBCLT) $(OBJS_RWEBMON)
	$(RM) $(TAGS_RWEBCLT) $(TAGS_RWEBMON)

realclean: clean
	$(fancy_line)
	$(RM) $(TARGETS)


depend:
	makedepend -Y $(SRCS_WEBLOAD) $(SRCS_UPDSHM)


# 
# distclean:
# 	$(RM) ../export/perlincl
# 
# dist:	distclean all
# 	$(CP) run.webmon    ../export
# 	$(CP) run.webclient ../export
# 	$(CP) run.workload  ../export
# 	$(CP) README.webmon ../export

# the below were hand-hacked with make depend, which is broken in this make architecture
# DO NOT DELETE THIS LINE -- make depend depends on it.

checksum.o: checksum.h errexit.h generic.h stats.h timefunc.h sysdep.h
checksum.o: super.h webopts.h socket.h
cookie.o: checksum.h cookie.h stats.h timefunc.h sysdep.h webopts.h
cookie.o: errexit.h generic.h parse.h  super.h header.h
errexit.o: sysdep.h
generic.o: sockify.h errexit.h generic.h stats.h timefunc.h sysdep.h
generic.o: webopts.h 
header.o: header.h 
fetchurl.o: fetchurl.h generic.h pool.h super.h header.h request.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h errexit.h
fetchpage.o: fetchpage.h generic.h pool.h super.h header.h request.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h errexit.h
parse.o: generic.h stats.h timefunc.h sysdep.h webopts.h super.h parse.h
pool.o: generic.h pool.h sysdep.h
request.o: request.h generic.h header.h super.h sysdep.h
shhopt.o: shhopt.h sysdep.h
socket.o: sockify.h errexit.h generic.h stats.h timefunc.h sysdep.h webopts.h
socket.o: generic.h pool.h socket.h super.h
sslopts.o: errexit.h generic.h stats.h timefunc.h sysdep.h webopts.h socket.h
sslopts.o: shhopt.h
skit_error.o: skit_error.h
sysdep.o: sysdep.h
super.o: super.h
timefunc.o: sysdep.h timefunc.h

webclient.o: webclient.C
webmon.o: webmon.C

webclient.o: checksum.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h
webclient.o: errexit.h generic.h parse.h header.h super.h pool.h request.h fetchurl.h fetchpage.h 
webmon.o: checksum.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h
webmon.o: errexit.h generic.h parse.h header.h super.h pool.h request.h fetchurl.h fetchpage.h

clt-webopts.o: webopts.h webopts.C cookie.h super.h sysdep.h socket.h generic.h shhopt.h  
mon-webopts.o: webopts.h webopts.C cookie.h super.h sysdep.h socket.h generic.h shhopt.h  

rchecksum.o: checksum.h errexit.h generic.h stats.h timefunc.h sysdep.h
rchecksum.o: super.h webopts.h socket.h
rcookie.o: checksum.h cookie.h stats.h timefunc.h sysdep.h webopts.h
rcookie.o: errexit.h generic.h parse.h  super.h header.h
rerrexit.o: sysdep.h
rgeneric.o: sockify.h errexit.h generic.h stats.h timefunc.h sysdep.h
rgeneric.o: webopts.h 
rfetchurl.o: fetchurl.h generic.h pool.h super.h header.h request.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h errexit.h
rfetchpage.o: fetchpage.h generic.h pool.h super.h header.h request.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h errexit.h
rparse.o: generic.h stats.h timefunc.h sysdep.h webopts.h super.h parse.h
rpool.o: generic.h pool.h sysdep.h
rrequest.o: request.h generic.h header.h super.h sysdep.h
rshhopt.o: shhopt.h sysdep.h
rsocket.o: sockify.h errexit.h generic.h stats.h timefunc.h sysdep.h webopts.h
rsocket.o: generic.h pool.h socket.h super.h
rsslopts.o: errexit.h generic.h stats.h timefunc.h sysdep.h webopts.h socket.h
rsslopts.o: shhopt.h
rsysdep.o: sysdep.h
rtimefunc.o: sysdep.h timefunc.h

rwebclient.o: webclient.C
rwebmon.o: webmon.C

rwebclient.o: checksum.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h
rwebclient.o: errexit.h generic.h parse.h header.h super.h pool.h request.h fetchurl.h fetchpage.h 
rwebmon.o: checksum.h cookie.h stats.h timefunc.h sysdep.h webopts.h socket.h
rwebmon.o: errexit.h generic.h parse.h header.h super.h pool.h request.h fetchurl.h fetchpage.h

rclt-webopts.o: webopts.h webopts.C cookie.h super.h sysdep.h socket.h generic.h shhopt.h  
rmon-webopts.o: webopts.h webopts.C cookie.h super.h sysdep.h socket.h generic.h shhopt.h  
