# -*- perl -*-

#
# Author: Slaven Rezic
#
# Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2013,2014,2015,2016,2017,2019,2021,2023 Slaven Rezic. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: slaven@rezic.de
# WWW:  http://bbbike.de
#

# This file is included automatically by "perl Makefile.PL"

use BBBikeUtil qw(is_in_path);
use BBBikeBuildUtil qw(get_pmake);
use BBBikeVar qw();
use Config qw(%Config);

$bbbike_makefile_admin = "";

$bbbike_makefile_admin .= <<EOF;
BBBIKE_STABLE_VERSION=	$BBBike::STABLE_VERSION
BBBIKE_DISTFILE_SOURCE=	distfiles/BBBike-\$(BBBIKE_STABLE_VERSION).tar.gz

EOF

my $pmake = get_pmake;
$bbbike_makefile_admin .= <<EOF;
PMAKE=	$pmake
APT=	apt
EOF

######################################################################
# POD files
#
# grab a good pod2html XXX this is somewhat hacky

# I thought also about Pod::ProjectDocs for pod2html conversion, but
# the 0.31 version has some bugs (it cannot handle umlauts and
# entities correctly, see
# http://rt.cpan.org/Ticket/Display.html?id=25123)
# 0.34 has additional problems: it depends on Syntax::Highlight::Universal,
# which fails nearly everywhere, and the Pod-ProjectsDocs tarball is
# broken because of some additional dot files from MacOSX.
# 0.38 has no progress on the Syntax::Highlight::Universal issue.
# 0.40 has fixed the Syntax::Highlight::Universal problem, but there's
# another one: =encoding directives are not handled

my $bbbike_html_rule;
my $pod2html;
my $patched_marek_pod_html = "$ENV{HOME}/work2/MarekPodHtml";
if (-d $patched_marek_pod_html) {
    # Currently the html documentation is built using a patched
    # Marek::Pod::Html. To use it, do the following:
    #
    #    mkdir -p ~/work2
    #    cd ~/work2
    #    git clone https://github.com/eserte/MarekPodHtml.git
    #    cd MarekPodHtml
    #    perl5.20.3 Makefile.PL && make all test
    #
    my $old_perl = 'perl5.20.3'; # with recent perls there are "Bizarre copy of ARRAY" errors --- need an old perl here
    if (!is_in_path($old_perl)) {
	warn <<EOF;
NOTE: no old perl ($old_perl) available, which is required for
      generating the html documentation. Please compile or get one.
EOF
    }
    $pod2html = "$old_perl -I$patched_marek_pod_html/blib/lib $patched_marek_pod_html/blib/script/mpod2html";
} else {
    warn <<EOF;
NOTE: no patched MarekPodHtml available, which is required for
      generating the html documentation. Please look into
      Makefile_admin_PL for instrcutions
EOF
    if (-e "$Config{sitebin}/mpod2html") {
	$pod2html = "$Config{sitebin}/mpod2html";
    } else {
	$pod2html = is_in_path("mpod2html");
    }
}

use constant POD_POD   => 0;
use constant POD_HTML  => 1;
use constant POD_TITLE => 2; # XXX do not put double quotes into title
#        podfile         htmlfile             title
my @pods = 
    (
     [qw(README          doc/README.html),        "README fr BBBike"],
     [qw(README.english  doc/README.english.html),"README for BBBike"],
     [qw(bbbike.pod      doc/bbbike.html),        "Dokumentation fr BBBike"],
     [qw(doc/links.pod   doc/links.html),     "Links"],
     [qw(doc/bbd.pod     doc/bbd.html),           "BBD file format"],
     [qw(doc/bbbike_internals.pod  doc/bbbike_internals.html),        "BBBike Internals"],
     );
