# @configure_input@
#***********************************************************************
#
# Makefile
#
# Makefile for Roaring Penguin's Linux user-space PPPoE client.
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
# Copyright (C) 2018-2021 Dianne Skoll
#
# This program may be distributed according to the terms of the GNU
# General Public License, version 2 or (at your option) any later version.
#
# LIC: GPL
#
# $Id$
#***********************************************************************

# Version is set ONLY IN THE MAKEFILE!  Don't delete this!
RP_VERSION=3.15

DEFINES=
prefix=@prefix@
exec_prefix=@exec_prefix@
mandir=@mandir@
docdir=@prefix@/share/doc/rp-pppoe-$(RP_VERSION)
install=@INSTALL@
install_dir=@INSTALL@ -d
sbindir=@sbindir@
datarootdir=@datarootdir@

# Plugin for pppd on Linux
LINUX_KERNELMODE_PLUGIN=@LINUX_KERNELMODE_PLUGIN@
PPPD_INCDIR=@PPPD_INCDIR@

# Licensed version
LIC_INCDIR=@LIC_INCDIR@
LIC_LIBDIR=@LIC_LIBDIR@
LIC_LIB=@LIC_LIB@
LIC_DEFINE=@LIC_DEFINE@

# PPPoE relay -- currently only supported on Linux
PPPOE_RELAY=@PPPOE_RELAY@

# Program paths
PPPOE_PATH=$(sbindir)/pppoe
PPPD_PATH=@PPPD@

# Include ServPoET version if we're building for ServPoET
@LIC_MAKEFILE_INCLUDE@

# Kernel-mode plugin gets installed here.
PLUGIN_DIR=/etc/ppp/plugins
PLUGIN_PATH=$(PLUGIN_DIR)/rp-pppoe.so

# Configuration file paths
PPPOESERVER_PPPD_OPTIONS=/etc/ppp/pppoe-server-options

PATHS='-DPPPOE_PATH="$(PPPOE_PATH)"' '-DPPPD_PATH="$(PPPD_PATH)"' \
	'-DPLUGIN_PATH="$(PLUGIN_PATH)"' \
	'-DPPPOE_SERVER_OPTIONS="$(PPPOESERVER_PPPD_OPTIONS)"'

CFLAGS= @CFLAGS@ $(LIC_INCDIR) $(DEFINES) $(LIC_DEFINE) $(PATHS) -Ilibevent
TARGETS=@TARGETS@
PPPOE_SERVER_LIBS=$(LIC_LIBDIR) $(LIC_LIB)
LDFLAGS= @LDFLAGS@

all: $(TARGETS)
	@echo ""
	@echo "Type 'make install' as root to install the software."

pppoe-sniff: pppoe-sniff.o if.o common.o debug.o
	@CC@ -o $@ $^ $(LDFLAGS)

pppoe-server: pppoe-server.o if.o debug.o common.o md5.o libevent/libevent.a @PPPOE_SERVER_DEPS@
	@CC@ -o $@ @RDYNAMIC@ $^ $(LDFLAGS) $(PPPOE_SERVER_LIBS) -Llibevent -levent

# Experimental code from Savoir Faire Linux.  I do not consider it
# production-ready, so not part of the official distribution.
#pppoe-bridge: pppoe-bridge.o ppp.o modem.o rs232.o if.o debug.o common.o md5.o libevent/libevent.a @PPPOE_SERVER_DEPS@
#	@CC@ -o $@ @RDYNAMIC@ $^ $(LDFLAGS) $(PPPOE_SERVER_LIBS) -Llibevent -levent

pppoe: pppoe.o if.o debug.o common.o ppp.o discovery.o
	@CC@ -o $@ $^ $(LDFLAGS)

pppoe-relay: relay.o if.o debug.o common.o
	@CC@ -o $@ $^ $(LDFLAGS)

pppoe.o: pppoe.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

discovery.o: discovery.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

ppp.o: ppp.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

md5.o: md5.c md5.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

pppoe-server.o: pppoe-server.c pppoe.h @PPPOE_SERVER_DEPS@
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

# Experimental code from Savoir Faire Linux.  I do not consider it
# production-ready, so not part of the official distribution.
#pppoe-bridge.o: pppoe-bridge.c pppoe.h @PPPOE_SERVER_DEPS@
#	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

pppoe-sniff.o: pppoe-sniff.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

if.o: if.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

libevent/libevent.a:
	cd libevent && $(MAKE) DEFINES="$(DEFINES)"

common.o: common.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

debug.o: debug.c pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

relay.o: relay.c relay.h pppoe.h
	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

# Experimental code from Savoir Faire Linux.  I do not consider it
# production-ready, so not part of the official distribution.
#modem.o: modem.c modem.h
#	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<

