#!/bin/bash

. ../config-settings.bash


gcc_archtype=$( gcc -v 2>&1 | grep "Target" | awk '{ print $2}' | sed 's/-.*//' )

if [ $gcc_archtype = "i686" ] ; then
  bitness=32
elif [ $gcc_archtype = "x86_64" ] ; then
  bitness=64
else
  echo "Warning: failed to determine bitness of gcc compiler" >&2
fi

if [ ! -f cpan.tar.gz ] ; then
    echo "No prepared cpan.tar.gz detected"
    echo "You need to run ./PREPARE.sh to populate cpan/ with the"
    echo "packages 'relocateable-perl' makes use of"
    exit 1
else
    if [ $( ls cpan | wc -l ) = 0 ] ; then
	# Directory is empty
	echo "----"
	echo "Untarring cpan.tar.gz"
	echo "----"
	tar xvzf cpan.tar.gz
    fi
fi

if [ ! -d relocatable-perl-git ] ; then
    echo "----"
    echo "Untarring relocatable-perl (snapshot of git repo)"
    echo "----"
    tar xvzf relocatable-perl-git.tar.gz
fi

gsdist_prefix="$( dirname $PWD)/dist/perl-$version"
perl_src_tarball="$( dirname $PWD)/perl-$version.tar.gz"

# To help the relocatable-perl Perl code find the CPAN packages it makes use of
# Thankfully, these seem to be all 'pure-perl' ones, so their lib/ .pm files
# can be put in one place in the following cpan/ folder
export PERL5LIB=$PWD/cpan

# Some additional options we might light to consider
#
# "-Duse64bitall", # (conditionally add in if 64-bit arch?) # gsdl added
# "-Duseshrplib"


if [ $bitness = "32" ] ; then
  opt_bitness_options="--extra_option -Darchname=i686-linux-gnu"
fi

#if [ ! -f gcc-specs.txt ] ; then
#  echo "----"
#  echo "Generating gcc-specs.txt"
#  echo "----"
#  gcc -dumpspecs > gcc-specs.txt
#fi

# The LSB build environment looks to explicitly link in -lssp (stack smashing protection)
# to harden executables against overflow attacks
# On an LSB enviroment boath libssp.a and libssp.so file are present, which the
# compiler selecting the .so version.  Unfortunately (and somewhat ironically)
# this makes the binary/executables generated less portable to other Linux distributions
# as libssp.so is not always present (e.g. Ubuntu). 

# Long story short, by generating the gcc-specs, we actually get the compiler's
# specs before the LSB tweak that actually explicitly makes the gcc-linker
# include -lssp. This means by controlling gcc when compiling up Perl, 
# we can direct it to the gcc-specs.txt we have, meaning no explicit
# mention of -lssp, leaving it to the internal version of stack smashing protection
# gcc has.

# There is not a big difference between the spec "LSB gcc" and the one produced
# by 'gcc -specdump' (presumably the settings that are compiled into the gcc
# executable itself).  If interested, you can compare the two with:
#
# On 64-bit LSB:
#   diff gcc-specs.txt  /usr/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/specs
# On LSB (32-bit gcc compiler):
#   diff gcc-specs.txt /usr/lib/gcc/i686-pc-linux-gnu/4.1.1/specs
 
 
#-Dccflags="-specs=$PWD/gcc-specs.txt" -Dldflags="-specs=$PWD/gcc-specs.txt" 

relocatable_args=(
     --prefix "$gsdist_prefix"
     --tarball "$perl_src_tarball"

     # If 'cross-compiling' in 64-bit OS with a 32-bit gcc, then control the Config/arch name of the generated Perl
     $opt_bitness_options
    
     # For our build we also want to control the gcc-specs, so it uses internally provided stack smashing protection
     #--extra_option "-Dccflags=-specs=$PWD/gcc-specs.txt"
     #--extra_option "-Dldflags=-specs=$PWD/gcc-specs.txt"
     #--extra_option "-Dlddlflags=-specs=$PWD/gcc-specs.txt -shared -g -O2 -L/usr/local/lib -fstack-protector"

     # The following args suggested by ChatGPT as options that promote portability
     --extra_option "-Dusemymalloc=n" --extra_option "-Duseperlio" --extra_option "-Duselargefiles"

     # In line with more general linux dist's of Perl, want our Greenstone version to support Threads
     --extra_option "-Dusethreads"

     --extra_option "-DOPTIMIZE=-O2"
)


#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'"

perl relocatable-perl-git/build/relocatable-perl-build "${relocatable_args[@]}"

#perl relocatable-perl-git/build/relocatable-perl-build-i686-linux-gnu \
#       --prefix "$gsdist_prefix" \
#       --tarball "$perl_src_tarball" \
#       $opt_bitness_options \
#       `# The following args suggested by ChatGPT as options that promote portability` \
#       --extra_option "-Dusemymalloc=n" --extra_option "-Duseperlio" --extra_option "-Duselargefiles" \
#       `# In line with more general linux dist's of Perl, want our Greenstone version to support Threads` \
#       --extra_option "-Dusethreads" \
#       --extra_option "-DOPTIMIZE=-O2"

#else
#  perl relocatable-perl-git/build/relocatable-perl-build \
#       --prefix "$gsdist_prefix" \
#       --tarball "$perl_src_tarball"
#fi