my $all_pods = join " ", map { $_->[POD_POD] } @pods;
my $all_html = join " ", map { $_->[POD_HTML] } @pods;
my $really_all_html = "$all_html doc/podindex.html";
if (defined $pod2html) {
    $bbbike_html_rule = <<EOF;
$all_html:	$all_pods Makefile
	$pod2html -stylesheet html/bbbikepod.css -tocname http://bbbike.sourceforge.net/index -notoc -nowarnings $all_pods
	\$(CLEAN_AFTER_POD2HTML)
	mv bbd.html doc/
	mv bbbike_internals.html doc/
	mv links.html doc/
	mv podindex.html doc/
	mv bbbike.html doc/
	mv README.html doc/
	mv README.english.html doc/
	# Fix warnings (http://rt.cpan.org/Public/Bug/Display.html?id=21622)
	# Fix links
	for f in $really_all_html; do \\
		perl -i -nle 'if (!/^Cannot find page.*on line/) { print }' \$\$f; \\
		perl -i -pe 's{(html/bbbikepod.css)}{../\$\$1}g' \$\$f; \\
		perl -i -pe 's{(images/)}{../\$\$1}g' \$\$f; \\
		perl -i -pe 's{Generated by Marek::Pod::HTML.*on.*}{}' \$\$f; \\
	done
	chmod ugo+r $really_all_html
EOF
} else {
    print STDERR <<EOF;
*** NOTE: if you want to build HTML documentation using "make bbbikedoc"
*** you should install Marek::Pod::HTML to get best results
EOF
    my $pod2html = "pod2html";
    $bbbike_html_rule = "";
    for my $poddef (@pods) {
	$bbbike_html_rule .= <<EOF
$poddef->[POD_HTML]: $poddef->[POD_POD]
	$pod2html --title "$poddef->[POD_TITLE]" $poddef->[POD_POD] > $poddef->[POD_HTML]
	\$(CLEAN_AFTER_POD2HTML)

EOF
    }
}

######################################################################
# Bundled distributions
my(@bundled_perl_module_dists) =
    qw(Tk-ContextHelp Tk-Enscript Tk-Getopt Tk-Autoscroll Tk-PathEntry
       Tk-CanvasFig BikePower savevars GD-Convert Tk-Splash
       Algorithm-GooglePolylineEncoding Http Msg
       Tk-SREZIC Tk-CanvasBalloon Tk-CanvasUtil Tk-LogScale
       Tk-ColorFlowChooser Tk-LayerEditor Tk-Placement
       Tk-StippleLine Tk-WidgetDump Win32Util WWWBrowser
       Tk-FlatCheckbox Tk-FreeDesktop-Wm
      );
my $EXTMODCPANTOP = "$ENV{HOME}/src/CPAN";
if (!-e $EXTMODCPANTOP) {
    $EXTMODCPANTOP = "$ENV{HOME}/work2";
}
my $EXTMODPERLTOP = "$ENV{HOME}/src/perl";
if (!-e $EXTMODPERLTOP) {
    $EXTMODPERLTOP = "$ENV{HOME}/work2";
}
my(@EXTMODCPAN, @EXTMODPERL);
for my $module_dist (@bundled_perl_module_dists) {
    if (-e "$EXTMODCPANTOP/$module_dist") {
	push @EXTMODCPAN, $module_dist;
    } elsif (-e "$EXTMODPERLTOP/$module_dist") {
	push @EXTMODPERL, $module_dist;
    } else {
	if (defined $ENV{USER} && $ENV{USER} eq 'eserte') {
	    warn "Can't find $module_dist neither in $EXTMODCPANTOP nor in $EXTMODPERLTOP\n";
	}
    }
}
my $EXTMODCPAN = join(" ", @EXTMODCPAN);
my $EXTMODPERL = join(" ", @EXTMODPERL);

######################################################################
# FreeBSD

my $fbsdportsdir;
my $fbsdportsdir_is_current = 0;
if ($^O eq 'freebsd') {
    if (-d "$ENV{HOME}/work2/freebsd/ports") {
	$fbsdportsdir = "$ENV{HOME}/work2/freebsd/ports";
	$fbsdportsdir_is_current = 1;
    } else {
	$fbsdportsdir = "/usr/ports";
	if (do { require Sys::Hostname; Sys::Hostname::hostname eq 'cvrsnica.herceg.de' }) {
	    # On cvrsnica portsnap is run regularily.
	    $fbsdportsdir_is_current = 1;
	}
    }
}

