#!/bin/bash

. ./config-settings.bash

export PATH="$PWD/dist/perl-$version/bin:$PATH"

#####
# Notes
#####
#
# In broad brush strokes, the perl-with-cpan extension replaces the need to have
#   gs2build/perllib/cpan
#
# However, there is still some content in this directory that we currently
# still need to retain

# The following only exists in Perl for Windows (not installable through CPAN on Linux)
#   Win32
#   Win32::Shortcut 


# The following are files GS developers have added in as optional/on the side
# files that look useful to top-up a particular installation

# perllib/cpan/compile-zerossl.sh
# perllib/cpan/compile-dbd-mysql.sh
# perllib/cpan/Ping.tgz

# perllib/cpan/Encode/Locale.pm
# perllib/cpan/Encode/Locale.pm.newForLocaleIssue
# perllib/cpan/Encode/Alias.pm.forLocaleIssue

# The following was also found to be an issue when installing cpan pacages

#???
# Mojolicious.pm  Mojo

# Need 'https' LWP Protocol to support installing modules (using cpanm!)
# via a direct https:// URL, as is done for MARC

#cpanm LWP LWP::Protocol::https
#
#if [ $? != 0 ] ; then
#    echo "" >&2
#    echo "**** Error encountered in cpanm install" >&2
#    echo "" >&2
#    exit 1
#fi


# Below, when cpanm untars some of the downloaded files, warnings about 'unknown keyword" in the
# header of a tar file are output.  These seem to be caused by tar files generated on Macs that
# have been committed to CPAN.
#
# These warnings can be safely ignored, but a potential solution is to
# see if cpmanminus can be tweaked to add in the tar no warning flag -- however
# this itself is a GNU extension for tar
#
# Note that MacOS tar is not the GNU one, so might need something like:
#   tar xf yourFile.tar.gz --warning=no-unknown-keyword || tar xf yourFile.tar.gz
#
# if looking to run our 'perl-with-cpan' extension to produce Mac versions of Greenstone
#
# Ironic, as it's a MacOS tar command that has added in the non-standard keyords anyway!
#
# For more details, see:
#   https://stackoverflow.com/questions/51655657/tar-ignoring-unknown-extended-header-keyword-libarchive-xattr-security-selinux

#export PERL_MM_OPT="CCFLAGS='-spec=$PWD/gcc-spec.txt' LDFLAGS='-spec=$PWD/gcc-spec.txt' LDDLFLAGS='-spec=$PWD/gcc-spec.txt -shared -O2 -L/usr/local/lib -fstack-protector'"

cpanm inc::latest Software::License

# On the DL lab iMac, getpwnam("~") spuriously returns /home/yannz00101
# (due to ITS practice of using '~' for empty name fields in ActiveDirectory settings in accounts linked with automation)
# This causes some of the Perl modules testing (t.tilde) to go wrong:
#   The test detildefy("~~") ends up with /home/yannz00101 due to the getpwnam() issue, meaning the test fails
#
# => Use --no-test here on the modules that get caught up in this

cpanm --no-test Module::Build  
cpanm --no-test ExtUtils::Helpers

cpanm XML::XPath XML::Rules XML::Twig

cpanm App::ChangeShebang

cpanm AudioFile::Info::Ogg::Vorbis::Header::PurePerl\
      Canary::Stability \
      Class::Accessor \
      CGI CGI::Ajax \
      Crypt::UnixCrypt \
      Encode Encode::Locale \
      File::Listing File::RandomAccess \
      HTML::Parser HTML::TokeParser HTML::TokeParser::Simple \
      Image::ExifTool Image::Size \
      LWP \
      Mail::Sendmail \
      MARC::Record MARC::Lint \
      MP3::Info \
      Mojolicious \
      Net::HTTP Net::HTTPS \
      Sub::Override Sub::Uplevel \
      Test::Exception \
      URI \
      WWW::RobotRules 

# Removed the following 28 May 2026, as Kathy found it's no longer needed
# and has been superseded by Sort Naturally GS included in GS perllib.
#      Sort::Naturally \

 
if [ $? != 0 ] ; then
    echo "" >&2
    echo "**** Error encountered in cpanm install" >&2
    echo "" >&2
    exit 1
fi


cpanm \
    HTTP::Config \
    HTTP::Cookies \
    HTTP::Daemon \
    HTTP::Date \
    HTTP::Headers \
    HTTP::Message \
    HTTP::Negotiate \
    HTTP::Request \
    HTTP::Response \
    HTTP::Status

if [ $? != 0 ] ; then
    echo "" >&2
    echo "**** Error encountered in cpanm install" >&2
    echo "" >&2
    exit 1
fi


# Packages GS historically installed from source as part of compilation,
# as they make use XS (binary) stubs
# Notes, earlier version of MD5 already installed in 5.34

cpanm Digest::MD5 JSON::PP JSON::XS JSON Text::CSV

if [ $? != 0 ] ; then
    echo "" >&2
    echo "**** Error encountered in cpanm install" >&2
    echo "" >&2
    exit 1
fi

#Canary-Stability-2013.tar.gz  Digest-SHA-5.92.tar.gz  JSON-XS-4.03.tar.gz  Text-CSV-1.99.tar.gz    XML-Parser-2.34.tar.gz
#Digest-MD5-2.55.tar.gz        JSON-4.10.tar.gz        README               XML-Parser-2.27.tar.gz  XML-Parser-2.41.tar.gz

echo ""
echo "----"
echo "Next steps:"
echo "  1. ./GS-PERL-DIST-TARUP.sh to tar up the generated perl distribution"
echo "  2. ./GS-PERL-DIST-SVN-COMMIT.sh to then svn commit the generated tar file"
echo "----"
echo ""
