#-----------------------------------------------------------------------------+
# Makefile for wp-mirror-mediawiki                                            |
# 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
SOURCEDIR        = $(PROGRAM)-$(VERSION)

GITSITE          = https://gerrit.wikimedia.org/r/p/mediawiki/
CORE             = core
SKINS            = skins
BRANCH           = wmf/1.24wmf22

DOCDIR           = $(DESTDIR)$(PREFIX)/usr/share/doc/$(PROGRAM)/
ETCDIR           = $(DESTDIR)$(PREFIX)/etc/$(PROGRAM)/
SHAREDIR         = $(DESTDIR)$(PREFIX)/usr/share/$(PROGRAM)/
SKINSDIR         = $(DESTDIR)$(PREFIX)/usr/share/$(PROGRAM)/$(SKINS)/
SKINDIRS         = CologneBlue Modern MonoBook Nostalgia Vector
LIBDIR           = $(DESTDIR)$(PREFIX)/var/lib/$(PROGRAM)/
LNDOCDIR         = ../../../usr/share/doc/$(PROGRAM)/
LNETCDIR         = ../../../etc/$(PROGRAM)/
LNSHAREDIR       = ../../../usr/share/$(PROGRAM)/
LNLIBDIR         = ../../../var/lib/$(PROGRAM)/

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

LIBOWNER         = root
LIBGROUP         = root

DOCDIRS          = docs
DOCFILES         = CREDITS FAQ README RELEASE-NOTES-1.23 RELEASE-NOTES-1.24 \
	           UPGRADE
ETCFILES         = LocalSettings.php
LIBDIRS          = cache images
SHAREDIRS        = extensions includes languages maintenance mw-config \
                   resources serialized skins tests vendor
SHAREFILES       = api.php api.php5 composer.json img_auth.php img_auth.php5 \
	           index.php index.php5 load.php load.php5 \
	           opensearch_desc.php opensearch_desc.php5 \
                   profileinfo.php profileinfo.php5 \
	           thumb_handler.php thumb_handler.php5 \
	           thumb.php thumb.php5 StartProfiler.sample wiki.phtml
SHARE644S        = resources/src/mediawiki.skinning/content.externallinks.css \
	           tests/browser/Gemfile
SHARE755S        = includes/limit.sh \
	           includes/normal/UtfNormalTest2.php \
	           maintenance/postgres/compare_schemas.pl \
	           maintenance/postgres/mediawiki_mysql2postgres.pl \
	           maintenance/storage/make-blobs

#-----------------------------------------------------------------------------+
# targets: build, install, deinstall                                          |
#-----------------------------------------------------------------------------+

build: # nothing to build

