#! /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

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

if [ "$2" = "/" ] ; then
  APXS=/usr/lib/apache/bin/apxs
  APACHE=/usr/lib/apache/bin/httpd
  ourPERL5=
else
  APXS=$2/Apache/osdapxs
  APACHE=$2/Apache/usr/lib/apache/bin/httpd
  ourPERL5=$2/modperl/usr/lib/perl5/5.8
  ourPERL5=$ourPERL5:$2/modperl/usr/lib/perl5/sco_perl/5.8
  ourPERL5=$ourPERL5:$2/modperl/usr/lib/perl5/site_perl/5.8
fi

export APXS APACHE

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
  export PERL5LIB
  perl=$perldir/usr/bin/perl
}

#
# We need to add our own build directory to the Perl library, even if
# we're using the system Perl, so that each module picks up the stuff that
# has just been built, before the system stuff.
#
if [ -z "$PERL5LIB" ]; then
  PERL5LIB=$ourPERL5
else
  PERL5LIB=$PERL5LIB:$ourPERL5
fi
export PERL5LIB

shift 2

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} || exit $?

[ $# -gt 0 ] && {
  eval "$@" || exit $?
}
exit 0
