#!/bin/sh
# *** Under construction ***
# This is a quick installation to use for v2.2.5 release.
# This requires more work for further use.

# This sets the version information of h4h5tools in all related files.
# Usage: setversion [-r] <version>
#    <version> is the new version number in the form of M.m.R-relstr where
#         M is major, m is minor, R is release and -relstr is the
#         optional subrelease.
#    -r is the optional release mode.
#         When given, all the files are set for release by adding today's
#         date as the release date. The default is development mode which
#         sets all version information files with the
#         "currently under development" notice.

# Function definitions
USAGE()
{
    echo "Usage: $0 [-h] [-r] <M.m.R>"
    echo "    -h: Show this help page"
    echo "    -r: This is a release version"
    echo "     M: Major version number"
    echo "     m: Minor version number"
    echo "     R: Release number"

}

# Print Debug output. Activate this by removing the leading colon.
# With a leading colon, it is a label, effectively a noop.
DPRINT()
{
    : echo $@
}

# Show a banner for message given as $*
# Need to quote $* to allow metacharacters in the message.
BANNER()
{
    echo ""
    echo "=================================================="
    echo "$*"
    echo "=================================================="
}

# Main body
# Check command arguments
if [ $# -eq 0 ]; then
    # Hardcode for release v2.2.5
    Versions=2.2.5
    echo $Versions
    exit 0
fi

if [ "$1" = "-h" ]; then
    USAGE
    exit 0
fi

if [ "$1" = "-r" ]; then
    # Release mode
    ReleaseDate="released on "`date +%F`
    shift
else
    ReleaseDate="currently under development"
fi

if [ $# -ne 1 ]; then
    USAGE
    exit 1
fi

arg=$1
DPRINT "====== arg=$arg ============"

# Verify given arg is the correct format and pull out the optional release
# string.
case "$arg" in
    [0-9]*.[0-9]*.[0-9]* )
    Versions=$arg
    ;;
    *)
    echo "Bad argument $arg"
    USAGE
    exit 1
    ;;
esac

# Parse the Version argument into different parts
Vmajor=`echo $Versions | cut -d. -f1`
Vminor=`echo $Versions | cut -d. -f2`
Vrelease=`echo $Versions | cut -d. -f3`
Vstring="H4toH5 converter library Version $Vmajor.$Vminor Release $Vrelease, $ReleaseDate"

DPRINT Versions=$Versions;
DPRINT "Major=$Vmajor; minor=$Vminor; Release=$Vrelease;"
DPRINT "String=$Vstring"

BANNER Update files with new version information $arg $ReleaseDate
# Update configure.ac by fixing the AC_INIT statement and run bin/reconfigure
f=configure.ac
echo Update ${f} ...
if [ -w $f ]; then
ed - $f <<EOF
/^AC_INIT/s/, \[[^]]*/, [$arg/
w
q
EOF
    echo running reconfigure ...
    bin/reconfigure
else
    echo $f is not writable
    USAGE
    exit 1
fi

# Update src/h5edit.h by fixing the version information macros.
# The "$" is needed so that the "5" in H5EDIT is not replaced.
f=lib/src/h4toh5.h
echo Update ${f} ...
if [ -w $f ]; then
ed - $f <<EOF
/define H4TOH5_LIBVER_MAJOR/s/[0-9][0-9]*$/$Vmajor/
/define H4TOH5_LIBVER_MINOR/s/[0-9][0-9]*$/$Vminor/
/define H4TOH5_LIBVER_RELEASE/s/[0-9][0-9]*$/$Vrelease/
/define H4TOH5_LIBVER_STRING/s/".*"/"$Vstring"/
w
q
EOF
else
    echo $f is not writable
    USAGE
    exit 1
fi


# Update README.txt and release_docs/RELEASE.txt by fixing the version information in the
# header line at the beginning.
for f in README.txt release_docs/RELEASE.txt; do
    echo Update ${f} ...
    if [ -w $f ]; then
ed - $f <<EOF
/^H4H5TOOLS version/s/.*/H4H5TOOLS version $arg $ReleaseDate./
w
q
EOF
    else
    echo $f is not writable
    USAGE
    exit 1
    fi
done
exit 0

BANNER Files require manual update
# The following are not automized yet and should be done by hand.
echo you need to update the following files:
echo doc/h5edit.docx:
echo doc/h5edit-Command-Language-Defininition.docx:
echo doc/H5edit_User_Guide.docx:
echo "   The version in the footer and last edit date"
echo "   Then regenerate the corresponding .pdf and .htm files"


BANNER Reminder of post-release tasks
# Post-release cleanup and reset for next release. Docuement here temporary.
# Should be moved to a permenant spot later.
cat <<EOF
Reminder:
Generate the release tarball by
$ svn export http://svn.hdfgroup.uiuc.edu/h5edit/trunk h5edit-<version>
  E.g., svn export http://svn.hdfgroup.uiuc.edu/h5edit/trunk h5edit-1.1.0
$ tar zcf h5edit-<version>.tar.gz h5edit-<version>
  E.g., tar zcf h5edit-1.1.0.tar.gz h5edit-1.1.0

After release tarball is generated, you need to tag the released version by:
$ svn copy http://svn.hdfgroup.uiuc.edu/h5edit/trunk http://svn.hdfgroup.uiuc.edu/h5edit/tags/<version>
  E.g., svn copy http://svn.hdfgroup.uiuc.edu/h5edit/trunk http://svn.hdfgroup.uiuc.edu/h5edit/tags/h5edit_1_1_0
Then in doc,
1. archive RELEASE.txt to History
2. empty the "Changes since ...<version>" section.
3. reset version in the header

EOF
