#! /bin/ksh
usage() {
  cat <<EOF
Usage: GWXBUILD osname srcdir builddir xenvdir pidir

osname    one of 'osr5', 'uw7' or 'generic'
srcdir    full path to the top level source directory
builddir  full path to directory where builds will be done
xenvdir   full path to xenv installation root
pidir     full path to pi preparation area

Note: srcdir and builddir must be different, as must xenvdir and pidir.
The results of a successful build will be BUILDDIR/gwxlibs-OSNNAME.cpio
and xorg-OSNAME.cpio, which should be copied into the dist/OSNAME
directory where the commonpi script will be run from.

Note: each time this script is run it will erase ALL of BUILDDIR and PIDIR.
EOF
  exit 1
}

fatal() {
  echo "FATAL ERROR: $@"
  exit 1
}

[ $# -eq 5 ] || usage
osname=$1
case $osname in
  osr5)    ;;
  uw7)     ;;
  generic) ;;
  *)       usage ;;
esac

srcdir=$2
[ -f $srcdir/GWXBUILD ] || usage
[ -d $srcdir/build ] || usage

. $srcdir/build/${osname}.sh

blddir=$3
xenvdir=$4
pidir=$5

[ -d $xenvdir ] && rm -fr $xenvdir
[ -d $blddir ] && rm -fr $blddir
[ -d $pidir ] && rm -fr $pidir
mkdir -p $blddir $pidir $xenvdir $xenvdir/bin $xenvdir/lib
mkdir $pidir/gwxlibs $pidir/xorg

cd $blddir
mkdir gzip ast gnugrep gnumake gnused gnum4 gnubison texinfo flex bin lib LOGS
echo "Compiling procgwx ..."
$GWXHOSTCC -O -o bin/procgwx $srcdir/build/procgwx.c || fatal "procgwx build failed"
bin/procgwx -s $srcdir -m > $pidir/macros.gwxlibs || fatal "procgwx macro generation failed"

echo "Generating BUILD script ..."
bin/procgwx -s $srcdir -g $osname -b $blddir -x $xenvdir -p $pidir > BUILD || fatal "failed to run procgwx"
chmod 755 BUILD
PATH=$xenvdir/bin:$xenvdir/usr/bin:$xenvdir/usr/X11R6/bin:$xenvdir/usr/gnu/bin:$PATH; export PATH

[ -f $srcdir/build/prebuilt-${osname}.tar ] && {
  (cd $xenvdir; tar xf $srcdir/build/prebuilt-${osname}.tar)
}

[ -x $xenvdir/bin/ksh -a -x $xenvdir/bin/pax ] || {
  echo "Creating AST tools (ksh93, pax) ..."
  cd $srcdir/build/ast
  find . | cpio -pdum $blddir/ast 2> /dev/null
  cd $blddir/ast
  bin/package make > astmake.log 2>&1 || fatal "failed to compile AST tools"
  astarch=`bin/package host`
  [ -x arch/$astarch/bin/pax -a -x arch/$astarch/bin/ksh ] || fatal "error compiling AST tools"
  cp arch/$astarch/bin/ksh $xenvdir/bin/ksh
  cp arch/$astarch/bin/pax $xenvdir/bin/pax
  ln -f $xenvdir/bin/ksh $xenvdir/bin/sh
  ln -f $xenvdir/bin/ksh $xenvdir/bin/gwxksh
  ln -f $xenvdir/bin/ksh $xenvdir/bin/ksh93
  ln -f $xenvdir/bin/ksh $xenvdir/bin/gwxksh93
  ln -f $xenvdir/bin/pax $xenvdir/bin/gwxpax
}

SHELL=$xenvdir/bin/ksh
CONFIG_SHELL=$SHELL
export SHELL CONFIG_SHELL
cat > $xenvdir/bin/which <<EOF
#! $SHELL
whence -p "\$1"
EOF
chmod 755 $xenvdir/bin/which

[ -x $xenvdir/bin/gmake ] || {
  echo "Creating GNU make ..."
  cd $blddir/gnumake
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/gnumake/configure --disable-nls --build=$GWXARCH > gnumake.log 2>&1 || fatal "failed to configure GNU make"
  make >> gnumake.log 2>&1 || fatal "failed to compile GNU make"
  cp make $xenvdir/bin/make
  ln -f $xenvdir/bin/make $xenvdir/bin/gmake
}

[ -x $xenvdir/bin/sed ] || {
  echo "Creating GNU sed ..."
  cd $blddir/gnused
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/gnused/configure --disable-i18n --with-included-regex --disable-nls --build=$GWXARCH > gnused.log 2>&1 || fatal "failed to configure GNU sed"
  gmake >> gnused.log 2>&1 || fatal "failed to compile GNU sed"
  cp sed/sed $xenvdir/bin/sed
  ln -f $xenvdir/bin/sed $xenvdir/bin/gsed
}

