#!/bin/csh -f
goto begin
syntax:
  echo ''
  echo 'Usage: config [debug] [tcl] [fftw] [<build_dir>/]<arch>[.comment] [suffix]'
  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 -2 -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
    echo 'The "plugins" flag is not needed; plugins are built automatically.'
    shift
  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 =~ *Win* ) 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
  set ARCH_SUFFIX = ""
  if ( $ARCH =~ *-Scyld* ) set ARCH_SUFFIX = -Scyld
  if ( $ARCH =~ *-Clustermatic* ) set ARCH_SUFFIX = -Clustermatic
  if ( $ARCH =~ *-MPI* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-MPI
  if ( $ARCH =~ *-CMPI* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-CMPI
  if ( $ARCH =~ *-MPT* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-MPT
  if ( $ARCH =~ *-VMI* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-VMI
  if ( $ARCH =~ *-TCP* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-TCP
  if ( $ARCH =~ *-GM* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-GM
  if ( $ARCH =~ *-Elan* ) set ARCH_SUFFIX = ${ARCH_SUFFIX}-Elan
  if ($#argv > 0) then
    set ARCH_SUFFIX = -$1
    shift
  endif
  echo NAMD_PLATFORM = \$\(NAMD_ARCH\)${ARCH_SUFFIX} >> Makearch
  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
  else if ( $ARCH =~ *Win64* ) then
   cat .rootdir/arch/Win64.base >> Makearch
   if ( $use_tcl ) cat .rootdir/arch/Win64.tcl >> Makearch
   if ( $use_fftw ) cat .rootdir/arch/Win64.fftw >> Makearch
  else
   echo 'include .rootdir/arch/$(NAMD_ARCH)$(NAMD_SUBARCH).base' >> Makearch
   if ( $use_tcl ) echo 'include .rootdir/arch/$(NAMD_ARCH)$(NAMD_SUBARCH).tcl' >> Makearch
   if ( $use_fftw ) echo 'include .rootdir/arch/$(NAMD_ARCH)$(NAMD_SUBARCH).fftw' >> 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 =~ *Win* ) 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 =~ *Win* ) then
   echo "Converting text files"
   mkdir lib; mkdir lib/abf
   foreach d ( . lib lib/abf )
     foreach f ( .rootdir/$d/*.txt .rootdir/$d/*.tcl )
       perl -p -i -e 's/(?<!\r)\n$/\r\n/' < $f > $d/$f:t
     end
   end
  endif
  echo "Linking src, plugins, and psfgen"
  if ( $ARCH =~ *MSVC* ) then
    cp -r .rootdir/src ./src
    cp -r .rootdir/plugins ./plugins
    cp -r .rootdir/psfgen ./sb
  else
    ln -s .rootdir/src ./src
    ln -s .rootdir/plugins ./plugins
    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."
