cc = gcc
flags = -O2

BINDIR=/usr/local/bin
SBINDIR=/usr/local/sbin
PPPDIR=/etc/ppp

all: getln

getln: getln.c
	$(cc) $(flags) -o getln getln.c
	@if [ -f /proc/net/ip_fwchains -a -x /sbin/ipchains ]; then \
		echo "Chains Enabled ..."; \
		cp ip-up.something ip-up; \
		cp ip-down.chains ip-down; \
		cp ip-forwarding.chains ip-forwarding; \
		cp mkfirewall.chains mkfirewall; \
		cp rmfirewall.chains rmfirewall; \
	else \
		if [ -x /sbin/ipfwadm ]; then \
			if [ -f /proc/net/ip_fwchains ]; then \
				echo "Chains Capable but Not Enabled ..."; \
			else \
				echo "No Chains Enabled ..."; \
			fi; \
			cp ip-up.something ip-up; \
			cp ip-down.nochains ip-down; \
			cp ip-forwarding.nochains ip-forwarding; \
			cp mkfirewall.nochains mkfirewall; \
			cp rmfirewall.nochains rmfirewall; \
		else \
			echo "No firewall/masquerading support!"; \
			cp ip-up.nothing ip-up; \
			cp ip-down.nothing ip-down; \
		fi; \
	fi

install: getln
	if [ ! -d $(PPPDIR) ]; then \
		mkdir $(PPPDIR); \
	fi
	if [ -f $(PPPDIR)/ppp-settings ]; then \
		mv $(PPPDIR)/ppp-settings $(PPPDIR)/ppp-settings.old; \
	fi
	if [ -f $(PPPDIR)/options ]; then \
		mv $(PPPDIR)/options $(PPPDIR)/options.old; \
	fi
	if [ -f $(PPPDIR)/afs.cfg ]; then \
		mv $(PPPDIR)/afs.cfg $(PPPDIR)/afs.cfg.old; \
	fi
	cp -f * $(PPPDIR)
	cp -f getln $(BINDIR)
	chmod 711 $(PPPDIR)
	chmod 700 $(PPPDIR)/*
	chmod 600 $(PPPDIR)/README* $(PPPDIR)/*secrets $(PPPDIR)/options
	chmod 755 $(PPPDIR)/cur* $(PPPDIR)/chk4net $(BINDIR)/getln
	rm -f $(PPPDIR)/Makefile $(PPPDIR)/getln* $(PPPDIR)/*.chains $(PPPDIR)/*.nochains $(PPPDIR)/*.nothing $(PPPDIR)/*.something
	if [ -f $(PPPDIR)/ppp-settings.old ]; then \
		mv $(PPPDIR)/ppp-settings $(PPPDIR)/ppp-settings.new; \
		mv $(PPPDIR)/ppp-settings.old $(PPPDIR)/ppp-settings; \
	fi
	if [ -f $(PPPDIR)/options.old ]; then \
		mv $(PPPDIR)/options $(PPPDIR)/options.new; \
		mv $(PPPDIR)/options.old $(PPPDIR)/options; \
	fi
	if [ -f $(PPPDIR)/afs.cfg.old ]; then \
		mv $(PPPDIR)/afs.cfg $(PPPDIR)/afs.cfg.new; \
		mv $(PPPDIR)/afs.cfg.old $(PPPDIR)/afs.cfg; \
	fi
	ln -sf $(PPPDIR)/ppp-settings /etc/ppp-settings
	ln -sf $(PPPDIR)/cur* $(BINDIR)
	ln -sf $(PPPDIR)/chk4net $(BINDIR)
	ln -sf $(PPPDIR)/ppp-o* $(SBINDIR)
	if [ -x /sbin/ipfwadm -o -x /sbin/ipchains ]; then \
		ln -sf $(PPPDIR)/??firewall $(SBINDIR); \
	fi
	ln -sf $(PPPDIR)/modem $(SBINDIR)

clean:
	rm -f getln ip-up ip-down ip-forwarding ??firewall *~