######################################################################
# Test coverage
{
    my @ignore = (
	# - /tmp/osm2bbd.t- -> temporary copy of an already covered module copied here
	'/tmp/osm2bbd.t-.*',
	# imported CPAN modules (see also "Bundled distributions" above)
	'lib/BikePower.pm',
	'lib/Tk/ContextHelp.pm',
	'lib/Tk/FlatCheckbox.pm',
	'lib/Tk/FlatRadiobutton.pm',
	'lib/Tk/FreeDesktop/Wm.pm',
	'lib/Tk/Getopt.pm',
	'lib/Tk/PathEntry.pm',
	'lib/enum.pm',
	'lib/your.pm',
        # imported private src/perl modules
	'lib/Http.pm',
	'lib/Met/Wind.pm',
	'lib/Msg.pm',
	'lib/Tk/CanvasBalloon.pm',
	'lib/Tk/CanvasUtil.pm',
	'lib/Tk/Placement.pm',
    );
    my $std_devel_cover_options = join(',', map { "+ignore,$_" } @ignore);
    $bbbike_makefile_admin .= <<"EOF";
# Test coverage
STD_DEVEL_COVER_OPTIONS=$std_devel_cover_options

EOF
}

$bbbike_makefile_admin .= <<'EOF'
######################################################################
# DEPLOYMENTS

# The following variables may be used in mapserver/brb/Makefile
BBBIKE_HOSTEUROPE_USER=		root
BBBIKE_HOSTEUROPE_HOST=		bbbike.de
BBBIKE_HOSTEUROPE_APACHE_DIR=	/srv/www/bbbike-webserver/
RSYNC=				rsync

######################################################################
# DISTRIBUTIONS

TMPDIR?=	/tmp
BBBIKE_TMP=     $(TMPDIR)/$(DISTNAME)

# cbbbike #####################
# XXX evaluate if these targets are still useful
# XXX it's missing MANIFEST.cbbbike from bbbike-RCS, which is anyway missing some files
# XXX also the *dos* targets look quite archaic

CBBBIKE_ARCH=      $(TMPDIR)/cbbbike.zip
CBBBIKE_DOS_ARCH=  $(TMPDIR)/cbbbike_dos.zip
RECODE=            recode

.PHONY: develdist makepatch cbbbikedist cbbbikedistdit
.PHONY: cbbbikedosdist cbbbikedosdistdir cgidist cgidistdir

cbbbikedist: cbbbikedistdir
	$(RM_F) $(CBBBIKE_ARCH)
	cd $(TMPDIR) && $(ZIP) $(ZIPFLAGS) $(CBBBIKE_ARCH) $(DISTNAME)

cbbbikedistdir:
	$(RM_RF) $(BBBIKE_TMP)
	$(PERL) -I$(PERL_ARCHLIB) -I$(PERL_LIB) -MExtUtils::Manifest=manicopy,maniread \
                -e "manicopy(maniread('MANIFEST.cbbbike'),'$(BBBIKE_TMP)', '$(DIST_CP)');"

# cbbbikedosdist: wie cbbbikedist, nur wird noch zustzlich mit recode
# nach cp850 (besser als cp437) konvertiert
cbbbikedosdist: cbbbikedosdistdir
	$(RM_F) $(CBBBIKE_DOS_ARCH)
	cd $(TMPDIR) && $(ZIP) $(ZIPFLAGS) $(CBBBIKE_DOS_ARCH) $(DISTNAME)

cbbbikedosdistdir: cbbbikedistdir
	cd $(BBBIKE_TMP) && chmod u+w *; $(RECODE) latin1:cp850 *.pm cbbbike; chmod 755 cbbbike
	cd $(BBBIKE_TMP)/data && chmod u+w *; $(RECODE) latin1:cp850 *

######################################################################
# predist

predist:
	cd data && ${MAKE}
	${MAKE} generatemods
	${MAKE} extmod
	${MAKE} copyextmod
	${MAKE} bbbikedoc
	${MAKE} distcheck
	${MAKE} bundles

######################################################################
# DOCUMENTATION

CLEAN_AFTER_POD2HTML=$(RM_F) pod2html-*cache pod2htm?.?~~

README:	README.tpl BBBikeVar.pm
	tpage --define lang=DE --eval_perl README.tpl > README~
	mv README~ README

README.english: README.tpl BBBikeVar.pm
	tpage --define lang=EN --eval_perl README.tpl > README.english~
	mv README.english~ README.english

EOF
. $bbbike_html_rule . <<'EOF'

doc/HOWTO_edit_bbbike_data.html: doc/HOWTO_edit_bbbike_data.xml doc/Makefile.doc
	cd doc && env LANG=C LC_ALL=C $(MAKE) -f Makefile.doc HOWTO_edit_bbbike_data.html

clean_pod2html:

