################ Change following ########################
##  some other parameters exist in opengatemmng.h  ########
##########################################################
CONFIGPATH = /etc/opengate        ## directory of config file
WWWTOP = /usr/local/www/apache24  ## apache directory top(absolute path)
DOCDIR = /data			  ## apache data directory(relative path to wwwtop)
CGIDIR = /cgi-bin		  ## apache cgi directory(relative path to wwtop)
OPENGATEDIR = /opengate		  ## opengate web directory(relative path to docdir)

DOCUMENTROOT = ${WWWTOP}${DOCDIR}
INSTALLDOCPATH = ${WWWTOP}${DOCDIR}${OPENGATEDIR}
INSTALLCGIPATH = ${WWWTOP}${CGIDIR}${OPENGATEDIR}
INSTALLBINPATH = /usr/local/bin   ## install dir of executable file
MAKEDIR != pwd			## save present working directory of MAKE

.if exists(/usr/local/include/mysql)
CFLAGSMYSQL = -I/usr/local/include/mysql -pipe -fno-strict-aliasing
LIBMYSQL = -L/usr/local/lib/mysql -lmysqlclient  -pthread -lz -lm
.endif

.if exists(/usr/local/include/sqlite3.h)
LIBSQLITE = -lsqlite3 -lpthread
.endif

.if exists(/usr/local/include/ldap.h)
LIBLDAP = -lldap -llber
.endif

#LIBCCMALLOC = -lccmalloc -L/usr/local/lib  ## memory leak check tool for debugging

CFLAGS= -g -O3 -Wall -I/usr/local/include ${CFLAGSMYSQL}

LIBS = -lezxml -lssl -lcrypto -lradius -lpam -L../ezxml -L/usr/local/lib ${LIBLDAP} ${LIBSQLITE} ${LIBMYSQL} ${LIBCCMALLOC}

OBJS = util.o error.o getparam.o managementdb.o workdb.c getmac.o cgi.o messages.o queue.o ipfw.o proc.o auth.o auth-pam.o auth-ftps.o auth-pop3s.o auth-rad.o auth-ldap.o alarms.o udpcli.o signal.o
HDRS = opengatemmng.h

CHKPROGO = opengatemchk.o
CHKPROG = opengatemchk
REGPROGO = opengatemreg.o
REGPROG = opengatemreg
UPPROGO = opengatemup.o
UPPROG = opengatemup
MAILPROGO = opengatemmail.o
MAILPROG = opengatemmail
OWNPROGO = opengatemown.o
OWNPROG = opengatemown
FWDPROGO = opengatemfwd.o
FWDPROG = opengatemfwd

LIBOPT = ezxml

CLEANFILES = *.o *~ *.core a.out \\#*

all:		${CHKPROG} ${REGPROG} ${UPPROG} ${OWNPROG} ${MAILPROG} ${FWDPROG} ${LIBOPT}

opengatemchk:	${OBJS} ${CHKPROGO} ${LIBOPT}
		${CC} ${CFLAGS} -o $@ ${OBJS} ${CHKPROGO} ${LIBS}

opengatemreg:	${OBJS} ${REGPROGO} ${LIBOPT}
		${CC} ${CFLAGS} -o $@ ${OBJS} ${REGPROGO} ${LIBS}

opengatemup:	${OBJS} ${UPPROGO} ${LIBOPT}
		${CC} ${CFLAGS} -o $@ ${OBJS} ${UPPROGO} ${LIBS}

opengatemmail:	${OBJS} ${MAILPROGO} ${LIBOPT}
		${CC} ${CFLAGS} -o $@ ${OBJS} ${MAILPROGO} ${LIBS}

opengatemown:	${OBJS} ${OWNPROGO} ${LIBOPT}
		${CC} ${CFLAGS} -o $@ ${OBJS} ${OWNPROGO} ${LIBS}

opengatemfwd:	${OBJS} ${FWDPROGO} ${LIBOPT}
		${CC} ${CFLAGS} -o $@ ${OBJS} ${FWDPROGO} ${LIBS}

ezxml:	
		${MAKE} -C ../ezxml

