# written by Guido Socher
PREFIX=/usr
INSTALL=/usr/bin/install -c
MANP=man1/gpppwrap.1 
CC=gcc
CFLAGS= -Wall -O2
#sun c/c++-compiler:
#CC=CC
#CFLAGS= -O             

# use one of these if you do not have gtk-config:
#LIBS=-L /usr/X11R6/lib -lgmodule -lglib -lgtk -lgdk -lXext -lX11 -lm
#LIBS=-L /usr/X11R6/lib -lglib -lgtk -lgdk -lXext -lX11 -lm
#INCLUDES=-I /usr/lib/glib/include

# gtk-config is usually installed together with gtk and it should
# be used when possible:
LIBS=`gtk-config --libs`
INCLUDES=`gtk-config --cflags`
all: gpppquery gpppwrap

gpppquery: gpppquery.o
	$(CC) -o gpppquery $(LIBS) gpppquery.o 
gpppquery.o: gpppquery.c
	$(CC) -c $(CFLAGS) $(INCLUDES) gpppquery.c
gpppwrap: gpppwrap.o
	$(CC) -o gpppwrap $(LIBS) gpppwrap.o 
gpppwrap.o: gpppwrap.c
	$(CC) -c $(CFLAGS) $(INCLUDES) gpppwrap.c

install: gpppwrap $(MANP)
	[ -d "$(PREFIX)/bin" ] || $(INSTALL) -d $(PREFIX)/bin
	[ -d "$(PREFIX)/man/man1" ] || $(INSTALL) -d $(PREFIX)/man/man1
	@echo "installing gpppwrap with SUID"
	$(INSTALL) -m 755 gpppwrap $(PREFIX)/bin 
	$(INSTALL) -m 644 $(MANP) $(PREFIX)/man/man1
	@echo "NOTE: run make install_scripts to install the etc/... subtree"
	@echo "NOTE: The cgi-bin pppcontrol needs to be installed manually"
	@echo "NOTE: with the command cp."

install_with_cp: gpppwrap gpppquery $(MANP)
	[ -d "$(PREFIX)/bin" ] || mkdir -p $(PREFIX)/bin
	cp gpppwrap $(PREFIX)/bin 
	@echo "installing gpppwrap with SUID"
	chmod 755 $(PREFIX)/bin/gpppwrap
	chmod 644 $(MANP) 
	[ -d "$(PREFIX)/man/man1" ] || mkdir -p $(PREFIX)/man/man1
	cp $(MANP) $(PREFIX)/man/man1 
	@echo "NOTE: run make install_scripts to install the etc/... subtree"
	@echo "NOTE: The cgi-bin pppcontrol needs to be installed manually"
	@echo "NOTE: with command cp."

install_config_only: etc/ppp/gpppwrap.conf
	[ -d "/etc/ppp" ] || mkdir -p /etc/ppp
	@echo "NOTE: an existing gpppwrap.conf file may be renamed to gpppwrap.conf.bak"
	if [ -e "/etc/ppp/gpppwrap.conf" ]; then\
		 mv /etc/ppp/gpppwrap.conf /etc/ppp/gpppwrap.conf.bak;\
	fi
	$(INSTALL) -m 644 etc/ppp/gpppwrap.conf /etc/ppp/gpppwrap.conf

install_scripts: 
	[ -d "/etc/ppp/scripts" ] || mkdir -p /etc/ppp/scripts
	[ -d "/etc/ppp/peers" ] || mkdir -p /etc/ppp/peers
	@echo "NOTE: the existing /etc/ppp directory will be backed up to /etc/ppp_old.tar"
	(cd /etc;tar cvf /etc/ppp_old.tar ppp)
	(cd etc/ppp; find . -print | cpio -dump /etc/ppp)
	(cd /etc/ppp; chown -R root.root *)
	chmod 6755 /etc/ppp/scripts/ppp-off
	chmod 6755 /etc/ppp/scripts/ppp-on
clean:
	rm -f gpppwrap gpppquery *.o core