EOF
. "bbbikedoc:	README README.english $all_html doc/HOWTO_edit_bbbike_data.html\n" . <<'EOF'

# Alternative Pod generator using Pod::ProjectDocs, looks like CPAN docs
pod-projdocs:
	pod2projdocs -title BBBike -desc "Der Fahrradroutenplaner fr Berlin und Brandenburg" -verbose -out /tmp/pods/ -lib `pwd` -except 'projects/' -except '/blib/lib/' -except 'lib/5\.\d+\.\d+/' -except 'port/.*/inc/' -except 'cgi/(bbbike-test(\.en)?|bbbike\.en|bbbike2|bbbikegooglemap2)'

######################################################################
# PORTS, BINDISTS...

######################################################################
# FreeBSD

EOF
. <<"EOF"
fbsdport:
	cd port/freebsd && env TMPDIR=\$(TMPDIR) ./mkport.pl -portsdir $fbsdportsdir

EOF
. <<'EOF'
#XXX -useversion devel NYI
#fbsdport-devel:
#	cd port/freebsd && env TMPDIR=$(TMPDIR) ./mkport.pl -useversion devel

fbsdport-distfiles-link:
	[ -d /usr/ports/distfiles ] && \
	[ -w /usr/ports/distfiles ] && \
	[ ! -e /usr/ports/distfiles/BBBike-$(VERSION).tar.gz ] && \
	ln -s ${.CURDIR}/distfiles/BBBike-$(VERSION).tar.gz /usr/ports/distfiles || \
	exit 0

fbsdportdiff: fbsdport do-fbsdportdiff

fbsdportdiff-devel: fbsdport-devel do-fbsdportdiff

EOF
. <<"EOF"
do-fbsdportdiff:
	if @{[ $fbsdportsdir_is_current ? "true" : "false" ]} ; \\
	then \\
	    echo "INFO: first updating ports directory..."; \\
	    (cd $fbsdportsdir/german/BBBike && cvs update); \\
	    echo "INFO: now creating patch..."; \\
	    makepatch --exclude-vc --exclude 'work/*' $fbsdportsdir/german/BBBike \$(TMPDIR)/BBBike > \$(TMPDIR)/bbbike-freebsdport.diff; \\
	else \\
	    echo "WARNING: diff'ing against possibly outdated ports tree"; \\
	    makepatch --exclude 'work/*' $fbsdportsdir/german/BBBike \$(TMPDIR)/BBBike > \$(TMPDIR)/bbbike-freebsdport.diff ; \\
	fi

EOF
. <<'EOF'
VARPORTDIR=/var/tmp/BBBike
TMPPORTDIR=$(TMPDIR)/BBBike

# Taken form porters handbook, Automated package list creation
fbsdport-pkg-plist-check: fbsdport
	perl -le 'exit !(shift =~ m{^/var/tmp/})' ${VARPORTDIR}
	rm -rf ${VARPORTDIR}
	mkdir ${VARPORTDIR}
	mtree -U -f /etc/mtree/BSD.local.dist -d -e -p ${VARPORTDIR}
	cd ${TMPPORTDIR} && make clean depends PREFIX=${VARPORTDIR}
	(cd ${VARPORTDIR} && find -d * -type d) > ${TMPDIR}/OLD-DIRS
	cd ${TMPPORTDIR} && make install PREFIX=${VARPORTDIR}
	(cd ${VARPORTDIR} && find -d * \! -type d) | sort > ${TMPDIR}/pkg-plist
	(cd ${VARPORTDIR} && find -d * -type d) | comm -13 ${TMPDIR}/OLD-DIRS - | sed -e 's#^#@dirrm #' | perl -le 'print join "", sort { (length($$b) <=> length($$a)) || ($$b cmp $$a) } <>' >> ${TMPDIR}/pkg-plist
	diff -u ${TMPPORTDIR}/pkg-plist ${TMPDIR}/pkg-plist

######################################################################
# Windows

# instructions for building the windows distribution may be found in
# port/windows/Makefile

######################################################################
# Debian

# Debian packages needed for (subparts of) BBBike, loosely
# categorized:
DEB_DEVEL=	git-core bison byacc flex g++ bmake gcc \
		libc6-dev libinline-perl strace rsync less make
