#! /bin/sh

LOGFILE=
BUILDDIR=
PKGROOT=
NUMCPUS=

logmsg() {
  if [ -n "$LOGFILE" ]; then
    echo $@ >&5
  fi
  echo $@
}

logcmd() {
  if [ -n "$LOGFILE" ]; then
    echo "$@" >&5
    eval $@ > $$.__l 2>&1
    retval=$?
    cat $$.__l >&5 2>/dev/null
    rm -f $$.__l
    return $retval
  fi
  eval $@
}

cat << EOF
This script performs a full build of all of the GWXLIBS components. It
relies on the fact that the archives that accompanied this script have
had all of the patches and autoconf work applied to them already. If
you want to see the list of patches applied, and you want to do the
build manually for some reason, the file README.OSD has step-by-step
instructions. That file may offer advice on how to update GWXLIBS in
the future if I (Kean Johnston) am run over by a big red bus.

On a dual Xeon 2.8G system with fairly fast disks, this build takes
about 30 minutes. On a dual P3/500 also with fairly fast disks it
takes about 2 hours.

In order to perform the build and produce a basic distribution tree
layout that you must then clean up to produce a package from (possibly
using mkcdmt) I need you to answer the following three questions:

EOF

echo "How many CPUs does this system have[1]? \c"
if [ -z "$1" ]; then
  read numcpus
else
  numcpus=$1
  echo $numcpus
fi

cat << EOF

Before answering this next question, please be aware that the subdirectory
dist/ under whatever path you specify will be erased without prompting as
soon as the build starts. If you have a work-in-progress tree in dist/
under this directory, please abort this script now and move the dist/
directory aside.
EOF
echo "Where shall I root the distribution hierarchy? \c"
if [ -z "$2" ]; then
  read croot
else
  croot=$2
  echo $croot
fi

cat << EOF

I am fairly certain you will want to keep a record of what happens
during the build. If you do, enter the name of a file to log the
results of this script to. If you don't, please enter the word none.
EOF
echo "Where should I log the results to? \c"
if [ -z "$3" ]; then
  read ldest
else
  ldest=$3
  echo $ldest
fi

if [ "x$ldest" = "xnone" ]; then
  ldest=
fi

if [ -z "$numcpus" ]; then
  NUMCPUS=1
else
  NUMCPUS=$numcpus
fi

if [ -z "$croot" ]; then
  echo "You didn't specify a build destination. Exiting."
  exit 1
else
  PKGROOT=$croot
fi

if [ -n "$ldest" ]; then
  exec 5>$ldest
  LOGFILE=$ldest
  logmsg "Log file for the full build and package prep run of GWXLIBS"
fi

if [ -d $PKGROOT/dist ]; then
  logcmd rm -fr $PKGROOT/dist
fi
logcmd mkdir -p $PKGROOT/dist

MYPWD=`pwd`
startat=`date`

GWXROOT=$MYPWD
export GWXROOT
GWXCDMT=$PKGROOT
export GWXCDMT
GWXDIST=$GWXCDMT/dist
export GWXDIST
BUILDDIR=$MYPWD

cd $BUILDDIR
for dir in *; do
  if [ -d $dir ]; then
    case $dir in
      openssl-0.9.6*) OPENSSL6DIR=$dir ;;
      openssl-0.9.7*) OPENSSL7DIR=$dir ;;
      gwxlibs*) GWXLIBSDIR=$dir ;;
      openldap*) OPENLDAPDIR=$dir ;;
      icu*) ICUDIR=$dir ;;
      xerces*) XERCESDIR=$dir ;;
      xalan*) XALANDIR=$dir ;;
    esac
  fi
done

if [ -z "$OPENSSL6DIR" -o -z "$OPENSSL7DIR" -o -z "$GWXLIBSDIR" -o -z "$OPENLDAPDIR" -o -z "$ICUDIR" -o -z "$XERCESDIR" -o -z "$XALANDIR" ]; then
  logmsg "Missing archives! Cannot continue."
  exit 1
fi

DOPHASE1=1
DOPHASE2=1
DOPHASE3=1
DOPHASE4=1
DOPHASE5=1
DOPHASE6=1
DOPHASE7=1

if [ $DOPHASE1 -eq 1 ]; then
logmsg "Phase 1.0 ... doing the OpenSSL 0.9.6 configure"
cd $BUILDDIR/$OPENSSL6DIR
logcmd ./Configure sco5-gcc-pentiumpro-hpath shared --prefix=/usr --openssldir=/usr/lib/openssl
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenSSL 0.9.6 configuration failed"
  exit 1
