#
#  Makefile for Socks5++
#

# installation target
BINDIR=/usr/local/bin
LIBDIR=/usr/local/lib/socks5pp

# items to install
TARGETS=socks5pp s5resolver
SCRIPTS=s5dnstls s5host
SCRIPT_MODULES=s5utils.py

# build settings
CPPDEBUG=-ggdb
WARNINGS=-Wno-switch
CPP=g++

# template for C++ builds
.cc.o:
	$(CPP) $(CPPDEBUG) $(CXXFLAGS) $(WARNINGS) -c $<

# the 'all' and 'rebuild' targets
all: $(TARGETS)
rebuild: clean all

# C++ targets
MAIN_OBJS=readhosts.o ipchecks.o rmanager.o connection.o resolver.o addrutils.o popen3.o resname.o s5utils.o
socks5pp: %: %.o $(MAIN_OBJS)
	$(CPP) -o $@ $@.o $(MAIN_OBJS)
RESOLVER_OBJS=resname.o
s5resolver: %: %.o $(RESOLVER_OBJS)
	$(CPP) -o $@ $@.o $(RESOLVER_OBJS)

# clean the build
clean:
	rm -f $(TARGETS) Makefile.bak *.o

# update dependencies
dep depend:
	makedepend -Y *.cc 2>/dev/null

# strip the outputs
strip: all
	strip --strip-unneeded $(TARGETS)

# install target
install: strip
	# install the compiled executables in $(BINDIR)
	for i in $(TARGETS) ; do install -o root -g root -m 0755 $$i $(BINDIR) ; done
	# create the folder for the scripts and modules
	install -o root -g root -m 0755 -d $(LIBDIR)
	# install the scripts in $(LIBDIR)
	for i in $(SCRIPTS) ; do install -o root -g root -m 0755 $$i $(LIBDIR) ; done
	# link the top-level scripts into $(BINDIR)
	for i in $(SCRIPTS) ; do test -e $(BINDIR)/$$i || ln -sv $(LIBDIR)/$$i $(BINDIR)/ ; done
	# install modules in $(LIBDIR)
	for i in $(SCRIPT_MODULES) ; do install -o root -g root -m 0644 $$i $(LIBDIR) ; done

# uninstall target
uninstall:
	# uninstall the compiled binaries
	for i in $(TARGETS) ; do rm -vf $(BINDIR)/$$i ; done
	# uninstall the python scripts and modules (but leave the folder)
	for i in $(SCRIPTS) $(SCRIPT_MODULES) ; do rm -vf $(LIBDIR)/$$i ; done
	# remove the folder
	rmdir -v $(LIBDIR)

# DO NOT DELETE

addrutils.o: ipchecks.h addrutils.h
connection.o: addrutils.h ipchecks.h connection.h
popen3.o: popen3.h
readhosts.o: readhosts.h fastsplit.h ipchecks.h
resname.o: resname.h
resolver.o: addrutils.h ipchecks.h fastsplit.h popen3.h resolver.h
rmanager.o: rmanager.h resolver.h
s5resolver.o: resname.h
s5utils.o: ipchecks.h addrutils.h
socks5pp.o: connection.h addrutils.h ipchecks.h readhosts.h rmanager.h
socks5pp.o: resolver.h s5utils.h getrss.h pool.h