DEB_TEST=	libimage-info-perl libwww-mechanize-perl libemail-sender-perl libemail-mime-perl
# Note: libpango-perl not available in lenny, only squeeze
DEB_DRAW=	libgd3 \
		netpbm libgd-gd2-perl ttf-bitstream-vera fonts-dejavu \
		libpdf-create-perl libcairo-perl libpango-perl libfont-afm-perl \
		libtest-number-delta-perl
DEB_DRAW_OPTIONAL= perlmagick libimager-perl imagemagick libsvg-perl librsvg2-bin
DEB_MAIL=	libemail-mime-perl libemail-sender-perl libmailtools-perl
DEB_DATA=	libstring-approx-perl libdbd-xbase-perl libxml-parser-perl \
		libxml-twig-perl libdbi-perl libtie-ixhash-perl \
		libobject-realize-later-perl libmldbm-perl \
		libclass-accessor-perl libarchive-zip-perl \
		libtemplate-perl libxml-libxml-perl libxml-simple-perl \
		libyaml-perl libyaml-syck-perl libjson-xs-perl \
		libtext-unidecode-perl libdata-compare-perl agrep \
		pkwalify
DEB_MISC=	libdate-calc-perl libdatetime-perl libimage-exiftool-perl \
		libgeo-metar-perl libtext-csv-xs-perl libxml2-utils libdb-file-lock-perl \
		libarray-heap-perl
DEB_CGI=	libapache-session-perl libfile-counterfile-perl \
		libdigest-md5-perl libmodule-metadata-perl libimager-qrcode-perl
# If apache-perl is installed, then do not install apache2!
DEB_APACHE=	apache2
DEB_MAPSERVER=	mapserver-bin cgi-mapserver

# Debian packages from own repo
# Please add something like this (please replace $codename by something like "bookworm", "jammy" etc.)
#
#    deb http://mydebs.bbbike.de $codename main
#
# to a file a /etc/apt/sources.list.d/mydebs.bbbike.list
#
# On ubuntu systems you can run instead
# 
#    add-apt-repository ppa:eserte/bbbike
#
OWN_DEB=	libapache-session-counted-perl libpalm-palmdoc-perl \
		libgd-svg-perl libgeo-spacemanager-perl \
		libgeo-distance-perl libgeo-distance-xs-perl \
		libobject-iterate-perl libcdb-file-perl \
		libgeo-coder-bing-perl libgeo-coder-googlev3-perl libgeo-coder-osm-perl \
		libgeo-proj4-perl

# All packages needed for full-flavoured Web server operation:
DEB_ALL_CGI=	$(DEB_DEVEL)      \
		$(DEB_TEST)       \
		$(DEB_DRAW)       \
		$(DEB_MAIL)       \
		$(DEB_DATA)       \
		$(DEB_MISC)       \
		$(DEB_CGI)        \
		$(DEB_MAPSERVER)  

# All packages needed for Apache operation:
DEB_ALL_APACHE= $(DEB_ALL_CGI) $(DEB_APACHE)

# Complete list of required modules for full operation.
# This is what is available on a typical debian/lenny system
# XXX XXX XXX Should not duplicate the above lists XXX XXX XXX
DEB_ALL=	libalgorithm-diff-perl libapache-session-perl libarchive-zip-perl \
		libbsd-resource-perl libclass-accessor-perl libdata-compare-perl \
		libdate-pcalc-perl libdb-file-lock-perl libdbd-mysql-perl \
		libdbd-xbase-perl libdbi-perl libdbix-class-perl libdevel-leak-perl \
		libfile-readbackwards-perl libflickr-api-perl libfont-afm-perl \
		libgd-gd2-perl libgeo-coder-google-perl \
		libgeo-metar-perl libimage-exiftool-perl \
		libimage-info-perl libimager-perl libinline-perl libipc-run-perl \
		libjson-xs-perl libmailtools-perl libemail-mime-perl libemail-sender-perl libmldbm-perl \
		libobject-realize-later-perl libpdf-create-perl \
		libstatistics-descriptive-perl libstring-approx-perl libsvg-perl \
		libtemplate-perl libtest-differences-perl libtest-simple-perl \
		libtext-csv-xs-perl libtext-table-perl libtext-unidecode-perl \
		libtie-ixhash-perl libtk-gbarr-perl libtk-histentry-perl libtk-pod-perl \
		libwww-mechanize-formfiller-perl libwww-mechanize-perl libwww-perl \
		libwww-shorten-perl libx11-protocol-perl libxml-libxml-perl \
		libxml-parser-perl libxml-sax-perl libxml-simple-perl libxml-twig-perl \
		libyaml-perl libyaml-syck-perl perl perl-modules perl-tk perlmagick \
		libhtml-format-perl libmath-round-perl libgdal-perl fonts-dejavu \
		libcairo-perl libpango-perl mapserver-bin cgi-mapserver \
		libdigest-md5-perl libmodule-metadata-perl

