#!/bin/bash
source=`pwd`/$1
date=`date +%Y-%m-%d`
revision=`svn info ${source} | grep Revision:`
revision=`echo ${revision} | sed 's/Revision: //'`
#
# Pick the version of the code from the setting of RELEASE in config/makefile.h
# The assumption is that only one of the RELEASE lines is uncommented. If that line
# has a version number on it we will pick that number, otherwise we will use the
# string "dev".
#
version=`grep RELEASE "$source/src/config/makefile.h" | grep -v \# | grep -v \(RELEASE\)`
version=`echo $version dev | awk '{ print $3 }'`
release=Nwchem-${version}
#
if [[ ! ( $# == 1 || $# == 2 ) ]] ; then
  echo "Usage: $0 source-directory [distribution-directory]"
  echo ""
  echo "The source-directory is the directory in which the source code"
  echo "lives. The distribution-directory is the directory that the"
  echo "source will be copied into and which is used to generate the"
  echo "distribution tar-ball. If the distribution-directory is not"
  echo "specified ${release}.revision${revision}-src.${date}.tar.gz"
  echo "will be used instead."
  echo "Any artifacts from testing the code are removed and the resulting"
  echo "directory packaged as a compressed tar-file."
  exit 1
fi
if [ $# == 2 ] ; then
  distro=$2
else
  distro=${release}.revision${revision}-src.${date}
fi
target=`pwd`/${distro}
echo "Generating source distro"
echo "Source: " ${source}
echo "Target: " ${target}
if [ -d ${target} ] ; then
  echo "Old version of" $target "already exists!?"
  echo "Please move or remove it."
  exit 2
fi
cp -a ${source} ${target}
export NWCHEM_TOP=${target}
if [ ${#NWCHEM_TARGET} -eq 0 ] ; then
  NWCHEM_OS=`uname | tr '[a-z]' '[A-Z]'`
  if [ $? -ne 0 ] ; then
    unset NWCHEM_OS
  fi
  if [ ${#NWCHEM_OS} -ne 0 ] ; then
    if [ ${NWCHEM_OS} == "DARWIN" ] ; then
      export NWCHEM_TARGET=MACX64
    elif [ `expr match ${NWCHEM_OS} CYGWIN` != "0" ] ; then
      export NWCHEM_TARGET=CYGWIN
    elif [ `expr match ${NWCHEM_OS} CYGNUS` != "0" ] ; then
      export NWCHEM_TARGET=CYGNUS
    elif [ `expr match ${NWCHEM_OS} INTERIX` != "0" ] ; then
      export NWCHEM_TARGET=INTERIX
    else
      export NWCHEM_TARGET=LINUX64
    fi
  fi
fi
#
# Make sure we include the Global Arrays
#
export GA_VERSION=`${target}/src/tools/get-tools --ga-version`
echo -n "Check for Global Arrays... "
if [ -d ${target}/src/tools/${GA_VERSION} ] ; then
  echo "OK"
else
  echo "No Global Arrays."
  echo "Running get-tools to get: ${GA_VERSION}"
  pushd ${target}/src/tools
  ./get-tools
  popd
  if [ -d ${target}/src/tools/${GA_VERSION} ] ; then
    echo "Retrieved Global Arrays"
  else
    echo "Failed to get Global Arrays. Aborting..."
    exit 3
  fi
fi
#
# Add version info (might actually depend on the build so defer...)
#
#pushd ${target}/src/util
#make version
#popd
#
# Clean the distribution up before packaging it.
#
echo -n "Cleaning up any build/testing garbage... "
# Next line is to deal with the diana module...
svn revert ${target}/src/config/make_nwchem_config
pushd ${target}/src
export USE_RISKYFC=yes
export NWCHEM_MODULES=all
if [ -f config/nwchem_config.h ] ; then
  make -j 1 nwchem_config                                     2>&1 > /dev/null
  make -j 1 realclean                                         2>&1 > /dev/null
  if [ $? -ne 0 ] ; then
    echo "Make failed. Aborting..."
    exit 1
  fi
fi
unset NWCHEM_MODULES
popd
if [ -d ${target}/lib ] ; then
  rm -rf ${target}/lib
fi
if [ -d ${target}/src/tools/lib ] ; then
  rm -rf ${target}/src/tools/lib
fi
if [ -d ${target}/src/tools/include ] ; then
  rm -rf ${target}/src/tools/include
fi
if [ -d ${target}/src/stubs.F ] ; then
  rm -rf ${target}/src/stubs.F
fi
if [ -d ${target}/contrib/mov2asc ] ; then
  rm -rf ${target}/contrib/mov2asc/*.o \
         ${target}/contrib/mov2asc/asc2mov \
         ${target}/contrib/mov2asc/mov2asc
fi
find ${target} -name .svn -exec rm -rf {} \;                2>&1 > /dev/null
find ${target} -name testoutputs -exec rm -rf {} \;         2>&1 > /dev/null
find ${target} -name bin -exec rm -rf {} \;                 2>&1 > /dev/null
find ${target} -name include_stamp -exec rm -rf {} \;       2>&1 > /dev/null
find ${target} -name dependencies -exec rm -rf {} \;        2>&1 > /dev/null
find ${target} -name "doalltests.*.log" -exec rm -rf {} \;  2>&1 > /dev/null
find ${target} -name "build_nwchem*.log" -exec rm -rf {} \; 2>&1 > /dev/null
echo "Done."
#
# Now package it all up
#
echo -n "Tarring everything up... "
tar -vzcf ${target}.tar.gz ${distro} 2>&1 > ${target}.MANIFEST
echo "Done."
