#!/bin/bash

. ./config-settings.bash

export LANGUAGE=C
export LANG=C
export LC_ALL=C


if [ ! -d "$PWD/dist" ] ; then
  mkdir -v "$PWD/dist"
fi

PERL_PREFIX="$PWD/dist/perl-$version"

if [ ! -d "$PERL_PREFIX" ] ; then
  mkdir -v "$PERL_PREFIX"
fi


# Initial development was based on compiling up Perl from scratch
# Subsequently the 'perl-reclocatable' git project was found, and this script
# migrated to work with this instead

# Old way (from scratch):

#if [ ! -d perl-$version ] ; then
#    tar xvzf perl-$version.tar.gz
#fi

#cd perl-$version \
#    && gcc -dumpspecs > gcc-specs.txt \
#    && ./Configure -des -Dprefix="$PERL_PREFIX" \
#        -Duse64bitall -Dusemymalloc=n -Duseperlio -Duselargefiles -Dusethreads -Duseshrplib \
#        -Dccflags="-specs=$PWD/gcc-specs.txt" -Dldflags="-specs=$PWD/gcc-specs.txt" \
#        -Dlddlflags="-specs=$PWD/gcc-specs.txt -shared -O2 -g" \
#        -DDEBUGGING=-g -Doptimize=-O2 \
#    && make \
#    && make install \
#    && cd ..


# The above approach got rather convoluted, due to libssp, an internal library used by gcc.
# On LSB this is included as a libssp.so, but on newer Linux distros, only libssp.a.  This meant
# a perl compiled up on one of of LSB machines, failed to run on a more general linux dist
#
# A workaround was found by using gcc-spec files, which had the effect of the LSB machine
# statically linking to libssp.a.  It should have been that the gcc-spec.txt file needed
# editing to steer gcc to statically link libssp, however -- unexpectedly -- an initial
# test using the gcc dumped gcc-spec.txt (done purely to make use the modified gcc compile
# statement worked as before ... 'perl' but with the .a/.so issue) found that gcc now
# used libssp.a.  This was the end result we were looking for, but did not come from
# introduced, controled change.  At this point relocatable-perl was found, and so
# continuation of this approach to generating a portable perl was halted.
#
#
# If ever going back to compilig up Perl this, way, then some Configure options that
# should perhaps be added in are:

# Consider setting the following via CFLAGS ??
# -fstack-protector-strong \
#
# Relocatable perl uses
# -Duserelocatableinc -Dman1dir=none -Dman3dir=none -DDEBUGGING=-g -des -Ui_xlocale


# While using relocatable-perl is overall preferred as an approach, compared with the above,
# this git project makes a non-threaded version.  Most linux distros provide a perl that
# can run Threads, and so the decision was made to align with the latter.  As relocatable-perl
# does not provide a way to pass in extra Configure options, its source code was modified
# to introduce the extra optinos we want

cd relocatable-perl-complete && ./BUILD.sh && cd ..

if [ $? = 0 ] ; then
    if [ ! -f install-cpanminus.pl ] ; then
	wget --no-check-certificate -O - https://cpanmin.us > install-cpanminus.pl
	if [ $? != 0 ] ; then
	    echo "Failed to run 'wget'. Trying 'curl':"
	    curl -s https://cpanmin.us > install-cpanminus.pl
	fi
    fi

    export PATH="$PWD/dist/perl-$version/bin:$PATH"
    cat install-cpanminus.pl | perl - App::cpanminus

    ./BYHAND-INSTALL.sh
    ./CPAN-INSTALL.sh
fi


# There is no perl-$version/bin/perl/libssp on Mac, where uname -s comes back as Darwin (not darwin)
# Also, can't use ldd on Mac, where we need to use otool -L
GSDLOS=`uname -s`
if [ "x$GSDLOS" != "xDarwin" ] ; then
    libssp_file=$( ldd dist/perl-$version/bin/perl | grep libssp | awk -F'=>' '{ print $2 }' | awk '{ print $1 }' )
    /bin/cp "$libssp_file" "$PWD/dist/perl-$version/lib/."
fi

