#!/bin/sh

set -o errexit
set -o nounset



VERSION=$(./tools/buildNum.sh)
AUTHORS=$(cat DESCRIPTION.in | perl -0pe 's,\n\s+, ,sg' |grep '^Author' | cut -d ':' -f 2)
DATE=$(git log -n 1 --date=short --format=%cd)
YEAR=$(echo $DATE | cut -d '-' -f 1)

cat DESCRIPTION.in | sed -e "s|@VERSION@|${VERSION}|" -e "s|@DATE@|${DATE}|" > DESCRIPTION
#cat inst/CITATION.in | sed -e "s/@VERSION@/$VERSION/" -e "s/@YEAR@/$YEAR/" -e "s/@AUTHORS@/$AUTHORS/" > inst/CITATION

dVersion=R/dynrVersion.R
sed -e s/GIT_VERSION/$(git describe --dirty)/ $dVersion.in > $dVersion

# uncomment once roxygen2 is hooked up
if ! grep -q 'Generated by roxygen2' man/*.Rd; then
  ./tools/roxygenize
  grep -v -E '(man/.*\.Rd|# The following list of roxygen)' .gitignore > .gitignore.new
  echo '# The following list of roxygen built man pages is automatically updated' >> .gitignore.new
  grep -l -F 'Generated by roxygen2' man/*.Rd | sort --ignore-case >> .gitignore.new
  mv .gitignore.new .gitignore
fi

chmod 755 configure
chmod 755 cleanup

