## Makefile.common
## ======================================================================

PACKAGE_NAME=		@PACKAGE_NAME@
PACKAGE_VERSION=	@PACKAGE_VERSION@
PACKAGE_TARNAME=	@PACKAGE_TARNAME@

## Path
## ----------------------------------------------------------------------

INSTALL_ROOT=		$(DESTDIR)

package_subdir=		@package_subdir@

prefix=			@prefix@
exec_prefix=		@exec_prefix@
bindir=			@bindir@
sbindir=		@sbindir@
libdir=			@libdir@
libexecdir=		@libexecdir@$(package_subdir)
sysconfdir=		@sysconfdir@$(package_subdir)
localstatedir=		@localstatedir@
datadir=		@datadir@
datarootdir=		@datarootdir@$(package_subdir)
mandir=			@mandir@
infodir=		@infodir@
docdir=			@docdir@

vardir=			$(localstatedir)/lib$(package_subdir)
rundir=			$(localstatedir)/run$(package_subdir)
logdir=			$(localstatedir)/log$(package_subdir)

## Commands, Flags and so on
## ----------------------------------------------------------------------

SUBST_SRC=		$(SOURCE_BUILD)/subst.pl
SUBST_VARS=		$(SOURCE_BUILD)/subst.vars
SUBST_COMMAND=		$(SOURCE_BUILD)/subst
SUBST=			@PERL_COMMAND@ $(SUBST_COMMAND) $(SUBST_ARGS)

CC=			@CC@
CPPFLAGS=		@CPPFLAGS@
CFLAGS=			@CFLAGS@

CP=			cp -p
RM=			rm -rf
LN_S=			ln -s
INSTALL=		$(SOURCE_BUILD)/install-sh -c
CHMOD_FILES=		chmod 0644
CHMOD_CMDS=		chmod 0755

## Package local
## ----------------------------------------------------------------------

include $(SOURCE_BUILD)/Makefile.package

## Suffix rules
## ======================================================================

.SUFFIXES: .pl .pm .cmd .example .tmpl

.pl.cmd:
	@echo "Building $@ from $< ..."
	@$(RM) $@ $@.tmp
	@$(SUBST) <$< >$@.tmp
	@$(CHMOD_CMDS) $@.tmp
	@mv $@.tmp $@

.pl.pm:
	@echo "Building $@ from $< ..."
	@$(RM) $@ $@.tmp
	@$(SUBST) <$< >$@.tmp
	@$(CHMOD_FILES) $@.tmp
	@mv $@.tmp $@

.tmpl.example:
	@echo "Building $@ from $< ..."
	@$(RM) $@ $@.tmp
	@$(SUBST) <$< >$@.tmp
	@$(CHMOD_FILES) $@.tmp
	@mv $@.tmp $@

## Target rules
## ======================================================================

default:: build

build:: $(BUILD_TARGETS)

install:: $(INSTALL_TARGETS)

install-dir:
	@echo "Creating directory $(INSTALL_ROOT)$(INSTALL_DIR) ..."
	@$(INSTALL) -d $(INSTALL_ROOT)$(INSTALL_DIR)

install-files: install-dir
	@for f in $(INSTALL_FILES); do \
	    echo "Installing file $$f into $(INSTALL_ROOT)$(INSTALL_DIR) ..."; \
	    $(INSTALL) -m 0644 -t='s/\.substed$$//' $$f "$(INSTALL_ROOT)$(INSTALL_DIR)" \
	      || exit 1; \
	done

install-cmd-dir:
	@echo "Creating directory $(INSTALL_ROOT)$(INSTALL_CMD_DIR) ..."
	@$(INSTALL) -d $(INSTALL_ROOT)$(INSTALL_CMD_DIR)

install-cmds: install-cmd-dir
	@for f in $(INSTALL_CMDS); do \
	    echo "Installing file $$f into $(INSTALL_ROOT)$(INSTALL_CMD_DIR) ..."; \
	    $(INSTALL) -m 0755 -t='s/\.cmd$$//' $$f "$(INSTALL_ROOT)$(INSTALL_CMD_DIR)" \
	      || exit 1; \
	done

clean::
	$(RM) $(BUILD_TARGETS) $(BUILD_TARGETS_POST) $(CLEAN_TARGETS) *.tmp

distclean:: clean

build install clean distclean::
	@if [ -n "$(MAKE_DIRS)" ]; then \
	    set - $(MAKE_DIRS); \
	    for subdir in "$$@"; do \
		(cd $$subdir && $(MAKE) $@) || exit 1; \
	    done \
	fi

build:: $(BUILD_TARGETS_POST)

$(BUILD_TARGETS):: $(SUBST_COMMAND)

$(SUBST_COMMAND): $(SUBST_SRC) $(SUBST_VARS) $(SOURCE_BUILD)/Makefile.common $(SOURCE_BUILD)/Makefile.package
	cp $< $@.tmp
	cat $(SUBST_VARS) >>$@.tmp
	sed -n 's/^\([A-Za-z_][A-Za-z_0-9]*\)[ 	]*=[ 	]*\(.*\)/\1=\2/p' \
	  $(SOURCE_BUILD)/Makefile.common \
	  $(SOURCE_BUILD)/Makefile.package \
	  >>$@.tmp
	chmod 0755 $@.tmp
	mv $@.tmp $@

