#!/bin/bash
set -x

export LANG=en_US
export LC=C
export LC_ALL=C

PETSC_DIR=/home/balay/git-repo/petsc-maint.clone

#create the tarfile
maintdir=`dirname $0`
${maintdir}/builddist ${PETSC_DIR} maint

#determine tarball version
version_major=`grep '^#define PETSC_VERSION_MAJOR ' ${PETSC_DIR}/include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`
version_minor=`grep '^#define PETSC_VERSION_MINOR ' ${PETSC_DIR}/include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`
version_subminor=`grep '^#define PETSC_VERSION_SUBMINOR ' ${PETSC_DIR}/include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`

RELEASE=${version_major}.${version_minor}
VERSION=${version_major}.${version_minor}.${version_subminor}

#Now copy the generated tarfiles over to the website
scp ~/petsc-${VERSION}.tar.gz petsc@login.mcs.anl.gov:/mcs/ftp/pub/petsc/release-snapshots/
scp ~/petsc-lite-${VERSION}.tar.gz petsc@login.mcs.anl.gov:/mcs/ftp/pub/petsc/release-snapshots/

# petsc.tar.gz/petsc-lite.tar.gz links are done manually
ssh petsc@login.mcs.anl.gov " \
  cd /mcs/ftp/pub/petsc/release-snapshots; \
  /bin/rm -f petsc-${RELEASE}.tar.gz petsc-lite-${RELEASE}.tar.gz ; \
  /bin/ln -s petsc-${VERSION}.tar.gz petsc-${RELEASE}.tar.gz; \
  /bin/ln -s petsc-lite-${VERSION}.tar.gz petsc-lite-${RELEASE}.tar.gz"

# Update documentation/index.html documentation/installation.html download/index.html as well
ssh petsc@login.mcs.anl.gov chmod u+w /mcs/web/research/projects/petsc/documentation/index.html \
  /mcs/web/research/projects/petsc/documentation/installation.html /mcs/web/research/projects/petsc/download/index.html
scp ${PETSC_DIR}/src/docs/website/documentation/index.html petsc@login.mcs.anl.gov:/mcs/web/research/projects/petsc/documentation/
scp ${PETSC_DIR}/src/docs/website/documentation/installation.html petsc@login.mcs.anl.gov:/mcs/web/research/projects/petsc/documentation/
scp ${PETSC_DIR}/src/docs/website/download/index.html petsc@login.mcs.anl.gov:/mcs/web/research/projects/petsc/download/

# update websnapshot. 'petsc-current' link is updated manually
echo "*** updating snapshot ***"
ssh petsc@login.mcs.anl.gov " \
  cd  /mcs/web/research/projects/petsc; \
  /bin/tar -xzf /mcs/ftp/pub/petsc/release-snapshots/petsc-${VERSION}.tar.gz; \
  /bin/rm -f petsc-${RELEASE}; \
  /bin/ln -s petsc-${VERSION} petsc-${RELEASE}"

#######################################
# M bin/maint/update-docs.py
# M include/petscversion.h
# M src/docs/tex/manual/intro.tex
# M src/docs/tex/manual/manual.tex
# M src/docs/website/documentation/changes/300.html
# M src/docs/website/documentation/changes/dev.html
# M src/docs/website/documentation/changes/index.html
# M src/docs/website/documentation/installation.html
# M src/docs/website/download/index.html
# M src/docs/website/index.html
# A src/docs/website/documentation/changes/31.html

# ? bin/win32fe/win32fe.exe
# ? bin/win32fe/win32feutils.dll

