################################################################################
#
# perl.cygclass - functions for building CPAN Perl modules
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport 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.
#
# cygport 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 cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/perl.cygclass
#  SYNOPSIS
#  [CPAN_AUTHOR="username"]
#  inherit perl
#  DESCRIPTION
#  Perl is an interpreted programming language used in a variety of software,
#  particularly text processing, network programming, system administration,
#  and CGI scripting.  It is easily extendible with modules written in Perl
#  and/or C/C++.  Thousands of such modules are centrally hosted on the
#  Comprehensive Perl Archive Network (CPAN).
#
#  This cygclass provides definitions for building Perl dependent packages,
#  and functions for building Perl module packages.
#  EXAMPLES
#  A typical CPAN extension package looks like this:
#
#    CPAN_AUTHOR=adamk
#    inherit perl
#    
#    NAME="perl-Archive-Zip"
#    VERSION=1.30
#    RELEASE=1
#    CATEGORY="Perl"
#    SUMMARY="Perl module for working with Zip archives"
#    DESCRIPTION="This module allows a Perl program to create, manipulate, read,
#    and write Zip archive files. Zip archives can be created, or you can read
#    from existing zip files. Once created, they can be written to files, streams,
#    or strings."
#
#  Occasionaly, a CPAN module tries to ask questions while configuring the
#  build, in which case you can override that in src_compile:
#
#    src_compile() {
#      lndirs
#      cd ${B}
#      # answer 'y' to all questions from Makefile.PL
#      yes | perl_compile
#    }
#
#  Perl modules which provide XML::SAX parser engines require an additional
#  step during src_install, e.g.:
#
#    src_install() {
#      cd ${B}
#      perl_install
#      perl_sax_parser_register XML::LibXML::SAX
#    }
#
#  INHERITED BY
#  gtk2-perl.cygclass
#  REQUIRES
#  perl
#****

# cross-compiling is not (yet?) supported
__cross_compiling_error

check_prog_req perl

#****d* perl.cygclass/PERL
#  DESCRIPTION
#  Absolute path to the Perl interpreter.
#****
PERL=/usr/bin/perl

#****d* perl.cygclass/PERL_VERSION
#  DESCRIPTION
#  The major.minor version of the Perl interpreter.
#****
PERL_VERSION=$(${PERL} -MConfig -e 'print $Config{api_revision}.".".$Config{api_version};')

#****d* perl.cygclass/PERL_LIB
#  DESCRIPTION
#  Absolute path to the Perl arch-independent standard library.
#  NOTE
#  Third-party packages must not be installed here; use PERL_VENDORLIB instead.
#****
PERL_LIB=$(${PERL} -MConfig -e 'print $Config{privlib};')

#****d* perl.cygclass/PERL_ARCHLIB
#  DESCRIPTION
#  Absolute path to the Perl arch-specific standard library.
#  NOTE
#  Third-party packages must not be installed here; use PERL_VENDORARCH instead.
#****
PERL_ARCHLIB=$(${PERL} -MConfig -e 'print $Config{archlib};')

#****d* perl.cygclass/PERL_VENDORLIB
#  DESCRIPTION
#  Installation path for third-party arch-independent Perl modules.
#****
PERL_VENDORLIB=$(${PERL} -MConfig -e 'print $Config{vendorlib};')

#****d* perl.cygclass/PERL_VENDORARCH
#  DESCRIPTION
#  Installation path for third-party arch-specific Perl modules.
#****
PERL_VENDORARCH=$(${PERL} -MConfig -e 'print $Config{vendorarch};')

# private
PERL_SITELIB=$(${PERL} -MConfig -e 'print $Config{sitelib};')

#****d* perl.cygclass/LIBPERL
#  DESCRIPTION
#  Link flags for the Perl C library.
#****
LIBPERL="-L${PERL_ARCHLIB}/CORE -lperl"