fi

logmsg "Phase 1.1 ... doing the OpenSSL 0.9.6 build"
logcmd /bin/make
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenSSL 0.9.6 build failed"
  exit 1
fi

logmsg "Phase 1.2 ... doing the OpenSSL 0.9.6 install"
logcmd mkdir -p $GWXDIST/usr/lib
logcmd cp libcrypto.so.0.9.6 $GWXDIST/usr/lib
logcmd cp libssl.so.0.9.6 $GWXDIST/usr/lib
logmsg "End of phase 1 - OpenSSL build completed"
fi # End of if DOPHASE1

if [ $DOPHASE2 -eq 1 ]; then
logmsg "Phase 2.0 ... doing the OpenSSL 0.9.7 configure"
cd $BUILDDIR/$OPENSSL7DIR
logcmd ./Configure sco5-gcc-pentiumpro-hpath shared --prefix=/usr --openssldir=/usr/lib/openssl
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenSSL 0.9.7 configuration failed"
  exit 1
fi

logmsg "Phase 2.1 ... doing the OpenSSL 0.9.7 build"
logcmd /bin/make
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenSSL 0.9.7 build failed"
  exit 1
fi

logmsg "Phase 2.2 ... doing the OpenSSL 0.9.7 install"
logcmd /bin/make 'INSTALL_PREFIX=$GWXDIST install'
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenSSL 0.9.7 installation failed"
  exit 1
fi
logcmd chmod 755 $GWXDIST/usr/lib/pkgconfig
logcmd rm -f $GWXDIST/usr/lib/pkgconfig/openssl.pc

cd apps
logcmd gcc -o openssl "*.o" ../libssl.so.0.9.7 ../libcrypto.so.0.9.7
logcmd cp -f openssl $GWXDIST/usr/bin/openssl
cd ..

logcmd mv $GWXDIST/usr/lib/openssl/man $GWXDIST/usr/man
logcmd rm $GWXDIST/usr/lib/\*.a
logmsg "End of phase 2 - OpenSSL 0.9.7 build completed"
fi # End of if DOPHASE2

if [ $DOPHASE3 -eq 1 ]; then
logmsg "Phase 3.0 ... doing the GWXLIBS configure"
cd $BUILDDIR/$GWXLIBSDIR
mkdir osr5
cd osr5
logcmd sh ../OSDBUILD
if [ $? -ne 0 ]; then
  logmsg "FATAL: GWXLIBS configuration failed"
  exit 1
fi

logmsg "Phase 3.1 ... doing the GWXLIBS build"
logcmd VERBOSE=1 MAKEJFLAG=-j$NUMCPUS /usr/gnu/bin/gmake
logcmd make
if [ $? -ne 0 ]; then
  logmsg "FATAL: GWXLIBS build failed"
  exit 1
fi

logmsg "Phase 3.2 ... doing the GWXLIBS install"
logcmd /bin/make install DESTDIR=$GWXDIST
if [ $? -ne 0 ]; then
  logmsg "FATAL: GWXLIBS installation failed"
  exit 1
fi
logmsg "End of phase 3 - GWXLIBS build completed"
fi # End of if DOPHASE3

if [ $DOPHASE4 -eq 1 ]; then
logmsg "Phase 4.0 ... doing the OpenLDAP configure"
cd $BUILDDIR/$OPENLDAPDIR
mkdir osr5
cd osr5
logcmd sh ../OSDBUILD
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenLDAP configuration failed"
  exit 1
fi

logmsg "Phase 4.1 ... doing the OpenLDAP build"
logcmd /usr/gnu/bin/gmake
logcmd /usr/gnu/bin/gmake
logcmd /usr/gnu/bin/gmake
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenLDAP build failed"
  exit 1
fi

logmsg "Phase 4.2 ... doing the OpenLDAP install"
logcmd /usr/gnu/bin/gmake install DESTDIR=$GWXDIST
if [ $? -ne 0 ]; then
  logmsg "FATAL: OpenLDAP installation failed"
  exit 1
fi
logmsg "End of phase 4 - OpenLDAP build completed"
fi # End of if DOPHASE4

