# utilities build and install
# Copyright (C) 1998, 1999  Henry Spencer.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# RCSID $Id: Makefile,v 1.60 1999/12/14 21:23:42 henry Exp $

# pathnames, subject to overrides from main Makefile
PUBDIR=/usr/local/sbin
PRIVDIR=/usr/local/lib/ipsec
FINALPRIVDIR=/usr/local/lib/ipsec
RCDIR=/etc/rc.d/init.d
CONFDIR=/etc
MANTREE=/usr/local/man

FMANDIR=$(MANTREE)/man5
CMANDIR=$(MANTREE)/man8
PUBS=ipsec
BINS=ranbits rsasigkey
SCRIPTS=barf manual auto look
INTERNALS=_include _confread _keycensor _secretcensor
PRIVS=$(PUBS) $(SCRIPTS) $(INTERNALS) $(BINS)
GENDFILES=secrets.eg conf.eg
PERMINDEX=html2four four2perm
LOCALS=man_xref $(PERMINDEX)
PLAIN_MANS=ipsec.8
IPSEC_CMANS=barf.8 manual.8 auto.8 setup.8 ranbits.8
MANS=$(PLAIN_MANS) $(IPSEC_CMANS)
CFLAGS=-I../lib
LIB=../lib/libfreeswan.a
INSTALL=install
PUB=--mode=644
PRIV=--mode=600

SHELL=/bin/sh

all:	$(PUBS) $(PRIVS) $(GENDFILES) $(LOCALS)

permindex:	$(PERMINDEX)

ranbits:	ranbits.o
	$(CC) $(CFLAGS) ranbits.o $(LIB) -o $@

rsasigkey:	rsasigkey.o
	$(CC) $(CFLAGS) rsasigkey.o $(LIB) ../lib/libgmp.a -o $@

man_xref:	man_xref.o
	$(CC) $(CFLAGS) man_xref.o $(LIB) -o $@

html2four:	html2four.o
	$(CC) $(CFLAGS) html2four.o $(LIB) -o $@

four2perm:	four2perm.o
	$(CC) $(CFLAGS) four2perm.o $(LIB) -o $@

ipsec:	ipsec.in
	VER=`sed -n '/"/s/^[^"]*"//p' ../version.c | sed 's/".*//'` ; \
	sed "s/xxx/$$VER/" ipsec.in | sed "s:@IPSECDIR@:$(FINALPRIVDIR):" >$@
	chmod +x $@

# generate sample secret, obfuscating it so that people don't get confused
# about whether it is a string or a hex number
secrets.eg:	secrets.proto ranbits
	rm -f $@
	umask 077 ; \
		cat secrets.proto | \
		sed "s/xxx/`./ranbits 256 | tr '0-9a-f_' 'j-n1-5R-Wu'`/" | \
		egrep -v RCSI >$@

conf.eg:	conf.proto ranbits randomize
	rm -f $@
	./randomize conf.proto | egrep -v RCSI >$@

install:	$(PUBS) $(PRIVS) $(MANS) $(GENDFILES)
	mkdir -p $(PUBDIR) $(PRIVDIR)
	$(INSTALL) $(PUBS) $(PUBDIR)
	$(INSTALL) $(PRIVS) $(PRIVDIR)
	$(INSTALL) $(PUB) ipsec.conf.5 $(FMANDIR)
	$(INSTALL) $(PUB) $(PLAIN_MANS) $(CMANDIR)
	for f in $(IPSEC_CMANS) ; \
	do \
		$(INSTALL) $(PUB) $$f $(CMANDIR)/ipsec_$$f || exit 1 ; \
		./manlink $(CMANDIR) ipsec_$$f ; \
	done
	test -f $(CONFDIR)/ipsec.secrets || \
		$(INSTALL) $(PRIV) secrets.eg $(CONFDIR)/ipsec.secrets
	test -f $(CONFDIR)/ipsec.conf || \
		$(INSTALL) $(PUB) conf.eg $(CONFDIR)/ipsec.conf
	# main copy must go in RCDIR, PRIVDIR may not be mounted at boot time
	$(INSTALL) setup $(RCDIR)/ipsec
	rm -f $(PRIVDIR)/setup
	ln -s $(RCDIR)/ipsec $(PRIVDIR)/setup
	if which chkconfig >/dev/null 2>&1 ; \
	then chkconfig --add ipsec ; \
	else $(MAKE) setup4 ; \
	fi

setup4:	$(RCDIR)/ipsec
	# fallback rc install -- on in run states 2345, off in 016
	-cd $(RCDIR)/../rc0.d ; ln -s ../init.d/ipsec K35ipsec
	-cd $(RCDIR)/../rc1.d ; ln -s ../init.d/ipsec K35ipsec
	-cd $(RCDIR)/../rc2.d ; ln -s ../init.d/ipsec S35ipsec
	-cd $(RCDIR)/../rc3.d ; ln -s ../init.d/ipsec S35ipsec
	-cd $(RCDIR)/../rc4.d ; ln -s ../init.d/ipsec S35ipsec
	-cd $(RCDIR)/../rc5.d ; ln -s ../init.d/ipsec S35ipsec
	-cd $(RCDIR)/../rc6.d ; ln -s ../init.d/ipsec K35ipsec

clean:
	rm -f *.o $(BINS) $(GENDFILES) $(LOCALS)