case ${PN} in
	perl-*) ORIG_PN=${ORIG_PN:-${PN#perl-}} ;;
esac

#****v* perl.cygclass/CPAN_AUTHOR
#  DESCRIPTION
#  The CPAN username of the Perl module's author.  This variable must be set
#  before inherit()ing perl.cygclass to have any effect.  If set, the package
#  HOMEPAGE and SRC_URI are set to their usual locations on CPAN.
#****

if defined CPAN_AUTHOR
then
#****o* perl.cygclass/CATEGORY (perl)
#  DEFINITION
CATEGORY="Perl"
#  NOTE
#  This variable is set only if CPAN_AUTHOR is defined before inherit()ing
#  perl.cygclass.
#****
#****o* perl.cygclass/SUMMARY (perl)
#  DEFINITION
SUMMARY="Perl ${ORIG_PN//-/::} module"
#  NOTE
#  This variable is set only if CPAN_AUTHOR is defined before inherit()ing
#  perl.cygclass.
#****
#****o* perl.cygclass/HOMEPAGE (perl)
#  DESCRIPTION
#  Web address for the Perl module on CPAN.
#  NOTE
#  This variable is set only if CPAN_AUTHOR is defined before inherit()ing
#  perl.cygclass.
#****
HOMEPAGE="http://search.cpan.org/dist/${ORIG_PN}/"

#****o* perl.cygclass/SRC_URI (perl)
#  DESCRIPTION
#  Download location for the Perl module on CPAN.
#  NOTE
#  This variable is set only if CPAN_AUTHOR is defined before inherit()ing
#  perl.cygclass.
#  SEE ALSO
#  mirror_cpan
#****
cpan_author_ftp=${CPAN_AUTHOR^^}
SRC_URI="mirror://cpan/authors/id/${cpan_author_ftp:0:1}/${cpan_author_ftp:0:2}/${cpan_author_ftp}/${ORIG_PN}-${PV}.${CPAN_TARBALL_SUFFIX:-tar.gz}"
unset cpan_author_ftp

fi	# defined CPAN_AUTHOR

#****C* perl.cygclass/perl_compile
#  SYNOPSIS
#  cd $B
#  perl_compile [OPTIONS]
#  DESCRIPTION
#  Configures and builds a Perl module package.  Options, if any, are passed
#  during the configure phase.
#****
perl_compile() {
	if [ -e Build.PL ]
	then
		${PERL} Build.PL --installdirs=vendor "${@}" || error "Module::Build creation failed"
		${PERL} Build || error "Module::Build build failed"
	elif [ -e Makefile.PL ]
	then
		${PERL} Makefile.PL "${@}" PREFIX=/usr INSTALLDIRS=vendor || error "Perl Makefile creation failed"
		cygmake all manifypods OPTIMIZE="${CFLAGS}"
	else
		error "No Perl module detected"
	fi
}

#****T* perl.cygclass/perl_test
#  SYNOPSIS
#  cd $B
#  perl_test
#  DESCRIPTION
#  Runs the test suite for the Perl module.
#****
perl_test() {
	if [ -f Build ]
	then
		${PERL} Build test
	elif [ -f Makefile ]
	then
		make test
	else
		error "No Perl module detected"
	fi
}

#****I* perl.cygclass/perl_install
#  SYNOPSIS
#  cd $B
#  perl_install [OPTIONS]
#  DESCRIPTION
#  Installs the Perl module into $D.  Options, if any, are passed to the
#  install command.
#****
perl_install() {
	if [ -f Build ]
	then
		${PERL} Build install --destdir=${D} "${@}" || error "Module::Build install failed"
	elif [ -f Makefile ]
	then
		cyginstall ${@}
	else
		error "No Perl module detected"
	fi
}

# Functionality moved to __prepperl; this remains for backwards compatibility
perl_postinst() { :; }

#****I* perl.cygclass/perl_fix_shebang
#  SYNOPSIS
#  perl_fix_shebang SCRIPT [SCRIPT ...]
#  DESCRIPTION
#  Fixes the designated interpreter of SCRIPT to PERL.  This would be necessary
#  if the original uses an incorrect path (e.g. /usr/local/bin) or an
#  incorrectly versioned binary.  SCRIPT need not be prefixed by $D.
#****
perl_fix_shebang() {
    __fix_shebang ${PERL} ${D}/${@#${D}}
}

#****I* perl.cygclass/perl_sax_parser_register
#  SYNOPSIS
#  perl_sax_parser_register PARSER_CLASS [PARSER_CLASS ...]
#  DESCRIPTION
#  Generates postinstall and preremove scripts to handle registration of
#  XML::SAX parser drivers with XML::SAX's ParserDetails.ini file.
#****
perl_sax_parser_register() {
	local parser

	dodir /etc/postinstall /etc/preremove
	for parser in $@
	do
		cat >> ${D}/etc/postinstall/${NAME}.sh <<-_EOF
/usr/bin/perl -MXML::SAX -e "XML::SAX->add_parser(q($parser))->save_parsers()"
_EOF
		cat >> ${D}/etc/preremove/${NAME}.sh <<-_EOF
/usr/bin/perl -MXML::SAX -e "XML::SAX->remove_parser(q($parser))->save_parsers()"
_EOF
	done
}

#****o* perl.cygclass/src_compile (perl)
#  DEFINITION
src_compile() {
	lndirs
	cd ${B}
	perl_compile
}
#****

#****o* perl.cygclass/src_test (perl)
#  DEFINITION
src_test() {
	cd ${B}
	perl_test
}
#****

#****o* perl.cygclass/src_install (perl)
#  DEFINITION
src_install() {
	cd ${B}
	perl_install
}
#****

readonly -f perl_compile perl_test perl_install perl_postinst \
            perl_fix_shebang perl_sax_parser_register
