#
#      Makefile for mipdiag
#
#      Authors:
#      Jaakko Laine <jaakko.laine@hut.fi>
#
#      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.

SHELL = @SHELL@

VERSION = @VERSION@

CC = @CC@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
sbindir = @sbindir@
libdir = @libdir@
sysconfdir = @sysconfdir@
localstatedir = @localstatedir@
mandir = @mandir@
man5dir = $(mandir)/man5
man8dir = $(mandir)/man8
initdir = @INITDIR@
initscript = mobile-ip6
docdir = @sysconfdir@

NETWORK_MIP6_CONF = @NETWORK_MIP6_CONF@

RM = @RM@
MKDIR = @MKDIR@
SED = @SED@
GZIP = @GZIP@

CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DIR = @INSTALL_DIR@
INSTALL_DATA = @INSTALL_DATA@

### End of configuration section ###

PROGRAM = @PROGS@
OBJS = mipdiag.o
C_FILES = mipdiag.c
MAN_FILES = mipdiag.8.man mipv6_acl.conf.5.man mipv6_sas.conf.5.man network-mip6.conf.5.man
MANS =  mipdiag.8 mipv6_acl.conf.5 mipv6_sas.conf.5 network-mip6.conf.5

.SUFFIXES:
.SUFFIXES: .c .o .man

all: $(PROGRAM) $(MANS) mobile-ip6 network-mip6.conf

$(PROGRAM): $(C_FILES)
	$(CC) $(CFLAGS) $? -o $@

network-mip6.conf: network-mip6.conf.in Makefile
	sed	\
		-e "s/\@sysconfdir\@/`echo @sysconfdir@ | sed -e 's#/#\\\\/#g'`/g" \
		$< > $@

clean:
	$(RM) -f $(OBJS) $(PROGRAM) core $(MANS)

install: all
	@for dir in $(bindir) $(man5dir) $(man8dir) $(sysconfdir) $(docdir) ; do \
		$(MKDIR) -p $$dir ; \
	done

	@$(INSTALL_DIR) $(sbindir)
	@$(INSTALL_PROGRAM) -m 0755 $(PROGRAM) $(sbindir)/$(PROGRAM)
	@$(INSTALL_DIR) $(initdir)
	@$(INSTALL_PROGRAM) -m 0755 $(initscript) $(initdir)/$(initscript)

	@$(INSTALL_DIR) $(man8dir)
	@$(INSTALL_DATA) mipdiag.8 $(man8dir)/mipdiag.8
	@$(GZIP) -9 -f $(man8dir)/mipdiag.8

	@$(INSTALL_DIR) $(man5dir)
	@for file in mipv6_acl.conf.5 network-mip6.conf.5 mipv6_sas.conf.5 ; do \
		$(INSTALL_DATA) $$file $(man5dir)/$$file ; \
		$(GZIP) -9 -f $(man5dir)/$$file ; \
	done

	@for file in mipv6_acl.conf.example mipv6_sas.conf.example ; do \
		$(INSTALL_DATA) $$file $(docdir)/$$file ; \
	done

	@if test ! -f $(NETWORK_MIP6_CONF)/network-mip6.conf ; then \
		$(INSTALL_DATA) network-mip6.conf $(NETWORK_MIP6_CONF)/network-mip6.conf ; \
	fi

man: $(MANS)

.c.o:
	$(CC) -c $(CFLAGS) $<

.man:
	@$(SED)	-e 's#@''VERSION''@#$(VERSION)#g' \
		-e 's#@''sbindir''@#s(sbindir)#g' \
		-e 's#@''sysconfdir''@/network-mip6.conf#$(NETWORK_MIP6_CONF)/network-mip6.conf#g' \
		-e 's#@''sysconfdir''@#$(sysconfdir)#g' \
		$< > $@
