#!/bin/sh

syntax() {
  echo ''
  echo 'Usage: build <target> <version> <options> [charmc-options ...]'
#  echo ''
  echo '<targets>: converse, charm++ libs AMPI IDL tsm sm pvm sdag bluegene'
  echo '<targets>: doc ps-doc pdf-doc html-doc'
  echo '<versions>: ' 
  ( cd src ; ls -1 | egrep -v '(^Common)|(^CVS)|(^QuickThreads)|(^ccs-client)' | pr -3 -t )
#  echo ''
  echo '<options>: help cc cc64 cxx kcc pgcc icc gm vmi scyld '
  echo '<charmc-options>: e.g. -g -save -verbose'
  echo 'Note: This script is trivial.  It'
  echo ' 1. Creates directories <version> and <version>/tmp';
#  echo ' 2. Copies src/Common/scripts/Makefile into <version>/tmp';
  echo ' 2. Does a "make <target> <version> OPTS=<charmc-options>" in <version>/tmp.'
#  echo "That's all build does.  The rest is handled by the Makefile."
  exit 1
}

printOption() {
  echo
  ( cd src/$BASEVERSION; 
    str=`ls conv-mach-*.h 2>/dev/null | sed 's/conv-mach-\([^.]*\).h/\1/'`; 
    printf 'supported options: '
    echo $str )
  echo
  exit 1
}

  [ $# -lt 2 ] && syntax

MAKEOPTS=""
OPTS=""
MORE=""
COMPILER=""

while [ ! $# -eq 0 ]
do
  case "$1" in
    -*) 
          MAKEOPTS="$MAKEOPTS $1"; shift
	  ;;
    *)
          PROGRAM=$1 ; shift
          VERSION=$1; BASEVERSION=$1; shift
          test "$1" = "help" && printOption
          while [ ! $# -eq 0 ]
          do
            if test "$1" = "pgcc" || test "$1" = "icc" || test "$1" = "cxx" || test "$1" = "kcc" || test "$1" = "cc" || test "$1" = "cc64"
            then
              if [ ! -f src/$VERSION/conv-mach-$1.h ]
              then
                echo; echo "Error> option: $1 is not supported in this version!"; 
                printOption
              fi
              COMPILER=$1; shift;
            elif test "$1" = "gm" || test "$1" = "vmi" || test "$1" = "scyld"
            then
              if [ ! -f src/$VERSION/conv-mach-$1.h ]
              then
                echo; echo "Error> option: $1 is not supported in this version!";
                printOption
              fi
              MORE="$MORE $1"; shift;
            else
              OPTS="$OPTS $1"; shift;
            fi
	  done
	  ;;
  esac
done

[ "x$VERSION" = "x" ] && syntax
	
( echo $BASEVERSION | egrep -s '^Common' ) && syntax
[ -d src/$BASEVERSION ] || syntax

#generate VERSION name combining all options.
if test -n "$MORE"
then
  for i in $MORE
  do
    VERSION=$VERSION-$i
  done
fi

if test -n "$COMPILER"
then
  VERSION=$VERSION-$COMPILER
  MORE="$COMPILER $MORE"
fi

[ -d $VERSION ] || echo "Creating dir: $VERSION" 
[ -d $VERSION ] || mkdir $VERSION
[ -d $VERSION/tmp ] || echo "Creating dir: $VERSION/tmp" 
[ -d $VERSION/tmp ] || mkdir $VERSION/tmp
echo "Copying src/Common/scripts/Makefile to $VERSION/tmp"
rm -f $VERSION/tmp/Makefile

if [ "x_`echo $VERSION | awk -F- '{print $2}'`" = "x_win32" ]
then
#Win32 version needs special compilers and *copied* (not linked)
# source files.
	echo "Copying compilers for win32 into cygwin /bin directory"
	cp src/Common.win32/unix2nt* /bin
	cp src/Common.win32/system_ln $VERSION/tmp
	cp src/Common.win32/unistd.h $VERSION/tmp
else
	cat > $VERSION/tmp/system_ln <<EOF
#!/bin/sh
ln -f -s \$@
EOF
fi

chmod +x $VERSION/tmp/system_ln
ln -s "../../src/Common/scripts/Makefile" $VERSION/tmp/Makefile

#special options
ConvHeader="$VERSION/tmp/conv-mach-opt.h"
ConvSh="$VERSION/tmp/conv-mach-opt.sh"
if test ! -f $ConvHeader
then
  echo "Generating $ConvHeader, conv-mach-opt.sh"
  /bin/rm -f $ConvHeader  $ConvSh
  touch $ConvHeader; touch $ConvSh
  if test -n "$MORE"
  then
    for i in $MORE
    do
#      touch $VERSION/tmp/$i
      echo '/* '"src/$BASEVERSION/conv-mach-$i.h"' */' >> $ConvHeader
      cat src/$BASEVERSION/conv-mach-$i.h >> $ConvHeader
      echo '# '"src/$BASEVERSION/conv-mach-$i.sh" >> $ConvSh
      cat src/$BASEVERSION/conv-mach-$i.sh >> $ConvSh
    done
  fi
fi

echo $BASEVERSION > $VERSION/tmp/.vdir

echo "Performing 'make $MAKEOPTS $PROGRAM OPTS="$OPTS"' in $VERSION/tmp"
cd $VERSION/tmp ; make $MAKEOPTS $PROGRAM OPTS="$OPTS"
