#!/bin/bash

. ./config-settings.bash

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

cpanm ExtUtils::MakeMaker

if [ ! -d XML-Parser-2.47-with-libexpat ] ; then
    tar xvzf XML-Parser-2.47-with-libexpat.tar.gz
fi

cd XML-Parser-2.47-with-libexpat

if [ ! -d expat-1.95.8 ] ; then
    tar xvzf expat-1.95.8.tar.gz 
fi

INSTALL_DIR="$PWD/installed-expat"

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

#cd expat-1.95.8 \
#  && CFLAGS="-fno-stack-protector -fPIC" ./configure --enable-static --disable-shared --prefix="$INSTALL_DIR" \
#  && make \
#  && make install \
#  && cd ..

cd expat-1.95.8 \
  && CFLAGS="-fPIC" ./configure --enable-static --disable-shared --prefix="$INSTALL_DIR" \
  && make \
  && make install \
  && cd ..

# In compiling up the Expat.so, we want to change from the (likely) situation where
# the default flags MakeMaker provides means that libssp (an internal library to gnu's
# libc) is specified.  The follow lines look for 'stack-protector' and change it to
# 'no-stack-protector' if present. 
# We do this so we don't end up dynmaically relying on libssp.so for Expat.so, because some
# distribution of Linux only provide libssp.a. 

##ldflags=$( echo 'use Config ; print $Config{lddlflags} ;' | perl - | sed 's/fstack-protector/fno-stack-protector/g' )
##lddlflags=$( echo 'use Config ; print $Config{lddlflags} ;' | perl - | sed 's/fstack-protector/fno-stack-protector/g' )

#ldflags=$( echo 'use Config ; print $Config{lddlflags} ;' | perl - )
#lddlflags=$( echo 'use Config ; print $Config{lddlflags} ;' | perl - )

#perl Makefile.PL LDFLAGS="$ldflags" LDDLFLAGS="$lddlflags"
#make LDLOADLIBS="$INSTALL_DIR/lib/libexpat.a"
#make install

perl Makefile.PL
make
make install


cd ..

#
# Can now install the XML packages that build on XML::Parser
#

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

# This line moved to CPAN-INSTALL.sh
#cpanm XML::XPath XML::Rules XML::Twig