install: build
	$(INSTALL) --mode=755 --directory          $(DOCDIR)
	$(INSTALL) --mode=755 --directory          $(ETCDIR)
	$(INSTALL) --mode=755 --directory          $(LIBDIR)
	$(INSTALL) --mode=755 --directory          $(SHAREDIR)
	# install /usr/share/doc/wp-mirror-mediawiki/*
	for dir in $(DOCDIRS); do \
	  (cd $(CORE); \
	  $(FIND) $$dir -depth -print | $(GREP) -v ".git" \
	  | $(CPIO) --pass-through --make-directories \
	  --preserve-modification-time --unconditional \
	  --owner $(LIBUSER):$(LIBGROUP) $(DOCDIR);) \
	done
	$(INSTALL) --mode=655 $(CORE)/HISTORY        $(DOCDIR)changelog
	for file in $(DOCFILES); do \
	  $(INSTALL) --mode=644 $(CORE)/$$file       $(DOCDIR)$$file; \
	done
	# install /etc/wp-mirror-mediawiki/*
	for file in $(ETCFILES); do \
	  $(LN) --symbolic $(LNETCDIR)$$file         $(LIBDIR)$$file; \
	  $(LN) --symbolic $(LNLIBDIR)$$file         $(SHAREDIR)$$file; \
	done
	# install /var/lib/wp-mirror-mediawiki/*
	for dir in $(LIBDIRS); do \
	  (cd $(CORE); \
	  $(FIND) $$dir -depth -print | $(GREP) -v ".git" \
	  | $(CPIO) --pass-through --make-directories \
	  --preserve-modification-time --unconditional \
	  --owner $(LIBUSER):$(LIBGROUP)             $(LIBDIR);) \
	done
	for dir in $(LIBDIRS); do \
	  $(LN) --symbolic $(LNLIBDIR)$$dir          $(SHAREDIR)$$dir; \
	done
	# install /usr/share/wp-mirror-mediawiki/*
	for dir in $(SHAREDIRS); do \
	  (cd $(CORE); \
	  $(FIND) $$dir -depth -print | $(GREP) -v ".git" \
	  | $(GREP) -v COPYING | $(GREP) -v LICENSE \
	  | $(CPIO) --pass-through --make-directories \
	  --preserve-modification-time --unconditional \
	  --owner $(LIBUSER):$(LIBGROUP)             $(SHAREDIR);) \
	done
	for dir in $(SHAREDIRS); do \
	  $(LN) --symbolic $(LNSHAREDIR)$$dir        $(LIBDIR)$$dir; \
	done
	for file in $(SHAREFILES); do \
	  $(INSTALL) --mode=644 $(CORE)/$$file       $(SHAREDIR)$$file; \
	  $(LN) --symbolic $(LNSHAREDIR)$$file       $(LIBDIR)$$file; \
	done
	# install /usr/share/wp-mirror-mediawiki/skins/*
	for dir in $(SKINDIRS);  do \
	  $(FIND) $$dir -depth -print | $(GREP) -v ".git" \
	  | $(GREP) -v COPYING | $(GREP) -v LICENSE \
	  | $(CPIO) --pass-through --make-directories \
	  --preserve-modification-time --unconditional \
	  --owner $(LIBUSER):$(LIBGROUP)             $(SKINSDIR); \
	done
	# fix permissions
	for file in $(SHARE644S); do \
	  $(CHMOD) 644                               $(SHAREDIR)$$file; \
	done
	for file in $(SHARE755S); do \
	  $(CHMOD) 755                               $(SHAREDIR)$$file; \
	done

deinstall:
	$(RM) -fr $(DOCDIR)
	$(RM) -fr $(ETCDIR)
	$(RM) -fr $(SHAREDIR)
	$(RM) -fr $(LIBDIR)

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

all: pull checkout
clone:
	$(GIT) clone $(GITSITE)$(CORE).git;
	for dir in $(SKINDIRS); do \
	  $(GIT) clone $(GITSITE)$(SKINS)$$dir.git; \
	done
pull:
	echo $(CORE); cd $(CORE); $(GIT) pull;
	for dir in $(SKINDIRS); do \
	  (echo $$dir; cd $$dir; $(GIT) pull;); \
	done
branch-remote:
	echo $(CORE); cd $(CORE); $(GIT) branch -r;
	for dir in $(SKINDIRS); do \
	  (echo $$dir; cd $$dir; $(GIT) branch -r;); \
	done
branch:
	echo $(CORE); cd $(CORE); $(GIT) branch;
	for dir in $(SKINDIRS); do \
	  (echo $$dir; cd $$dir; $(GIT) branch;); \
	done
checkout:
	echo $(CORE); cd $(CORE); $(GIT) checkout $(BRANCH);
	for dir in $(SKINDIRS); do \
	  (echo $$dir; cd $$dir; $(GIT) checkout $(BRANCH);); \
	done

wipe:
	$(RM) -rf $(CORE)

#-----------------------------------------------------------------------------+
# misc                                                                        |
#-----------------------------------------------------------------------------+

clean:
	$(RM) -f *~
