#!/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 acc icc ecc gcc3 mpcc smp gm tcp vmi scyld bluegene'
  echo ''
  echo '<charmc-options>: e.g. -g -save -verbose'
  echo 'Note: This script:'
  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/'`;
    cd ../..
    if [ -d src/Common.${ARCH} ]
    then
      cd src/Common.$ARCH
      str="$str `ls conv-mach-*.h 2>/dev/null | sed 's/conv-mach-\([^.]*\).h/\1/'`";
    fi
    printf 'supported options: '
    echo $str)
  echo
  exit 1
}

CheckDir() {
  for dir in $*
  do
    if test ! -d $dir
    then
      echo Error: cannot find $1!
      exit 1
    fi
  done
}

TestIfSupported() {
  if [ ! -f src/$VERSION/conv-mach-$1.h ] && [ ! -f src/Common.$ARCH/conv-mach-$1.h ]
  then
    echo; 
    echo "Error> option: $1 is not supported in this version!"; 
    printOption
  fi
}


# start

[ $# -lt 2 ] && syntax


MAKEOPTS=""
OPTS=""
MORE=""
COMPILER=""
BASEDIR=
LIBDIR=
INCDIR=
ARCH=

while [ ! $# -eq 0 ]
do
  case "$1" in
    -*) 
          MAKEOPTS="$MAKEOPTS $1"; shift
	  ;;
    *)
          PROGRAM=$1 ; shift
          VERSION=$1; BASEVERSION=$1; ARCH=`echo $1 | sed -e 's@-.*@@'`; shift
          test "$1" = "help" && printOption
          while [ ! $# -eq 0 ]
          do
            case "$1" in
	    "pgcc"|"ecc"|"icc"|"cxx"|"kcc"|"cc"|"cc64"|"acc"|"gcc3"|"mpcc")
                TestIfSupported $1
                COMPILER=$1; shift
	        ;;
            "gm"|"vmi"|"scyld"|"smp"|"tcp"|"bluegene")
                TestIfSupported $1
                MORE="$MORE $1"; shift
		;;
    	    "--basedir")
          	shift; CheckDir $1/include $1/lib
          	LIBDIR="$LIBDIR -L$1/lib";
          	INCDIR="$INCDIR -I$1/include"; shift
 	  	;;
    	    --basedir=*)
                dir=`echo $1 | awk -F= '{print $2}'`
		CheckDir $dir/include $dir/lib
          	LIBDIR="$LIBDIR -L$dir/lib";
          	INCDIR="$INCDIR -I$dir/include"; shift
 	  	;;
    	    "--libdir")
          	shift; CheckDir $1
          	LIBDIR="$LIBDIR -L$1"; shift
 	  	;;
    	    --libdir=*)
                dir=`echo $1 | awk -F= '{print $2}'`
		CheckDir $dir
          	LIBDIR="$LIBDIR -L$dir"; shift
 	  	;;
    	    "--incdir")
          	shift; CheckDir $1
          	INCDIR="$INCDIR -I$1"; shift
 	  	;;
    	    --incdir=*)
                dir=`echo $1 | awk -F= '{print $2}'`
		CheckDir $dir
          	INCDIR="$INCDIR -I$dir"; shift
 	  	;;
            *)
                OPTS="$OPTS $1"; shift
		;;
 	    esac
	  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
  MORE=`echo $MORE | awk  '{ for (i = 1; i <= NF; ++i) print $i }' | sort`
  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
	for newdir in `echo bin lib include tmp`
	do
		echo "Soft-linking over $newdir"
		if [ -r $newdir ] 
		then
			rm -f $newdir
		fi
		ln -s $VERSION/$newdir $newdir
	done
fi

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

ConvUsr="$VERSION/tmp/conv-mach-pre.sh"
echo "Generating $ConvUsr"
echo > $ConvUsr
test -n "$LIBDIR" && echo "CMK_LIBDIR=\"$LIBDIR\"" >> $ConvUsr
test -n "$INCDIR" && echo "CMK_INCDIR=\"$INCDIR\"" >> $ConvUsr
chmod +x $ConvUsr

#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
      if [ -f src/$BASEVERSION/conv-mach-$i.h ]
      then
        CONVFILEDIR="src/$BASEVERSION/"
      else
        CONVFILEDIR="src/Common.$ARCH"
      fi
      echo '/* '"$CONVFILEDIR/conv-mach-$i.h"' */' >> $ConvHeader
      cat $CONVFILEDIR/conv-mach-$i.h >> $ConvHeader
      echo '# '"$CONVFILEDIR/conv-mach-$i.sh" >> $ConvSh
      cat $CONVFILEDIR/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"

MAKEEXIT=$?
if [ $MAKEEXIT -eq 0 ]
then
	echo "-------------------------------------------------"
	echo "$PROGRAM built successfully."
	echo "Next, try out a sample program like" \
	     "pgms/charm++/simplearrayhello"
else
	echo "-------------------------------------------------"
	echo "Charm++ NOT BUILT. Either cd into $VERSION/tmp and try"
	echo "to resolve the problems yourself, visit"
	echo "    http://charm.cs.uiuc.edu/"
	echo "for more information. Otherwise, email the developers at ppl@cs.uiuc.edu"
	exit $MAKEEXIT
fi