clean:
		rm -f ${CHKPROG} ${REGPROG} ${UPPROG} ${OWNPROG} ${FWDPROG} ${MAILPROG} ${CLEANFILES}
		${MAKE} clean -C ../ezxml
		rm -f ../*/*~  ../*~ ../*/*/*~

# install mac registration and upating systems
install:	install-chkcgi install-regcgi install-upcgi install-owncgi install-fwdcgi install-mailbin install-conf install-html

#install only mac registration system
installmreg:	install-chkcgi install-regcgi install-conf install-html

#install only mac updating system
installmup:	install-upcgi install-mailbin install-conf install-html

#install only mac reg/update by owner oneself
installmown:	install-owncgi install-fwdcgi install-mailbin install-conf install-html

install-chkcgi:	${CHKPROG}
		## Copy CGI program and Set S bit ##
.if !exists(${INSTALLCGIPATH})
		mkdir ${INSTALLCGIPATH}
.endif
		cp ${CHKPROG} ${INSTALLCGIPATH}/${CHKPROG}.cgi
		chmod 4755 ${INSTALLCGIPATH}/${CHKPROG}.cgi

install-regcgi:	${REGPROG}
		## Copy CGI program and Set S bit ##
.if !exists(${INSTALLCGIPATH})
		mkdir ${INSTALLCGIPATH}
.endif
		cp ${REGPROG} ${INSTALLCGIPATH}/${REGPROG}.cgi
		chmod 4755 ${INSTALLCGIPATH}/${REGPROG}.cgi

install-upcgi:	${UPPROG}
		## Copy CGI program and Set S bit ##
.if !exists(${INSTALLCGIPATH})
		mkdir ${INSTALLCGIPATH}
.endif
		cp ${UPPROG} ${INSTALLCGIPATH}/${UPPROG}.cgi
		chmod 4755 ${INSTALLCGIPATH}/${UPPROG}.cgi

install-owncgi:	${OWNPROG}
		## Copy CGI program and Set S bit ##
.if !exists(${INSTALLCGIPATH})
		mkdir ${INSTALLCGIPATH}
.endif
		cp ${OWNPROG} ${INSTALLCGIPATH}/${OWNPROG}.cgi
		chmod 4755 ${INSTALLCGIPATH}/${OWNPROG}.cgi

install-fwdcgi:	${FWDPROG}
		## Copy CGI program and Set S bit ##
.if !exists(${INSTALLCGIPATH})
		mkdir ${INSTALLCGIPATH}
.endif
		cp ${FWDPROG} ${INSTALLCGIPATH}/${FWDPROG}.cgi
		chmod 4755 ${INSTALLCGIPATH}/${FWDPROG}.cgi

install-mailbin:	${MAILPROG}
.if !exists(${INSTALLBINPATH})
		mkdir ${INSTALLBINPATH}
.endif
		cp ${MAILPROG} ${INSTALLBINPATH}/${MAILPROG}
		chmod 4755 ${INSTALLBINPATH}/${MAILPROG}

install-html:
		## Copy HTML documents and etc ##
.if !exists(${INSTALLDOCPATH})
		mkdir ${INSTALLDOCPATH}
.endif
		cp -R ../html/* ${INSTALLDOCPATH}
		mv ${INSTALLDOCPATH}/index.html.var ${DOCUMENTROOT}/index.html.var.opengatem

install-conf:
		## Copy Config files ##
.if !exists(${CONFIGPATH})
		mkdir ${CONFIGPATH}
.endif
		cp  ../conf/opengatemmng.conf.sample ${CONFIGPATH}
		cp  ../conf/warningmail.sample ${CONFIGPATH}
		cp  ../conf/warningmail.sample.en ${CONFIGPATH}
		#
		#-------------------------------------------------------#
		# COPY xx.conf.sample to xx and EDIT it.                #
		#  Eg. 'cp opengatemmng.conf.sample  opengatemmng.conf' #
		#      'vi opengatemmng.conf'                           #
		#-------------------------------------------------------#

opengatemchk.o:	${HDRS}
		${CC} ${CFLAGS} -DMAKEDIR='"${MAKEDIR}"' -c $<

opengatemreg.o:	${HDRS}
		${CC} ${CFLAGS} -DMAKEDIR='"${MAKEDIR}"' -c $<

opengatemup.o:	${HDRS}
		${CC} ${CFLAGS} -DMAKEDIR='"${MAKEDIR}"' -c $<

opengatemmail.o:	${HDRS}
		${CC} ${CFLAGS} -DMAKEDIR='"${MAKEDIR}"' -c $<

opengatemown.o:	${HDRS}
		${CC} ${CFLAGS} -DMAKEDIR='"${MAKEDIR}"' -c $<

opengatemfwd.o:	${HDRS}
		${CC} ${CFLAGS} -DMAKEDIR='"${MAKEDIR}"' -c $<

util.o:		${HDRS}

mamagementdb.o:	${HDRS}
.if !exists(/usr/local/include/mysql)
		${CC} ${CFLAGS} -DMYSQL_NOT_INSTALLED -c $<
.endif

workdb.o:	${HDRS}
.if !exists(/usr/local/include/sqlite3.h)
		${CC} ${CFLAGS} -DSQLITE3_NOT_INSTALLED -c $<
.endif

error.o:	${HDRS}

getparam.o:	${HDRS}

messages.o:	${HDRS}

cgi.o:		${HDRS}

getmac.o:	${HDRS}

queue.o:	${HDRS}

ipfw.o:		${HDRS}

proc.o:		${HDRS}

auth.o:		${HDRS}

auth-pam.o:	${HDRS}

auth-ftps.o:	${HDRS}

auth-pop3s.o:	${HDRS}

auth-rad.o:	${HDRS}

auth-ldap.o:	${HDRS}
.if !exists(/usr/local/include/ldap.h)
		${CC} ${CFLAGS} -DLDAP_NOT_INSTALLED -c $<
.endif

alarms.o:	${HDRS}

udpcli.o:	${HDRS}