#rs232.o: rs232.c rs232.h
#	@CC@ $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -c -o $@ $<


# Linux-specific plugin
rp-pppoe.so: plugin/plugin.o plugin/libplugin.a
	@CC@ -o $@ -shared $^ $(LDFLAGS)

plugin/plugin.o: plugin.c
	@CC@ -DPLUGIN=1 '-DRP_VERSION="$(RP_VERSION)"' $(CFLAGS) -I$(PPPD_INCDIR) -c -o $@ -fPIC $<

plugin/libplugin.a: plugin/discovery.o plugin/if.o plugin/common.o plugin/debug.o
	ar -rc $@ $^

plugin/discovery.o: discovery.c
	@CC@ -DPLUGIN=1 $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -I$(PPPD_INCDIR) -c -o $@ -fPIC $<

plugin/if.o: if.c
	@CC@ -DPLUGIN=1 $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -I$(PPPD_INCDIR) -c -o $@ -fPIC $<

plugin/debug.o: debug.c
	@CC@ -DPLUGIN=1 $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -I$(PPPD_INCDIR) -c -o $@ -fPIC $<

plugin/common.o: common.c
	@CC@ -DPLUGIN=1 $(CFLAGS) '-DRP_VERSION="$(RP_VERSION)"' -I$(PPPD_INCDIR) -c -o $@ -fPIC $<

install: all
	-mkdir -p $(DESTDIR)$(sbindir)
	$(install) -m 755 pppoe $(DESTDIR)$(sbindir)
	$(install) -m 755 pppoe-server $(DESTDIR)$(sbindir)
	if test -x licensed-only/pppoe-server-control ; then $(install) -m 755 licensed-only/pppoe-server-control $(DESTDIR)$(sbindir); fi
	if test -x pppoe-relay ; then $(install) -m 755 pppoe-relay $(DESTDIR)$(sbindir); fi
	if test -x pppoe-sniff; then $(install) -m 755 pppoe-sniff $(DESTDIR)$(sbindir); fi
	$(install) -m 755 ../scripts/pppoe-connect $(DESTDIR)$(sbindir)
	$(install) -m 755 ../scripts/pppoe-start $(DESTDIR)$(sbindir)
	$(install) -m 755 ../scripts/pppoe-status $(DESTDIR)$(sbindir)
	$(install) -m 755 ../scripts/pppoe-stop $(DESTDIR)$(sbindir)
	$(install) -m 755 ../scripts/pppoe-setup $(DESTDIR)$(sbindir)
	-mkdir -p $(DESTDIR)$(docdir)
	$(install) -m 644 ../doc/CHANGES $(DESTDIR)$(docdir)
	$(install) -m 644 ../doc/KERNEL-MODE-PPPOE $(DESTDIR)$(docdir)
	$(install) -m 644 ../doc/HOW-TO-CONNECT $(DESTDIR)$(docdir)
	$(install) -m 644 ../doc/LICENSE $(DESTDIR)$(docdir)
	$(install) -m 644 ../README $(DESTDIR)$(docdir)
	$(install) -m 644 ../SERVPOET $(DESTDIR)$(docdir)
	$(install) -m 644 ../configs/pap-secrets $(DESTDIR)$(docdir)
	-mkdir -p $(DESTDIR)$(mandir)/man8
	for i in $(TARGETS) ; do \
		if test -f ../man/$$i.8 ; then \
			$(install) -m 644 ../man/$$i.8 $(DESTDIR)$(mandir)/man8 || exit 1; \
		fi; \
	done
	$(install) -m 644 ../man/pppoe-start.8 $(DESTDIR)$(mandir)/man8
	$(install) -m 644 ../man/pppoe-stop.8 $(DESTDIR)$(mandir)/man8
	$(install) -m 644 ../man/pppoe-status.8 $(DESTDIR)$(mandir)/man8
	$(install) -m 644 ../man/pppoe-connect.8 $(DESTDIR)$(mandir)/man8
	$(install) -m 644 ../man/pppoe-setup.8 $(DESTDIR)$(mandir)/man8
	-mkdir -p $(DESTDIR)$(mandir)/man5
	$(install) -m 644 ../man/pppoe.conf.5 $(DESTDIR)$(mandir)/man5
	-mkdir -p $(DESTDIR)/etc/ppp
	-mkdir -p $(DESTDIR)$(PLUGIN_DIR)
	-echo "# Directory created by rp-pppoe for kernel-mode plugin" > $(DESTDIR)$(PLUGIN_DIR)/README
	if test -r rp-pppoe.so; then $(install) -m 755 rp-pppoe.so $(DESTDIR)$(PLUGIN_DIR); fi
	@for i in pppoe.conf firewall-standalone firewall-masq ; do \
		if [ ! -f $(DESTDIR)/etc/ppp/$$i ] ; then \
			$(install) -m 644 ../configs/$$i $(DESTDIR)/etc/ppp ; \
		else \
			echo "NOT overwriting existing $(DESTDIR)/etc/ppp/$$i" ;\
			$(install) -m 644 ../configs/$$i $(DESTDIR)/etc/ppp/$$i-$(RP_VERSION) ;\
		fi ;\
	done
	@if [ ! -f $(DESTDIR)$(PPPOESERVER_PPPD_OPTIONS) ] ; then \
		$(install) -m 644 ../configs/pppoe-server-options $(DESTDIR)$(PPPOESERVER_PPPD_OPTIONS) ; \
	else \
		echo "NOT overwriting existing $(DESTDIR)$(PPPOESERVER_PPPD_OPTIONS)"; \
		$(install) -m 644 ../configs/pppoe-server-options $(DESTDIR)$(PPPOESERVER_PPPD_OPTIONS)-example ; \
	fi
	@if [ -f /etc/redhat-release ] ; then \
		echo "Looks like a Red Hat system; installing $(DESTDIR)/etc/rc.d/init.d/pppoe" ; \
		mkdir -p $(DESTDIR)/etc/rc.d/init.d ;\
		$(install) -m 755 ../scripts/pppoe-init $(DESTDIR)/etc/rc.d/init.d/pppoe ; \
	fi
	@if [ -f /etc/SuSE-release ] ; then \
		echo "Looks like a SuSE Linux system; installing $(DESTDIR)/etc/rc.d/init.d/pppoe" ; \
		mkdir -p $(DESTDIR)/etc/rc.d/init.d ;\
		$(install) -m 755 ../scripts/pppoe-init-suse $(DESTDIR)/etc/rc.d/init.d/pppoe ; \
	fi

	@if [ -f l2tp/handlers/sync-pppd.so ] ; then \
		mkdir -p $(DESTDIR)/usr/lib/l2tp/plugins ; \
		$(install) -m 755 l2tp/handlers/sync-pppd.so $(DESTDIR)/usr/lib/l2tp/plugins ; \
		mkdir -p $(DESTDIR)/etc/l2tp ; \
		$(install) -m 600 l2tp/l2tp.conf $(DESTDIR)/etc/l2tp/l2tp.conf.example ; \
	fi
	@echo ""
	@echo "Type 'pppoe-setup' to configure the software."

