#!/bin/sh

# NAME: install_perl_only_from_tarball_custom_location
#
# OBJECTIVE:
#
# From a previously downloaded release tarball, installs a perl executable for
# testing purposes at a user-specified location.
# Does not install man pages, CPAN modules or other utilities such as 'cpanm'.

# PREREQUISITES:

# I.    Programs in PATH:  'perl', 'wget', 'make'

# II.   Environmental variables:

#       $DOWNLOADS_DIR
#
#       Directory assigned on your machine to receive downloaded tarballs, etc.
#
#       $TEST_JOBS

#       An integer such as 4 or 8, appropriate to the number of cores in your machine,
#       which will determine how many jobs 'make' will attempt to run in parallel.

# III.  USAGE: 2 required command-line arguments, 1 optional command-line argument

#   $ install_perl_only_from_tarball_custom_location \
#         <name> \
#         <installation_directory> \
#         <custom_configure_arguments>

# <name> must be a string like 'perl-5.42.0' which appears in the filename of a perl
# release tarball at a location like this: https://www.cpan.org/src/5.0/perl-5.42.0.tar.gz.

# <installation_directory> must be the name of an already existing directory
# on disk to which the user has write-access.

# <custom_configure_arguments>:  Optional.  Any switches for ./Configure above and beyond
# the basic "-des -Dusedevel".  For example, to build a threaded perl the user
# would supply a third argument "-Dusethreads".

# Adapted from Florian Ragwitz and Karl Williamson
# Reference: https://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Control_flow

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

# Don't let me use uninit vars, and any error is a problem
set -u
set -e
# Print expanded commands
set -x

if [ ! -d $DOWNLOADS_DIR ]; then
  echo "FAILED:  could not locate DOWNLOADS_DIR"
  exit 1
fi

if [ "$#" -eq 2 ]; then
    RELEASE=$1;
    INSTALLDIR=$2
    CONFIG_ARGS=
elif [ "$#" -eq 3 ]; then
    RELEASE=$1;
    INSTALLDIR=$2
    CONFIG_ARGS=$3
else
  echo "$0: FAILED: wrong number of arguments"
  exit 1
fi

if [ ! -d $INSTALLDIR ]; then
  echo "$0: FAILED: could not locate INSTALLDIR"
  exit 1
fi

echo "Handling Perl release: $RELEASE"
set -x
TARBALL="${RELEASE}.tar.gz"
TARBALL_URL="https://www.cpan.org/src/5.0/$TARBALL"

echo $TARBALL_URL

# Clean out any existing installation of this release under $INSTALLDIR
cd $INSTALLDIR
test -d $RELEASE && rm -rf $RELEASE
RELEASEDIR=$INSTALLDIR/$RELEASE
mkdir -p $RELEASEDIR

# Now fetch and unpack the dev release tarball
cd $DOWNLOADS_DIR
wget $TARBALL_URL
tar xzf $TARBALL
cd "$DOWNLOADS_DIR/$RELEASE"

echo "Handling release:   $RELEASE"
echo "To be installed in: $RELEASEDIR"

PERL5OPT=      # In case you have this set, it shouldn't be for these purposes

#make -j${TEST_JOBS} install
#
#cd $RELEASEDIR

# configure so as to install in $RELEASEDIR; don't bother with man pages
# configure and build quietly; redirect STDOUT to /dev/null

CONFIGURE_COMMAND="./Configure -des -Dusedevel"
if [ -n $CONFIG_ARGS ]; then
    CONFIGURE_COMMAND="${CONFIGURE_COMMAND} $CONFIG_ARGS"
fi
CONFIGURE_COMMAND="${CONFIGURE_COMMAND} -Uversiononly -Dprefix=${RELEASEDIR} -Dman1dir=none -Dman3dir=none"

echo "Quietly configuring a perl for installation under $RELEASEDIR"

$CONFIGURE_COMMAND 1>/dev/null

echo "Quietly building and installing a perl for installation under $RELEASEDIR"
make -j${TEST_JOBS} install 1>/dev/null

THISPERL="$RELEASEDIR/bin/perl -I$RELEASEDIR/lib"
$THISPERL -v | head -n 2 | tail -n 1