# XXX Does not work, see below
install-debian-for-cgi:	install-debian-packages-for-cgi \
			install-debian-owndebs-for-cgi

install-debian-packages-for-apache:
	${APT} install ${DEB_ALL_APACHE}

install-debian-packages-for-cgi:
	${APT} install ${DEB_ALL_CGI}

install-debian-packages-for-all:
	${APT} install ${DEB_ALL} ${OWN_DEB}

mydebs-bbbike-de-add:
	cd misc/apt-sources.list.d && sudo make add

mydebs-bbbike-de-remove:
	cd misc/apt-sources.list.d && sudo make remove

######################################################################
# par

pardist:
	test -f $(BBBIKE_DISTFILE_SOURCE)
	zcat $(BBBIKE_DISTFILE_SOURCE) | (cd /tmp && tar xfv -)
	$(PERL) -MExtUtils::Manifest=maniread -e 'print join("\n", sort keys %{ maniread() })' | grep -v '^bbbike$$' > /tmp/bbbike-MANIFEST
	cd /tmp/BBBike-$(BBBIKE_STABLE_VERSION) && \
	    pp -A /tmp/bbbike-MANIFEST -vv -o /tmp/bbbike-$(BBBIKE_STABLE_VERSION)-$(OSNAME) bbbike

########################################################################

Makefile : Makefile_admin_PL

# Use with caution. Or do things manually.
alldist: predist dist dist_mv fbsdport fbsdport-distfiles-link

dist_mv:
	[ -d distfiles ] && true || mkdir distfiles
	mv -i BBBike-$(VERSION).tar.gz distfiles/

autoload:
	./miscsrc/make_autoload.pl -dd -f \
	    Strassen/Core.pm Strassen/Fast.pm Strassen/Kreuzungen.pm \
	    Strassen/MultiStrassen.pm \
	    Strassen/Strasse.pm Strassen/StrassenNetz.pm Strassen/Util.pm

# Pfad zum mymake-Skript, das Makefile.PL's fr ~/lib/perl erzeugt
# Da vieler meiner Skripte Test.pm verwenden, wird das nicht mit 5.004 gehen
# XXX mymake ist nur auf meiner Platte vorhanden!
EOF
		    . "MYEXTMODMAKE=	\$(HOME)/src/mymake -perl $^X\n"
		    . <<EOF
# normales Erzeugen mit: \$(FULLPERL) Makefile.PL
# "offizielle" Module
EXTMODCPANTOP=	$EXTMODCPANTOP
EXTMODCPAN=	$EXTMODCPAN
# "experimentelle" Module
EXTMODPERLTOP=	$EXTMODPERLTOP
EXTMODPERL=	$EXTMODPERL

EOF
		    . <<'EOF'

echoextmodcpantop:
	@echo $(EXTMODCPANTOP)

echoextmodcpan:
	@echo $(EXTMODCPAN)

echoextmodperltop:
	@echo $(EXTMODPERLTOP)

echoextmodperl:
	@echo $(EXTMODPERL)

# baut und installiert die externen Module wie BikePower etc.
# verwendet Xfvb oder Xnest
# XXX testen, ob man berhaupt einen X-Server hat
#	alternative: Xnest -ac -geometry 640x400 :121 &
#
#
# To selectively build and install modules, use something like this:
#
#    make extmod copyextmod EXTMODPERL=Tk-LayerEditor EXTMODCPAN=
#
extmod:
	Xvfb -fp /usr/X11R6/lib/X11/fonts/misc -ac :121 & \
	_XVFB_PID=$$!; \
	DISPLAY=:121; \
	for i in $(EXTMODCPAN); do \
	    echo $$i; \
	    cd $(EXTMODCPANTOP)/$$i; \
		$(MYEXTMODMAKE) && \
		make && \
		BATCH=yes make test && \
		make install; \
	done; \
	for i in $(EXTMODPERL); do \
	    echo $$i; \
	    cd $(EXTMODPERLTOP)/$$i; \
		$(MYEXTMODMAKE) && \
		make && \
		BATCH=yes make test && \
		make install; \
	done; \
	echo Kill process $$_XVFB_PID; \
	kill -TERM $$_XVFB_PID