servpoet-tgz: distro-servpoet
	cd .. && tar cvf servpoet-$(RP_VERSION)$(BETA).tar servpoet-$(RP_VERSION)$(BETA)
	gzip -f -v -9 ../servpoet-$(RP_VERSION)$(BETA).tar
	rm -rf ../servpoet-$(RP_VERSION)$(BETA)

tgz: distro
	cd .. && tar cvf rp-pppoe-$(RP_VERSION)$(BETA).tar rp-pppoe-$(RP_VERSION)$(BETA)
	gzip -f -v -9 ../rp-pppoe-$(RP_VERSION)$(BETA).tar
	rm -rf ../rp-pppoe-$(RP_VERSION)$(BETA)

distro-servpoet: distro
	cp ../servpoet.spec ../rp-pppoe-$(RP_VERSION)$(BETA)
	$(MAKE) -C licensed-only distro RP_VERSION=$(RP_VERSION) BETA=$(BETA)
	mv ../rp-pppoe-$(RP_VERSION)$(BETA) ../servpoet-$(RP_VERSION)$(BETA)

distro:
	rm -rf ../rp-pppoe-$(RP_VERSION)$(BETA)
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)
	for i in README SERVPOET go go-gui rp-pppoe.spec ; do \
		cp ../$$i ../rp-pppoe-$(RP_VERSION)$(BETA) || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/gui
	for i in en.msg ja.msg Makefile.in tkpppoe.in wrapper.c tkpppoe.1 pppoe-wrapper.1 ; do \
		cp ../gui/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/gui || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/gui/html
	for i in mainwin-busy.png mainwin-nonroot.png mainwin.png props-advanced.png props-basic.png props-nic.png props-options.png tkpppoe.html ; do \
		cp ../gui/html/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/gui/html || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/configs
	for i in firewall-masq firewall-standalone pap-secrets pppoe-server-options pppoe.conf ; do \
		cp ../configs/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/configs || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/doc
	for i in CHANGES KERNEL-MODE-PPPOE HOW-TO-CONNECT LICENSE PROBLEMS ; do \
		cp ../doc/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/doc || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/man
	for i in pppoe-connect.8 pppoe-setup.8 pppoe-start.8 pppoe-status.8 pppoe-stop.8 pppoe-server.8 pppoe-sniff.8 pppoe.8 pppoe-relay.8 pppoe.conf.5 ; do \
		cp ../man/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/man || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/scripts
	for i in pppoe-connect.in pppoe-init-suse.in pppoe-init.in pppoe-setup.in pppoe-start.in pppoe-stop.in pppoe-status ; do \
		cp ../scripts/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/scripts || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/src
	for i in Makefile.in install-sh common.c config.h.in configure configure.in debug.c discovery.c if.c md5.c md5.h ppp.c pppoe-server.c pppoe-sniff.c pppoe.c pppoe.h pppoe-server.h plugin.c relay.c relay.h ; do \
		cp ../src/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/src || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/src/libevent
	for i in Makefile.in event.c event.h event_tcp.c event_tcp.h eventpriv.h hash.c hash.h event_sig.c ; do \
		cp ../src/libevent/$$i ../rp-pppoe-$(RP_VERSION)$(BETA)/src/libevent || exit 1; \
	done
	mkdir ../rp-pppoe-$(RP_VERSION)$(BETA)/src/plugin