if [ $DOPHASE3 -eq 1 ]; then
logmsg "Special GWXLIBS post-LDAP phase compile"
cd $BUILDDIR/$GWXLIBSDIR/osr5
logcmd MAKEJFLAG=-j$NUMCPUS /usr/gnu/bin/gmake postldap
if [ $? -ne 0 ]; then
  logmsg "FATAL: GWXLIBS post-LDAP build failed"
  exit 1
fi
logmsg "Special GWXLIBS post-LDAP phase install"
logcmd MAKEJFLAG=-j$NUMCPUS /usr/gnu/bin/gmake ldapinstall DESTDIR=$GWXDIST
if [ $? -ne 0 ]; then
  logmsg "FATAL: GWXLIBS post-LDAP install failed"
  exit 1
fi
logmsg "End of special GWXLIBS post-LDAP phase"
fi # End of if DOPHASE3

if [ $DOPHASE5 -eq 1 ]; then
logmsg "Phase 5.0 ... doing the ICU configure"
cd $BUILDDIR/$ICUDIR/source
mkdir osr5
cd osr5
logcmd sh ../OSDBUILD
if [ $? -ne 0 ]; then
  logmsg "FATAL: ICU configuration failed"
  exit 1
fi

logmsg "Phase 5.1 ... doing the ICU build"
logcmd /usr/gnu/bin/gmake
if [ $? -ne 0 ]; then
  logmsg "FATAL: ICU build failed"
  exit 1
fi

logmsg "Phase 5.2 ... doing the ICU install"
logcmd /usr/gnu/bin/gmake install DESTDIR=$GWXDIST
if [ $? -ne 0 ]; then
  logmsg "FATAL: ICU installation failed"
  exit 1
fi
logmsg "End of phase 5 - ICU build completed"
fi # End of if DOPHASE5

# Xerces and Xalan both need this
ICUROOT=$GWXDIST/usr
export ICUROOT
XERCESCROOT=$BUILDDIR/$XERCESDIR
export XERCESCROOT
XALANCROOT=$BUILDDIR/$XALANDIR/c
export XALANCROOT
XALAN_USE_ICU=yes
export XALAN_USE_ICU

if [ $DOPHASE6 -eq 1 ]; then
logmsg "Phase 6.0 ... doing the Xerces-C configure"

cd $BUILDDIR/$XERCESDIR/src/xercesc
logcmd sh OSDBUILD
if [ $? -ne 0 ]; then
  logmsg "FATAL: Xerces-C configuration failed"
  exit 1
fi

logmsg "Phase 6.1 ... doing the Xerces-C build"
logcmd /usr/gnu/bin/gmake
if [ $? -ne 0 ]; then
  logmsg "FATAL: Xerces-C build failed"
  exit 1
fi

logmsg "Phase 6.2 ... doing the Xerces-C install"
logcmd /usr/gnu/bin/gmake install DESTDIR=$GWXDIST
if [ $? -ne 0 ]; then
  logmsg "FATAL: Xerces-C installation failed"
  exit 1
fi
logmsg "End of phase 6 - Xerces-C build completed"
fi # End of if DOPHASE6

if [ $DOPHASE7 -eq 1 ]; then
logmsg "Phase 7.0 ... doing the Xalan-C configure"

cd $BUILDDIR/$XALANDIR/c/src/xalanc
logcmd sh OSDBUILD
if [ $? -ne 0 ]; then
  logmsg "FATAL: Xalan-C configuration failed"
  exit 1
fi

logmsg "Phase 7.1 ... doing the Xalan-C build"
logcmd /usr/gnu/bin/gmake
if [ $? -ne 0 ]; then
  logmsg "FATAL: Xalan-C build failed"
  exit 1
fi

logmsg "Phase 7.2 ... doing the Xalan-C install"
logcmd /usr/gnu/bin/gmake install DESTDIR=$GWXDIST
if [ $? -ne 0 ]; then
  logmsg "FATAL: Xalan-C installation failed"
  exit 1
fi
logmsg "End of phase 7 - Xalan-C build completed"
fi # End of if DOPHASE7

endat=`date`

cat << EOF
Full build and base install of GWXLIBS done.
Started at: $startat
Ended at  : $endat

A few things to remember to package propperly:
o  install-info from the texinfo utilities in GNUTOOLS must go into
   /usr/gnu/bin
o  the libltdl.* files from the libtool in GNUTOOLS must go into
   /usr/lib
o  check previous versions of the dist tree to make sure things are
   the same, or that the differences are expected.
o  remember to edit icu-config and remove the -I and -L flags

EOF

