#
# 3 proxy Makefile for GCC/Linux/Cygwin
#
#
# remove -DNOODBC from CFLAGS and add -lodbc to LIBS to compile with ODBC
# library support. Add -DSAFESQL for poorely written ODBC library / drivers.

BUILDDIR = ../bin/
CC = mips-openwrt-linux-gcc

CFLAGS ?= -g -O2 -fno-strict-aliasing -c -pthread -DGETHOSTBYNAME_R -D_THREAD_SAFE -D_REENTRANT -DNOODBC  -DFD_SETSIZE=4096 -DWITH_POLL -DWITH_NETFILTER
COUT = -o 
LN = $(CC)
DCFLAGS = -fPIC
LDFLAGS ?= -O2 -fno-strict-aliasing -pthread -s
DLFLAGS = -shared
DLSUFFICS = .ld.so
# -lpthreads may be reuqired on some platforms instead of -pthreads
LIBSPREFIX = -l
LIBSSUFFIX = 
LNOUT = -o 
EXESUFFICS =
OBJSUFFICS = .o
DEFINEOPTION = -D
COMPFILES = *~
REMOVECOMMAND = rm -f
AFTERCLEAN = (find . -type f -name "*.o" -delete && find src/ -type f -name "Makefile.var" -delete && find bin/ -type f -executable -delete) || true
TYPECOMMAND = cat
COMPATLIBS =
MAKEFILE = Makefile.openwrt-mips
# PamAuth requires libpam, you may require pam-devel package to be installed
# SSLPlugin requires  -lcrypto -lssl
#LIBS = -lcrypto -lssl -ldl 
LIBS ?= -ldl 
#PLUGINS = SSLPlugin StringsPlugin TrafficPlugin PCREPlugin TransparentPlugin PamAuth
PLUGINS ?= StringsPlugin TrafficPlugin TransparentPlugin
OPENSSL_CHECK = $(shell echo "\#include <openssl/ssl.h>\\n int main(){return 0;}" | tr -d \\\\ | cc -x c $(CFLAGS) $(LDFLAGS) -l crypto -l ssl -o testssl - 2>/dev/null && rm testssl && echo true||echo false)
ifeq ($(OPENSSL_CHECK), true)
    LIBS += -l crypto -l ssl
    PLUGINS += SSLPlugin
endif
PCRE_CHECK = $(shell echo "\#define PCRE2_CODE_UNIT_WIDTH 8\\n\#include <pcre2.h>\\n int main(){return 0;}" | tr -d \\\\ | cc -x c $(CFLAGS) $(LDFLAGS) -l pcre2-8 -o testpcre - 2>/dev/null && rm testpcre && echo true||echo false)
ifeq ($(PCRE_CHECK), true)
    PLUGINS += PCREPlugin
endif
PAM_CHECK = $(shell echo "\#include <security/pam_appl.h>\\n int main(){return 0;}" | tr -d \\\\ | cc -x c $(CFLAGS) $(LDFLAGS) -l pam -o testpam - 2>/dev/null && rm testpam && echo true||echo false)
ifeq ($(PAM_CHECK), true)
    PLUGINS += PamAuth
endif

include Makefile.inc

allplugins:
	@list='$(PLUGINS)'; for p in $$list; do cp Makefile Makefile.var plugins/$$p; cd plugins/$$p ; make ;	cd ../.. ; done

DESTDIR		=
prefix		= /usr/local
exec_prefix	= $(prefix)
man_prefix	= $(prefix)/share

INSTALL		= /usr/bin/install
INSTALL_BIN	= $(INSTALL) -m 755
INSTALL_DATA	= $(INSTALL) -m 644
INSTALL_OBJS	= src/3proxy \
		  src/ftppr \
		  src/mycrypt \
		  src/pop3p \
		  src/proxy \
		  src/socks \
		  src/tcppm \
		  src/udppm
		  

INSTALL_CFG_OBJS = scripts/3proxy.cfg \
		   scripts/add3proxyuser.sh
INSTALL_CFG_DEST = config

INSTALL_CFG_OBJS2 = passwd counters bandlimiters

MANDIR1		= $(DESTDIR)$(man_prefix)/man/man1
MANDIR3		= $(DESTDIR)$(man_prefix)/man/man3
MANDIR8		= $(DESTDIR)$(man_prefix)/man/man8
BINDIR		= $(DESTDIR)$(exec_prefix)/bin
ETCDIR		= $(DESTDIR)$(prefix)/etc/3proxy

install-bin:
	$(INSTALL_BIN) -d $(BINDIR)
	$(INSTALL_BIN) -s $(INSTALL_OBJS) $(BINDIR)

install-etc-dir:
	$(INSTALL_BIN) -d $(ETCDIR)

install-etc-default-config:
	if [ -f $(ETCDIR)/$(INSTALL_CFG_DEST) ]; then \
	   : ; \
	else \
	   $(INSTALL_DATA) $(INSTALL_CFG_OBJS) $(ETCDIR)/$(INSTALL_CFG_DEST) \
	fi

install-etc: install-etc-dir
	for file in $(INSTALL_CFG_OBJS2); \
	do \
	  touch $(ETCDIR)/$$file; chmod 0600 $(ETCDIR)/$$file; \
	done;

install-man:
	$(INSTALL_BIN) -d $(MANDIR3)
	$(INSTALL_BIN) -d $(MANDIR8)
	$(INSTALL_DATA) man/*.3 $(MANDIR3)
	$(INSTALL_DATA) man/*.8 $(MANDIR8)

install: install-bin install-etc install-man