[ -x $xenvdir/bin/m4 ] || {
  echo "Creating GNU m4 ..."
  cd $blddir/gnum4
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/gnum4/configure --enable-changeword --disable-dependency-tracking --with-included-regex --build=$GWXARCH > gnum4.log 2>&1 || fatal "failed to configure GNU M4"
  gmake >> gnum4.log 2>&1 || fatal "failed to compile GNU M4"
  cp src/m4 $xenvdir/bin/m4
  ln -f $xenvdir/bin/m4 $xenvdir/bin/gm4
}

[ -x $xenvdir/bin/bison ] || {
  echo "Creating GNU bison ..."
  cd $blddir/gnubison
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/gnubison/configure --disable-nls --build=$GWXARCH > gnubison.log 2>&1 || fatal "failed to configure GNU bison"
  gmake >> gnubison.log 2>&1 || fatal "failed to compile GNU bison"
  cp src/bison $xenvdir/bin/bison
  cp src/yacc $xenvdir/bin/yacc
  mkdir $xenvdir/lib/m4sugar
  cp $srcdir/build/gnubison/data/*.c $srcdir/build/gnubison/data/*.cc $xenvdir/lib
  cp $srcdir/build/gnubison/data/*.m4 $xenvdir/lib
  cp $srcdir/build/gnubison/data/m4sugar/m4sugar.m4 $xenvdir/lib/m4sugar/
}
BISON_PKGDATADIR=$xenvdir/lib; export BISON_PKGDATADIR

[ -x $xenvdir/bin/flex ] || {
  echo "Creating flex ..."
  cd $blddir/flex
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/flex/configure --build=$GWXARCH > gnuflex.log 2>&1 || fatal "failed to configure flex"
  gmake >> gnuflex.log 2>&1 || fatal "failed to compile flex"
  cp flex $xenvdir/bin/flex
  cp libfl.a $xenvdir/lib
}

[ -x $xenvdir/bin/ggrep ] || {
  echo "Creating GNU grep ..."
  cd $blddir/gnugrep
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/gnugrep/configure --disable-dependency-tracking --disable-nls --with-included-getopt --with-included-regex --disable-perl-regexp --build=$GWXARCH > gnugrep.log 2>&1 || fatal "failed to configure GNU grep"
  gmake >> gnugrep.log 2>&1 || fatal "failed to compile GNU grep"
  cp src/grep $xenvdir/bin/grep
  ln -f $xenvdir/bin/grep $xenvdir/bin/ggrep
}

[ -x $xenvdir/bin/gzip ] || {
  echo "Creating GNU gzip ..."
  cd $blddir/gzip
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/gzip/configure > gzip.log 2>&1 || fatal "failed to configure GNU gzip"
  make >> gzip.log 2>&1 || fatal "failed to compile GNU gzip"
  cp gzip $xenvdir/bin/gzip
  ln -f $xenvdir/bin/gzip $xenvdir/bin/gunzip
  ln -f $xenvdir/bin/gzip $xenvdir/bin/gzcat
}

[ -x $xenvdir/bin/makeinfo ] || {
  echo "Creating GNU texinfo ..."
  cd $blddir/texinfo
  CC="$GWXHOSTCC" CFLAGS="-O" ${SHELL} $srcdir/build/texinfo/configure --disable-dependency-tracking --disable-nls --build=$GWXARCH --infodir=$GWXINFODIR > texinfo.log 2>&1 || fatal "failed to configure GNU texinfo"
  gmake >> texinfo.log 2>&1 || fatal "failed to compile GNU texinfo"
  cp makeinfo/makeinfo $xenvdir/bin/makeinfo
  cp info/ginfo $xenvdir/bin/info
  cp util/ginstall-info $xenvdir/bin/install-info
  cp util/texindex $xenvdir/bin/texindex
  ln -f $xenvdir/bin/info $xenvdir/bin/ginfo
  ln -f $xenvdir/bin/install-info $xenvdir/bin/ginstall-info
  ln -f $xenvdir/bin/makeinfo $xenvdir/bin/gmakeinfo
  ln -f $xenvdir/bin/texindex $xenvdir/bin/gtexindex
}

#
# Create the gwxsh script that can be used to ensure the correct environment
# is set up for running individual commands inside the build.
#
cat > $xenvdir/bin/gwxsh <<EOF
#! $xenvdir/bin/ksh
RUN_AS_GWXSH=1
EOF
cat ${blddir}/BUILD >> ${xenvdir}/bin/gwxsh
chmod 755 ${xenvdir}/bin/gwxsh

[ -z "$GWXNOBUILD" ] && {
  echo "Starting the build at `date`"
  cd $blddir
  ./BUILD
  ret=$?
  echo "Build finished at `date`"
  [ $ret = 0 ] || fatal "build failed!"
}

exit 0
