#! /bin/ksh
# Script to aid building libapreq when perl hasn't been fully installed
# and none of the other required Apache modules have either.
#
# Arguments are:
# 1) Path to the root of the perl distribution
# 2) Path to the root of the apache distribution

[ $# -eq 2 ] || {
  echo "Usage: SCOBUILD /path/to/perl/dist /path/to/apache/dist"
  exit 2
}

if [ "$2" = "/" ] ; then
  OSDAPX=/usr/lib/apache/bin/apxs
else
  OSDAPX=$2/Apache/osdapxs
fi


apachedir=$2
modperldir=$apachedir/modperl
perl=/usr/bin/perl

[ "$1" = "/" ] || {
  perldir=$1/perl
  perlextdir=$1/perlext

  #
  # Set up the environment for perl, since we will not be using the one
  # that may be installed on the system.
  #
  PATH=$perldir/usr/bin:$PATH; export PATH
  PERL5LIB=${perldir}/usr/lib/perl5/5.8
  PERL5LIB=$PERL5LIB:${perldir}/usr/lib/perl5/sco_perl/5.8
  PERL5LIB=$PERL5LIB:${perldir}/usr/lib/perl5/site_perl/5.8
  PERL5LIB=$PERL5LIB:${perlextdir}/usr/lib/perl5/5.8
  PERL5LIB=$PERL5LIB:${perlextdir}/usr/lib/perl5/sco_perl/5.8
  PERL5LIB=$PERL5LIB:${perlextdir}/usr/lib/perl5/site_perl/5.8
  PERL5LIB=$PERL5LIB:${modperldir}/usr/lib/perl5/5.8
  PERL5LIB=$PERL5LIB:${modperldir}/usr/lib/perl5/sco_perl/5.8
  PERL5LIB=$PERL5LIB:${modperldir}/usr/lib/perl5/site_perl/5.8
  export PERL5LIB
  perl=$perldir/usr/bin/perl
}

getperlvar() {
  eval v=`$perl -V::$1`
  echo $v
}

adir=`getperlvar archlibexp`
perlarch=${perldir}${adir}
adir=`getperlvar privlibexp`
perllib=${perldir}${adir}

PERL_LIB=$perllib
PERL_ARCHLIB=${perlarch}
export PERL_LIB PERL_ARCHLIB

$perl Makefile.PL PERL_LIB=${PERL_LIB} PERL_ARCHLIB=${PERL_ARCHLIB} EVERYTHING=1 NO_HTTPD=1 USE_APXS=1 USE_DSO=1 WITH_APXS=$OSDAPX
