#!/bin/bash
#
# Copyright (C) 2004-2008 Anders Logg
#
# This file is part of FFC.
#
# FFC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFC. If not, see <http://www.gnu.org/licenses/>.
#
# This script creates a new release of FFC

# Make sure FFC is installed (so we may run unit tests)
#echo '--- Uninstalling FFC'
#sudo python setup.py install

# Make sure we have the current version
echo '--- Synchronizing repository'
sleep 1
hg commit
hg pull ssh://ffc@fenics.org/hg/ffc
hg merge
hg commit
hg update
hg push ssh://ffc@fenics.org/hg/ffc

# Update version numbers
echo '--- Update version number in ChangeLog'
sleep 1
emacs -nw ChangeLog
echo '--- Update version number in constants.py'
sleep 1
emacs -nw ffc/common/constants.py
echo '--- Update version number in setup.py'
sleep 1
emacs -nw setup.py

# Install latest version
echo "Running commands for installing FFC locally on my machine. Sorry about that."
echo "We need to figure out a better way to organize the makedist script. /Anders"
fenics-install
fenics-dev

# Get the version number
VERSION=`grep 'FFC_VERSION' ffc/common/constants.py | cut -d'"' -f2`
echo "--- Version number is $VERSION"

# Run tests
echo '--- Running tests'
cd test
python test.py
echo '--- Only version numbers should differ, press return to continue'
read
cd regression
./update-references
cd ../..

# Run benchmark problem
echo '--- Running benchmark problem'
cd bench
echo "FFC version $VERSION" >> bench.log
date >> bench.log
echo "" >> bench.log
./bench >> bench.log
cd ../

# Tag repository
hg tag $VERSION

# Commit changes to hg
echo '--- Pushing changes to parent repository'
sleep 1
hg commit
hg push ssh://ffc@fenics.org/hg/ffc

# Create archive
hg archive -t tgz ffc-$VERSION.tar.gz

# Copy files to web page
echo '--- Copying files to web server'
scp ffc-$VERSION.tar.gz fenics@fenics.org:www.fenics.org/pub/software/ffc/v0.7
scp ChangeLog fenics@fenics.org:www.fenics.org/pub/software/ffc/
scp TODO fenics@fenics.org:www.fenics.org/pub/software/ffc/

# Notify ffc-dev of the new version
echo '--- Notifying mailing list'
SUBJECT="Version "$VERSION" of FFC released"
cat ChangeLog | mail -s "$SUBJECT" ffc-dev@fenics.org

# Edit web pages
echo '--- Edit web pages'
ssh -t fenics@fenics.org '/home/fenics/local/bin/news'
firefox http://www.fenics.org/wiki/Download

# Notify pypi
python setup.py register
