# This file is part of xenv
# Copyright (C) 2021 Sergey Poznyakoff
#
# Xenv 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 3 of the License, or (at your
# option) any later version.
#
# Xenv 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.
#
# You should have received a copy of the GNU General Public License along
# with xenv. If not, see <http://www.gnu.org/licenses/>. */

CPPFLAGS =
CFLAGS = -O2 -Wall
LEXFLAGS = -F

# Installation prefix
PREFIX  = /usr/local
MANDIR  = $(PREFIX)/share/man
BINDIR  = $(PREFIX)/bin
MAN1DIR = $(MANDIR)/man1

# Install program.  Use cp(1) if not available.
INSTALL = install
# Program to make a directory hierarchy.
MKHIER  = install -d

# #############################
# Building the program.
# #############################
PACKAGE_NAME = xenv
PACKAGE_VERSION = 1.0
PACKAGE_TARNAME = $(PACKAGE_NAME)-$(PACKAGE_VERSION)
PACKAGE_BUGREPORT = gray@gnu.org

xenv: lex.yy.c
	$(CC) -oxenv $(CPPFLAGS) $(CFLAGS) lex.yy.c
lex.yy.c: xenv.l
	flex $(LEXFLAGS) xenv.l
clean:
	rm -f xenv 

allclean: clean
	rm -f lex.yy.c

check: xenv testsuite
	@./testsuite

# ###############################
# Building the testsuite.
# ###############################
AUTOM4TE = autom4te
AUTOTEST = $(AUTOM4TE) --language=autotest

TESTSUITE_AT = \
 t/testsuite.at \
 t/asndfl.at\
 t/braces.at\
 t/comment.at\
 t/errmsg.at\
 t/error.at\
 t/ifdef.at\
 t/ifndef.at\
 t/ifnset.at\
 t/ifset.at\
 t/quote.at\
 t/set.at\
 t/simple.at\
 t/ternary.at\
 t/unset.at\
 t/usealt.at\
 t/usedfl.at

package.m4: Makefile
	{                                      \
	  echo '# Signature of the current package.'; \
	  echo 'm4_define([AT_PACKAGE_NAME],      [$(PACKAGE_NAME)])'; \
	  echo 'm4_define([AT_PACKAGE_TARNAME],   [$(PACKAGE_TARNAME)])'; \
	  echo 'm4_define([AT_PACKAGE_VERSION],   [$(PACKAGE_VERSION)])'; \
	  echo 'm4_define([AT_PACKAGE_STRING],    [$(PACKAGE_TARNAME)])'; \
	  echo 'm4_define([AT_PACKAGE_BUGREPORT], [$(PACKAGE_BUGREPORT)])'; \
	} > package.m4

EXTRA_DIST = atlocal package.m4

testsuite: package.m4 $(TESTSUITE_AT)
	$(AUTOTEST) -I . -I t t/testsuite.at -o testsuite

# ###############################
# Install
# ###############################

install: install-bin install-man

install-bin: xenv
	$(MKHIER) $(DESTDIR)$(BINDIR)
	$(INSTALL) xenv $(DESTDIR)$(BINDIR)

install-man:
	$(MKHIER) $(DESTDIR)$(MAN1DIR)
	$(INSTALL) xenv.1 $(DESTDIR)$(MAN1DIR)

# ###############################
# Distribution
# ###############################
DISTDIR = $(PACKAGE_TARNAME)
DISTFILES = xenv.l xenv.1 Makefile lex.yy.c testsuite README

distdir: lex.yy.c
	rm -rf $(DISTDIR)
	mkdir $(DISTDIR)
	cp $(DISTFILES) $(EXTRA_DIST) $(DISTDIR)
	mkdir $(DISTDIR)/t
	cp $(TESTSUITE_AT) $(DISTDIR)/t

dist: distdir
	tar zcf $(PACKAGE_TARNAME).tar.gz $(DISTDIR)
	rm -rf $(DISTDIR)


distcheck: dist
	tar xfz $(PACKAGE_TARNAME).tar.gz
	if $(MAKE) -C $(DISTDIR) $(DISTCHECKFLAGS) check; then \
          text="$(DISTDIR).tar.gz ready for distribution"; \
	  echo "$$text" | sed s/./=/g; \
	  echo "$$text"; \
	  echo "$$text" | sed s/./=/g; \
          rm -rf $(DISTDIR); \
        else \
          exit 2; \
        fi

