#!/bin/bash
#***********************************************************************
# This file is part of OpenMolcas.                                     *
#                                                                      *
# OpenMolcas is free software; you can redistribute it and/or modify   *
# it under the terms of the GNU Lesser General Public License, v. 2.1. *
# OpenMolcas is distributed in the hope that it will be useful, but it *
# is provided "as is" and without any express or implied warranties.   *
# For more details see the full text of the license in the file        *
# LICENSE or in <http://www.gnu.org/licenses/>.                        *
#***********************************************************************
#
# gives (parsed) version of the latest build,
# based on the hash that is in .stamp

# magical key for help indexer: %help

# functions
show_usage () {
    echo
    echo "Export the Molcas source tree from a specific snapshot"
    echo "from git to a compressed tar archive."
    echo
    echo "Usage:"
    echo
    echo "  $DRIVER_base export <snapshot>"
    echo
    echo "where: <snapshot> is a git tag or commit hash"
    echo
    echo "Options:"
    echo "  -h | --help       print usage information"
    echo
}

show_what () {
    echo "Export MOLCAS source to a compressed tar archive."
}

error () {
    echo "error: $@" >&2
    exit 1
}

if [ -z "$MOLCAS_DRIVER"]
then
    MOLCAS_DRIVER="molcas"  
fi
DRIVER_base=`basename $MOLCAS_DRIVER`

# check if we know where Molcas is located
if [ -z $MOLCAS_SOURCE ]
then
        error "environment variable MOLCAS_SOURCE not set, exiting..."
fi

cd $MOLCAS_SOURCE

# parse command line options, read destination
while [ "$#" -ne 0 ]
do
    case "$1" in
        "-h"|"--help") show_usage; exit 0 ;;
        *) snapshot="$1" ;;
    esac
    shift
done

# determine compression tool
echo "exporting Molcas source using gzip compression"
ext="tar.gz"

if [ -e "$MOLCAS_SOURCE/.git" ] && type git >& /dev/null
then
        # find SHA1 of the commit and set an appropriate name
        if [ -z $snapshot ]
        then
                snapshot=HEAD
        fi
        sha1=$(git rev-parse --verify --quiet $snapshot)
        if [ -z $sha1 ]
        then
                error "invalid tag or hash: $snapshot"
        fi
        version=$(git describe --match "v*" $sha1)
        name=molcas-$version

        tempdir=$(mktemp -d)
        if [ ! -d $tempdir ]
        then
                error "could not create temporary directory, exiting..."
        fi

        echo "creating $name.$ext"


        echo -n "exporting main source ... "

        git archive --format=tar --prefix=$name/ $sha1 | (cd $tempdir && tar xf -)

        echo $sha1 > $tempdir/$name/.stamp
        echo $version > $tempdir/$name/.molcasversion

        echo "done."

        # set modification times according to history, write version info
        echo -n "setting modification times ... "
        $MOLCAS/sbin/setmtime $tempdir/$name/ $sha1
        echo "done."

        # making the final archive
        echo -n "creating compressed archive ... "
        (cd $tempdir && tar zcf $name.$ext $name/)
        mv $tempdir/$name.$ext $MOLCAS_SOURCE/ && rm -rf $tempdir
        echo "done."
else
        error "not a git repository or git not installed, exiting..."
fi