EOF
. <<EOF

copyextmod:
	$^X ./miscsrc/link_ext_mod.pl -copy -v
EOF
. <<'EOF'

generatemods: Strassen/Generated.pm

bundles:
	$(FULLPERL) Makefile.PL -action make_bundle && \
	    $(FULLPERL) Makefile.PL -action make_small_bundle && \
	    $(FULLPERL) Makefile.PL -action make_cgi_bundle && \
	    $(FULLPERL) Makefile.PL -action make_psgi_bundle && \
	    $(FULLPERL) Makefile.PL -action make_windist_bundle

tasks:	bundles
	$(FULLPERL) miscsrc/make_task.pl -o Task/BBBike/windist -name Task::BBBike::windist -bundle Bundle/BBBike_windist.pm -with-tests use_ok

Strassen/Generated.pm: Strassen/Generated_src.pm Strassen.pm
	chmod u+w Strassen/Generated.pm
	echo "1;" > Strassen/Generated.pm
	cd Strassen && perl -I../lib Generated_src.pm
	perl -Ilib -c Strassen/Generated_src.pm
	chmod ugo+r,-w Strassen/Generated.pm

# XXX still used by cdrom/cdrom.pl
echoversion:
	@echo 1>&2 This rule is obsolete ... use BBBikeVar.pm
	@echo $(VERSION)

# Fix the permissions. Needed mainly for correct cgi/mod_perl
# operation.
permission:	permission-cgi permission-mapserver permission-data permission-other

permission-other:
	perl -MExtUtils::Manifest=maniread -le 'print join "\n", sort keys %{ maniread("MANIFEST") }' | xargs chmod ugo+r

permission-data:
	cd data && ${PMAKE} fix-permissions

permission-cgi:
	cd cgi && ${PMAKE} permissions

permission-mapserver:
	if [ -d mapserver/brb/Makefile ] ; then cd mapserver/brb && ${PMAKE} permissions; fi

.PHONY: data

data:
	cd data && ${MAKE}

BBBikeVar.tpl: BBBikeVar.pm
	${PERL} miscsrc/ttdump --rerun-notice "${MAKE} BBBikeVar.tpl" --package BBBike --prefix BBBike BBBikeVar.pm > BBBikeVar.tpl