distro-beta: beta-check
	$(MAKE) distro BETA=-BETA-$(BETA)

tgz-beta: beta-check
	$(MAKE) tgz BETA=-BETA-$(BETA)

beta-check:
	@if test "$(BETA)" = "" ; then \
		echo "Usage: make distro-beta BETA=<x>"; \
		exit 1; \
	fi

rpms: tgz
	cp ../rp-pppoe-$(RP_VERSION).tar.gz /usr/src/redhat/SOURCES
	gpg --detach-sign /usr/src/redhat/SOURCES/rp-pppoe-$(RP_VERSION).tar.gz
	cd ..; \
	rpm -ba rp-pppoe.spec
	rpm --addsign /usr/src/redhat/SRPMS/rp-pppoe-$(RP_VERSION)-1.src.rpm \
		/usr/src/redhat/RPMS/i386/rp-pppoe-$(RP_VERSION)-1.i386.rpm \
		/usr/src/redhat/RPMS/i386/rp-pppoe-gui-$(RP_VERSION)-1.i386.rpm

servpoet-rpms: servpoet-tgz
	cp ../servpoet-$(RP_VERSION).tar.gz /usr/src/redhat/SOURCES
	cd .. && rpm -ba servpoet.spec

clean:
	rm -f *.o pppoe-relay pppoe pppoe-sniff pppoe-server core rp-pppoe.so plugin/*.o plugin/libplugin.a *~
	test -f licensed-only/Makefile && $(MAKE) -C licensed-only clean || true
	test -f libevent/Makefile && $(MAKE) -C libevent clean || true
	test -f l2tp/Makefile && $(MAKE) -C l2tp clean || true

distclean: clean
	rm -f Makefile config.h config.cache config.log config.status
	rm -f libevent/Makefile
	rm -f ../scripts/pppoe-connect ../scripts/pppoe-start ../scripts/pppoe-stop ../scripts/pppoe-init ../scripts/pppoe-setup ../scripts/pppoe-init-suse

update-version:
	sed -e 's/^Version: .*$$/Version: $(RP_VERSION)/' ../rp-pppoe.spec > ../rp-pppoe.spec.new && mv ../rp-pppoe.spec.new ../rp-pppoe.spec
	sed -e 's+^Source: .*$$+Source: https://dianne.skoll.ca/projects/rp-pppoe/download/rp-pppoe-$(RP_VERSION).tar.gz+' ../rp-pppoe.spec > ../rp-pppoe.spec.new && mv ../rp-pppoe.spec.new ../rp-pppoe.spec
	test -f ../servpoet.spec && sed -e 's/^Version: .*$$/Version: $(RP_VERSION)/' ../servpoet.spec > ../servpoet.spec.new && mv ../servpoet.spec.new ../servpoet.spec || true
	test -f ../servpoet.spec && sed -e 's+^Source: .*$$+Source: https://dianne.skoll.ca/projects/rp-pppoe/download/servpoet-$(RP_VERSION).tar.gz+' ../servpoet.spec > ../servpoet.spec.new && mv ../servpoet.spec.new ../servpoet.spec || true

# Convenience target for Dianne!  Don't try to use this one.
km:
	./configure --enable-plugin=/home/dfs/Archive/PPP/ppp-2.4.0.pppoe4-patched-dfs

licensed-only:
	$(MAKE) -C licensed-only all RP_VERSION=$(RP_VERSION) SERVPOET_VERSION=$(SERVPOET_VERSION)

l2tp: libevent/libevent.a
	$(MAKE) -C l2tp all

.PHONY: update-version

.PHONY: clean

.PHONY: distclean

.PHONY: rpms

.PHONY: licensed-only

.PHONY: distro

.PHONY: l2tp
