#-----------------------------------------------------------------------------+
# Makefile for wp-mirror-mediawiki-extensions-math-texv                       |
# Copyright (C) 2014 Dr. Kent L. Miller.  All rights reserved.                |
#                                                                             |
# 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 3 of the License, or (at       |
# your option) any later version.                                             |
#                                                                             |
# 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.                                    |
#                                                                             |
# You should have received a copy of the GNU General Public License           |
# along with this program.  If not, see <http://www.gnu.org/licenses/>")      |
#-----------------------------------------------------------------------------+

VERSION          = 1.24
PROGRAM          = wp-mirror-mediawiki-extensions-math-texvc
SOURCEDIR        = $(PROGRAM)-$(VERSION)
DISTNAME         = $(SOURCEDIR)

GITSITE          = https://gerrit.wikimedia.org/r/p/mediawiki/extensions/
MATH             = Math
BRANCH           = wmf/1.24wmf22

BINDIR           = $(DESTDIR)$(PREFIX)/usr/bin/
EXTDIR           = $(DESTDIR)$(PREFIX)/usr/share/$(PROGRAM)/extensions/
MANDIR           = $(DESTDIR)$(PREFIX)/usr/share/man/man1/

CHMOD            = /bin/chmod
CPIO             = /bin/cpio
GIT              = /usr/bin/git
GREP             = /bin/grep
GZIP             = /bin/gzip
FIND             = /usr/bin/find
INSTALL          = /usr/bin/install
LN               = /bin/ln
SHELL            = /bin/sh
RM               = /bin/rm
TAR              = /bin/tar

LIBOWNER         = root
LIBGROUP         = root

STEM             = wp-mirror-
MATHDIRS         = math texvccheck
MATHFILES        = texvc texvccheck

#-----------------------------------------------------------------------------+
# targets: build, install, deinstall, clean, dist, diskclean                  |
#-----------------------------------------------------------------------------+

build: 
	for dir in $(MATHDIRS); do \
	  (cd $(MATH)/$$dir; make clean; rm -f $(STEM)texvc*; make;) \
	done

install: build
	$(INSTALL) --mode=755 --directory          $(BINDIR)
	# install /usr/bin/*
	(cd $(MATH)/math; \
	cp -a texvc $(STEM)texvc; \
	$(INSTALL) --mode=755 $(STEM)texvc         $(BINDIR);)
	(cd $(MATH)/texvccheck; \
	cp -a texvccheck $(STEM)texvccheck; \
	$(INSTALL) --mode=755 $(STEM)texvccheck    $(BINDIR);)
	# install /usr/share/man/man1/*
	$(INSTALL) --mode=755 --directory          $(MANDIR)
	for file in $(MATHFILES); do \
	  $(INSTALL) --mode=644 $(STEM)$$file.1.gz   $(MANDIR); \
	done

deinstall:
	for file in $(MATHFILES); do \
	  $(RM) -fr $(BINDIR)$(STEM)$$file; \
	  $(RM) -fr $(MANDIR)$(STEM)$$file.1.gz; \
	done

clean:
	for dir in $(MATHDIRS); do \
	  (cd $(MATH)/$$dir; make clean;) \
	done
	$(RM) -f $(MATH)/math/$(STEM)texvc
	$(RM) -f $(MATH)/texvccheck/$(STEM)texvccheck
	$(RM) -f *~

dist: clean distclean
	$(RM) -f $(DISTNAME)
	$(LN) --symbolic -f . $(DISTNAME)
	$(TAR) --create --verbose --file $(DISTNAME).tar \
	  $(DISTNAME)/Makefile \
	  $(DISTNAME)/Math/math \
	  $(DISTNAME)/Math/texvccheck \
	  $(DISTNAME)*.1.gz
	$(GZIP) -v $(DISTNAME).tar

distclean:
	$(RM) -f $(DISTNAME)
	$(RM) -f $(DISTNAME).tar.gz

#-----------------------------------------------------------------------------+
# obtain code from WMF                                                        |
#-----------------------------------------------------------------------------+

all: pull checkout
clone:
	git clone $(GITSITE)$(MATH).git;
pull:
	echo $(MATH); cd $(MATH); $(GIT) pull;
branch-remote:
	echo $(MATH); cd $(MATH); $(GIT) branch -r;
branch:
	echo $(MATH); cd $(MATH); $(GIT) branch;
checkout:
	echo $(MATH); cd $(MATH); $(GIT) checkout $(BRANCH);

wipe:
	$(RM) -fr $(MATH)
