#
# Screws Server Makefile
#

include ../../CONFIG

BIN=screwsd
# Build Defs
DEFS =-DUSESSL=${SSL} -DPOLL=${POLL} -DVERSION=\"${VERSION}\" 
DEFS+=-DEXEC=\"${EXEC}\" -DCFGFILE=\"${CFGFILE}\" -DPREFIX=\"${PREFIX}\" 
DEFS+=-DDEBUG=${DEBUG} -DIPV6=${IPV6} -DLOGS=${LOGS} -DLOGFILE=\"${LOGFILE}\" 
DEFS+=-DERRFILE=\"${ERRFILE}\" -DDLOPEN=${DLOPEN}
DEFS+=-DMODULE_PATH=\"${MODULE_PATH}\" -DEXEC_PATH=\"${EXEC_PATH}\"
CFLAGS+=${DEFS} -g -shared
# Objects
OBJS =Accept.o Args.o Config.o Engine.o Env.o Error.o Fmutex.o
OBJS+=Logs.o Signal.o Ssl.o Modules.o Request.o main.o Headers.o
OBJS+=IO.o IO_direct.o IO_buffer.o IO_poll.o IO_select.o Socket.o

.PHONY: all opts clean todo

%.o: %.c
	@echo [screwsd] $<
	@gcc $< -c -o $@ ${CFLAGS} ${DEFS}

all:	opts ${OBJS}
	@echo "[L] Linking..."
	@${CC} -L /usr/pkg/lib ${OBJS} `cat .opts` -o ${BIN} && \
	if [ "${DEBUG}"  = "0" ]; then strip ${BIN}; fi
	@echo " *** ${BIN} - BUILD OK"

opts:   
	@echo "[O] Options"
	@printf "" > .opts
	@if [ "${POLL}" = "1" ]; then echo " * USE_POLL" ; fi
	@if [ "${DEBUG}" = "1" ]; then echo " * USE_DEBUG"  ; \
	 echo -g > .opts ; fi
	@if [ "${SSL}" = "1" ]; then echo " * USE_SSL"    ; \
	 case "`uname`" in \
	"NetBSD"|"FreeBSD"|"OpenBSD") \
	 echo -L/usr/pkg/lib -lssl -lcrypto >> .opts ;; \
	*) \
	 echo -lssl >> .opts ; esac ; fi
	@if [ "`uname`" = "Linux" ]; then echo " * USE_DLOPEN" ; \
	 echo -ldl >> .opts ; fi
	@if [ "`uname`" = "syllable" ]; then echo " * USE_DLOPEN" ; \
	 echo -ldl >> .opts ; fi
	@if [ "`uname`" = "SunOS" ]; then \
	 echo -ldl -lsocket -lnsl >> .opts ; fi

clean:
	@echo Cleaning ugly files..
	-rm -f file *.po *.o a.out *~ .[]* .depend .opts ${BIN} core sws.core .*.swp DEADJOE 2> /dev/null
	@echo "[C] Clean OK"

todo:
	grep -e TODO -e XXX *.c *.h
