VPATH   = @srcdir@
srcdir  = @srcdir@

CC 	= @CC@
CFLAGS	= @CFLAGS@ @DEFS@

INSTALL	= @INSTALL@
RANLIB	= @RANLIB@
BINRM   = @BINRM@
TOUCH	= touch
SHELL   = /bin/sh

SUBDIRS  = lib clients server include shlib
CSUBDIRS = lib clients

AUTOCONF = @AUTOCONF@
AUTOHDR  = @AUTOHDR@

prefix      = @prefix@
exec_prefix = @exec_prefix@
mandir      = $(prefix)/man

all:	Makefile server lib clients shlib

Makefile: ./config.status $(srcdir)/Makefile.in 
	CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status

include/config.h: stamp-h

stamp-h: $(srcdir)/include/config.h.in ./config.status 
	CONFIG_FILES=stamp-h CONFIG_HEADERS=include/config.h ./config.status

config.status: configure 
	./config.status --recheck

include/configure: configure.in aclocal.m4 
	cd ${srcdir} && $(AUTOCONF)

include/config.h.in: stamp-h.in

$(srcdir)/stamp-h.in: configure.in aclocal.m4 acconfig.h 
	cd ${srcdir} && $(AUTOHDR)
	date > ${srcdir}/stamp-h.in

.PHONY : install
install:: all clients.install server.install lib.install shlib.install

##
# Client stuff.
##
.PHONY : clients
clients::
	@for i in $(CSUBDIRS); do ( cd $$i ; $(MAKE) all ); done

.PHONY : util.install
util.install:
	@(cd lib; $(MAKE) util.install)

.PHONY : clients.install
clients.install:
	@( if [ ! -d $(exec_prefix)/bin ]; then mkdir -p $(exec_prefix)/bin; fi )
	@for i in $(CSUBDIRS); do ( cd $$i ; $(MAKE) install ); done

.PHONY : clients.clean
clients.clean:
	@( cd clients; $(MAKE) clean )

##
# Server stuff.
##
.PHONY : server
server::
	@( cd server; $(MAKE) all )

.PHONY : server.install
server.install:
	@( if [ ! -d $(mandir)/man1 ]; then mkdir -p $(mandir)/man1; fi )
	@( if [ ! -d $(mandir)/man5 ]; then mkdir -p $(mandir)/man5; fi )
	@( if [ ! -d $(exec_prefix)/bin ]; then mkdir -p $(exec_prefix)/bin; fi )
	$(INSTALL) $(srcdir)/man/man5/socks5.conf.5 $(mandir)/man5
	$(INSTALL) $(srcdir)/man/man5/socks5.passwd.5 $(mandir)/man5
	$(INSTALL) $(srcdir)/man/man1/stopsocks.1 $(mandir)/man1
	$(INSTALL) $(srcdir)/man/man1/socks5.1 $(mandir)/man1
	@( cd server; $(MAKE) install )

.PHONY : server.clean
server.clean:
	@( cd server; $(MAKE) clean )

##
# Utility library stuff.
##
.PHONY : util
util:
	@(cd lib; $(MAKE) util)

##
# Shared library stuff.
##
.PHONY : shlib
shlib::
	@(cd shlib; $(MAKE) all)

.PHONY : shlib.install
shlib.install:
	@( if [ ! -d $(mandir)/man1 ]; then mkdir -p $(mandir)/man1; fi )
	@( if [ ! -d $(exec_prefix)/bin ]; then mkdir -p $(exec_prefix)/bin; fi )
	$(INSTALL) $(srcdir)/man/man1/runsocks.1 $(mandir)/man1
	@(cd shlib; $(MAKE) install)

.PHONY : shlib.clean
shlib.clean:
	@( cd shlib; $(MAKE) clean )


##
# Normal library stuff.
##
.PHONY : lib
lib::
	@(cd lib; $(MAKE) all)

.PHONY : lib.install
lib.install:
	@( if [ ! -d $(mandir)/man5 ]; then mkdir -p $(mandir)/man5; fi )
	$(INSTALL) $(srcdir)/man/man5/libsocks5.conf.5 $(mandir)/man5
	$(INSTALL) $(srcdir)/man/man1/socks5_clients.1 $(mandir)/man1
	@(cd include; $(MAKE) install)
	@(cd lib; $(MAKE) install)

.PHONY : lib.clean
lib.clean:
	@( cd lib; $(MAKE) clean )

tags:
	ctags -d -t include/* lib/*.c lib/*.h server/*.c server/*.h
	cd server; make tags
	cd lib; make tags

.PHONY : depend
depend:
	@for i in shlib lib server; do ( cd $$i ; $(MAKE) depend ); done

.PHONY : clean
clean:
	@for i in $(SUBDIRS); do ( cd $$i ; $(MAKE) clean ); done

.PHONY : distclean
distclean: clean
	@for i in $(SUBDIRS); do ( cd $$i ; $(MAKE) distclean ); done
	-$(BINRM) Makefile config.status config.cache config.log stamp-h tags *~ #*# *.bak