clean-cache:
	-rm -f tmp/b_de_*
	-rm -f tmp/m_de_*
	-rm -f tmp/*.cache
	-rm -rf cache/*

distcheck-fatal:
	perl -I. -MBBBikeBuildUtil=monkeypatch_manifind -MExtUtils::Manifest=fullcheck -e 'monkeypatch_manifind(); ($$missing,$$extra) = fullcheck; if (@$$missing) { die "Missing files found!\n" } if (@$$extra) { die "Extra files found\n" }'

######################################################################
# Towards git
bbbike-rcs-to-cvs-repo:
	[ -d ${.CURDIR}/../bbbike-cvs-rcs ]
	cd ${.CURDIR} && rcs2cvsrepo.pl -f ${.CURDIR}/../bbbike-cvs-rcs/bbbike

git-update-local:	bbbike-rcs-to-cvs-repo
	[ -d ${.CURDIR}/../bbbike-gitlocal ]
	cd ${.CURDIR}/../bbbike-gitlocal && env CVS_RSH=ssh git cvsimport -v -d ${.CURDIR}/../bbbike-cvs-rcs bbbike -a

git-update-sf:	do-git-update-sf data-sf-fix-permissions

do-git-update-sf:
	[ -d ${.CURDIR}/../bbbike-gitsf ]
	cd ${.CURDIR}/../bbbike-gitsf && env CVS_RSH=ssh git cvsimport -v -d :ext:eserte@bbbike.cvs.sourceforge.net:/cvsroot/bbbike bbbike -a

data-sf-fix-permissions:
	cd ${.CURDIR}/../bbbike-gitsf/data && ${MAKE} fix-permissions

git-update: git-update-local git-update-sf

git-backup:
	[ -e $(HOME)/bak ]
	rsync -a ../bbbike-gitsf/ $(HOME)/bak/bbbike-gitsf/
	rsync -a ../bbbike-gitlocal/ $(HOME)/bak/bbbike-gitlocal/
	rsync -a ../bbbike-cvs-rcs/ $(HOME)/bak/bbbike-cvs-rcs/

######################################################################
# TESTS

# Why this does not work with -j 2? Empty script files, immeditaly finished?
#test-with-protocol:	test-with-protocol-code \
#			test-with-protocol-data
#test-with-protocol-code:
#test-with-protocol-data:

test-with-protocol: permission
	DATE=`date +%Y%m%d`;\
	LOGDIR="$(HOME)/log/bbbike"; \
	    [ -d $$LOGDIR ] || mkdir -p $$LOGDIR; \
	    envinfo () { \
		echo -n "git describe: "; git describe --match 'RELEASE_*'; \
		echo "git status: "; git status; \
		echo; \
		echo -n "uname -a: "; uname -a; \
		echo; \
	    } ; \
	    export HARNESS_NOTTY=1;\
	    export HARNESS_TIMER=1;\
	    export BBBIKE_LONG_TESTS=1;\
	    export AUTOMATED_TESTING=1;\
	    envinfo >$$LOGDIR/bbbike-test-$$DATE 2>&1;\
	    $(MAKE) ext  >>$$LOGDIR/bbbike-test-$$DATE 2>&1;\
	    if ! $(MAKE) test distcheck >>$$LOGDIR/bbbike-test-$$DATE 2>&1; then \
		echo "$(MAKE) test or distcheck failed, please look at $$LOGDIR/bbbike-test-$$DATE for output";\
		has_failure=yes;\
	    fi; \
	    envinfo >$$LOGDIR/bbbike-test-data-$$DATE 2>&1;\
	    if ! (cd data && $(MAKE) all slow-checks really-slow-checks >>$$LOGDIR/bbbike-test-data-$$DATE 2>&1); then \
		echo "$(MAKE) all slow-checks really-slow-checks failed, please look at $$LOGDIR/bbbike-test-data-$$DATE for output";\
		has_failure=yes;\
	    fi; \
	    [ "$$has_failure" = "" ]

######################################################################
# COVERAGE

coverage: coverage-nonext coverage-ext coverage-result

coverage-nonext:
	env BBBIKE_TEST_NO_NETWORK=1 BBBIKE_TEST_NO_CGI_TESTS=1 PERL5OPT=-MDevel::Cover DEVEL_COVER_OPTIONS=${STD_DEVEL_COVER_OPTIONS} ${PERL} -MTest::Harness -e 'runtests(@ARGV)' t/*.t ESRI/t/*.t

coverage-ext:
	-env PERL5OPT=-MDevel::Cover DEVEL_COVER_OPTIONS=+ignore,'\bext/.*/t/' ${PERL} -Ilib -MTest::Harness -e 'runtests(@ARGV)' ext/*/t/*.t

coverage-singlefile:
	@if [ "${COVERFILE}" = "" ]; then echo "Please define COVERFILE"; false; fi
	env BBBIKE_TEST_NO_NETWORK=1 BBBIKE_TEST_NO_CGI_TESTS=1 PERL5OPT=-MDevel::Cover DEVEL_COVER_OPTIONS=${STD_DEVEL_COVER_OPTIONS} ${PERL} -MTest::Harness -e 'runtests(@ARGV)' "${COVERFILE}"

# Some tests have check_cgi_testing set, but do not run server-side.
coverage-singlefile-cgitest:
	@if [ "${COVERFILE}" = "" ]; then echo "Please define COVERFILE"; false; fi
	env BBBIKE_TEST_NO_NETWORK=1 BBBIKE_TEST_NO_CGI_TESTS=0 PERL5OPT=-MDevel::Cover DEVEL_COVER_OPTIONS=${STD_DEVEL_COVER_OPTIONS} ${PERL} -MTest::Harness -e 'runtests(@ARGV)' "${COVERFILE}"

coverage-result:
	${PERL} -S cover
	-rm -f MYMETA.json.lock
	-rm -f ext/*/MYMETA.json.lock

coverage-open:
	${PERL} -Ilib -MWWWBrowser -e 'WWWBrowser::start_browser(shift)' `pwd`/cover_db/coverage.html

EOF
;

1;

__END__
