#!/bin/csh -f
goto begin
syntax:
  echo ''
  echo 'Usage: config [debug] [tcl] [fftw] [plugins] [<build_dir>/]<arch>[.comment]'
  echo 'Environment variable NAMD_BUILD_BASE may point to scratch directory.'
  echo ''
  echo '<arch>: ' 
  ( cd arch ; ls -1 *.arch |sed -e 's/\.arch//' | egrep -v 'template' | pr -3 -t)
  echo ''
  exit 1

exists:
  echo ''
  echo 'directory already exists'
  echo ''
  exit 1

begin:

  if ($#argv < 1) goto syntax
  if ( $1 == debug ) then
    set use_debug = 1
    shift
  else
    set use_debug = 0
  endif

  if ($#argv < 1) goto syntax
  if ( $1 == tcl ) then
    set use_tcl = 1
    shift
  else
    set use_tcl = 0
  endif

  if ($#argv < 1) goto syntax
  if ( $1 == fftw ) then
    set use_fftw = 1
    shift
  else
    set use_fftw = 0
  endif

  if ($#argv < 1) goto syntax
  if ( $1 == plugins ) then
    set use_plugins = 1
    shift
  else
    set use_plugins = 0
  endif

  if ($#argv < 1) goto syntax
  set BUILD_DIR=$1 ; shift
  if ( x$BUILD_DIR == x ) goto syntax
  set DIR=`pwd`;

  set ARCH=$BUILD_DIR:t
  if ( ! -f arch/$ARCH.arch ) set ARCH=$ARCH:r
  if ( ! -f arch/$ARCH.arch ) goto syntax

  set BUILD_LINK = $BUILD_DIR
  if ( $BUILD_DIR:t == $BUILD_DIR && $?NAMD_BUILD_BASE ) then
    if ( -d $BUILD_DIR:t ) goto exists
    set UNIQ = `date '+%Y-%b-%d'`-$$
    set BUILD_DIR = $NAMD_BUILD_BASE/${UNIQ}_$BUILD_DIR
    echo "Creating link: $BUILD_DIR to $BUILD_LINK"
    ln -s $BUILD_DIR $BUILD_LINK
  endif

  if ( -d $BUILD_DIR ) goto exists
  echo "Creating directory: $BUILD_DIR"
  mkdir $BUILD_DIR
  cd $BUILD_DIR

  set ROOTDIR=$DIR
  if ( $BUILD_DIR:t == $BUILD_DIR ) set ROOTDIR='..'
  if ( ./$BUILD_DIR:t == $BUILD_DIR ) set ROOTDIR='..'

  echo "Creating link: $ROOTDIR to .rootdir"
  ln -s $ROOTDIR .rootdir
  echo "Creating Makearch"
  if ( $ARCH =~ *Win32* ) then
   cat .rootdir/Make.charm >! Makearch
   cat .rootdir/arch/$ARCH.arch >> Makearch
  else
   echo include .rootdir/Make.charm >! Makearch
   echo include .rootdir/arch/$ARCH.arch >> Makearch
  endif
  echo 'CHARM = $(CHARMBASE)/$(CHARMARCH)' >> Makearch
  if ( $ARCH =~ *-MPI* ) then
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)-MPI' >> Makearch
  else if ( $ARCH =~ *-VMI* ) then
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)-VMI' >> Makearch
  else if ( $ARCH =~ *-GM* ) then
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)-GM' >> Makearch
  else if ( $ARCH =~ *-ScyldCD* ) then
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)-ScyldCD' >> Makearch
  else if ( $ARCH =~ *-Scyld* ) then
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)-Scyld' >> Makearch
  else if ( $ARCH =~ *-Clustermatic* ) then
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)-Clustermatic' >> Makearch
  else
    echo 'NAMD_PLATFORM = $(NAMD_ARCH)' >> Makearch
  endif
  if ( $ARCH =~ *Win32* ) then
   cat .rootdir/arch/Win32.base >> Makearch
   if ( $use_tcl ) cat .rootdir/arch/Win32.tcl >> Makearch
   if ( $use_fftw ) cat .rootdir/arch/Win32.fftw >> Makearch
   if ( $use_plugins ) cat .rootdir/arch/Win32.plugins >> Makearch
  else
   echo 'include .rootdir/arch/$(NAMD_ARCH).base' >> Makearch
   if ( $use_tcl ) echo 'include .rootdir/arch/$(NAMD_ARCH).tcl' >> Makearch
   if ( $use_fftw ) echo 'include .rootdir/arch/$(NAMD_ARCH).fftw' >> Makearch
   if ( $use_plugins ) echo 'include .rootdir/arch/$(NAMD_ARCH).plugins' >> Makearch
   if ( $use_debug ) then
    echo 'CXXOPTS = -g' >> Makearch
    echo 'CXXTHREADOPTS = -g' >> Makearch
    echo 'CXXSIMPARAMOPTS = -g' >> Makearch
    echo 'CXXNOALIASOPTS = -g' >> Makearch
    echo 'COPTS = -g' >> Makearch
   endif
  endif
  echo "Linking Makefiles"
  if ( $ARCH =~ *Win32* ) then
   cp .rootdir/Makefile ./Makefile
   cp .rootdir/Make.depends ./Make.depends
  else
   ln -s .rootdir/Makefile ./Makefile
   ln -s .rootdir/Make.depends ./Make.depends
  endif
  if ( $ARCH =~ *Win32* ) then
   echo "Converting text files"
   foreach f ( .rootdir/*.txt )
     perl -p -i -e 's/(?<!\r)\n$/\r\n/' < $f > $f:t
   end
  endif
  echo "Linking src and psfgen"
  if ( $ARCH =~ *MSVC* ) then
    cp -r .rootdir/src ./src
    cp -r .rootdir/psfgen ./sb
  else
    ln -s .rootdir/src ./src
    ln -s .rootdir/psfgen ./sb
  endif
  echo "Creating dpme2 and linking files"
  (cd $DIR/dpme2;find . -name CVS -prune -o -type d -print )|sed -e s@.@@> temp.d
  (cd $DIR/dpme2;find . -name CVS -prune -o -type f -print )|sed -e s@.@@> temp.f
  awk '{print "mkdir dpme2"$1;}' < temp.d > temp.m
  awk '{print "ln -s ../.rootdir dpme2"$1;}' < temp.d >> temp.m
  awk '{print "ln -s .rootdir/dpme2"$1" dpme2"$1;}' < temp.f > temp.l
  sh temp.m
  sh temp.l
  rm -f temp.d temp.f temp.m temp.l
  echo "Creating dpmta-2.6 and linking files"
  (cd $DIR/dpmta-2.6;find . -name CVS -prune -o -type d -print )|sed -e s@.@@> temp.d
  (cd $DIR/dpmta-2.6;find . -name CVS -prune -o -type f -print )|sed -e s@.@@> temp.f
  awk '{print "mkdir dpmta-2.6"$1;}' < temp.d > temp.m
  awk '{print "ln -s ../.rootdir dpmta-2.6"$1;}' < temp.d >> temp.m
  awk '{print "ln -s .rootdir/dpmta-2.6"$1" dpmta-2.6"$1;}' < temp.f > temp.l
  sh temp.m
  sh temp.l
  rm -f temp.d temp.f temp.m temp.l
  echo "You are ready to do a make in directory $BUILD_LINK now."
