#!/bin/sh

XSRC=`pwd`
DQSUTIL=$XSRC/../UTIL
XSRCUTIL=$XSRC/Util
CONFIGSDIR=$XSRC/Configs
SUBDIRS="Xaw3d qmon qusage qidle xinfo qusage/At qusage/klib qusage/list-2.1"
PROGNAME=`basename $0`

if [ $# -gt 3 ]; then
  echo Usage: $PROGNAME [ArchitectureName MakeFlags CCompiler]
  echo where: ArchitectureName is the name of the current architecture
  echo "       MakeFlags is a set of arguments to pass to the make utility"
  echo "       CCompiler is the name of the desired C compiler"
  exit 1
fi

if [ $# -gt 0 ]; then
  ARCH=$1
  if [ $# -gt 1 ]; then
    MFLAGS=$2
  fi
  if [ $# -gt 2 ]; then
    CCOM=$3
  fi
else
  echo $PROGNAME: Attempting to guess current architecture...

  ARCH=`${DQSUTIL}/config.guess`

  if [ "$ARCH" = "" ]; then
    echo failed!
    echo $PROGNAME: To manually build, try: $PROGNAME ArchitectureName
    echo $PROGNAME: where: ArchitectureName is the name of the current architecture
    exit 2
  fi

  echo $PROGNAME: done.
fi

echo $PROGNAME: Making in directory named: $ARCH

if [ -d $ARCH ]; then
  echo $PROGNAME: Directory named $ARCH already exists.
else
  if [ ! -r ../ARCS/$ARCH/dqs.a ]; then
    echo $PROGNAME: Cannot locate library named ../ARCS/$ARCH/dqs.a
    echo $PROGNAME: Build the DQS distribution before the DQS X distribution.
    exit 3
  fi
fi

( cd $ARCH; cat $CONFIGSDIR/config $XSRC/Imakefile > Imakefile; xmkmf )

for subdir in $SUBDIRS
do
  if [ $subdir = "qusage/At" ]; then
    # special case
    ( cd $ARCH/$subdir; \
      cat $CONFIGSDIR/config $XSRC/qusage/plotter-6.0pl7/At/Imakefile \
           > Imakefile; \
      xmkmf )
  else
    # generic case
    ( cd $ARCH/$subdir; \
       cat $CONFIGSDIR/config $XSRC/$subdir/Imakefile > Imakefile; \
      xmkmf )
  fi
done

exit 0
