#!/bin/sh
# *** Under construction ***
# This is a quick installation to use for v2.2.5 release.
# This requires more work for further use.
#
# Copyright by The HDF Group.
# Copyright by the Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of H4H5Tools.  The full H4H5Tools copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# contained in the COPYING file, which can be found at the root of the
# source code distribution tree. The copyright notice can also be found
# at https://www.hdfgroup.org/licenses.  If you do not have access to
# either file, you may request a copy from help@hdfgroup.org.
#

# Make a release of H4H5Tools.
# This is modified from the same command in the HDF5 library.
#
# Programmer: Albert Cheng
# Creation date: 2013-07-24
#
# Modifications
#

# Function definitions
#
# Print Usage page
USAGE()
{
cat << EOF
Usage: $0 [--nocheck] [-d <dir>] [-h] <methods> ...
   -d DIR    The name of the directory where the releas(es) should be
                placed.  By default, the directory is ./releases

   --docver BRANCHNAME  This is added for 1.8 and beyond to get the correct
                        version of documentation files from the hdf5docs
                        repository.  BRANCHNAME for v1.8 should be hdf5_1_8.

   --nocheck    Ignore errors in MANIFEST file.

   --private    Make a private release with today's date in version information.

The other command-line options are the names of the programs to use
for compressing the resulting tar archive (if none are given then
"tar md5" is assumed):

    tar         -- use tar and don't do any compressing.
    compress    -- use compress and append ".Z" to the output name.
    gzip        -- use gzip with "-9" and append ".gz" to the output name.
    bzip2       -- use bzip2 with "-9" and append ".bz2" to the output name.
    zip         -- convert all text files to DOS style and form a zip file for Windows use.
    md5         -- produce a md5 checksum in addition to the archive.
    doc         -- produce the latest doc tree in addition to the archive.

Examples:

    $ release
    releases/h4h5tools-1.0.38.tar
    releases/h4h5tools-1.0.38.tar.md5

    $ release gzip
    releases/h4h5tools-1.0.38.tar.gz

    $ release -d /tmp tar compress gzip bzip2i zip md5
    /tmp/h4h5tools-1.0.38.tar
    /tmp/h4h5tools-1.0.38.tar.Z
    /tmp/h4h5tools-1.0.38.tar.gz
    /tmp/h4h5tools-1.0.38.tar.bz2
    /tmp/h4h5tools-1.0.38.tar.zip
    /tmp/h4h5tools-1.0.38.tar.md5

EOF

}
# Function name: tar2zip
# Convert the release tarball to a Windows zipball.
#
# Programmer: Albert Cheng
# Creation date: 2014-04-23
#
# Modifications
#
# Steps:
# 1. untar the tarball in a temporary directory;
#    Note: do this in a temporary directory to avoid changing
#    the original source directory which may be around.
# 2. convert all its text files to DOS (LF-CR) style;
# 3. form a zip file which is usable by Windows users.
#
# Parameters:
# $1 version
# $2 release tarball
# $3 output zipball file name
#
# Returns 0 if successful; 1 otherwise
#
tar2zip()
{
    if [ $# -ne 3 ]; then
        echo "usage: tar2zip <tarfilename> <zipfilename>"
        return 1
    fi
    ztmpdir=/tmp/ztmpdir$$
    mkdir -p $ztmpdir
    version=$1
    tarfile=$2
    zipfile=$3

    # step 1: untar tarball in ztmpdir
    (cd $ztmpdir; tar xf -) < $tarfile
    # sanity check
    if [ ! -d $ztmpdir/$version ]; then
        echo "untar did not create $ztmpdir/$version source dir"
        # cleanup
        rm -rf $ztmpdir
        return 1
    fi
    # step 2: convert text files
    # There maybe a simpler way to do this.
    # options used in unix2dos:
    # -k   Keep the date stamp
    # -q   quiet mode
    # grep redirect output to /dev/null because -q or -s are not portable.
    find $ztmpdir/$version | \
        while read inf; do \
            if file $inf | grep "$inf\: .*text" > /dev/null 2>&1 ; then \
                unix2dos -q -k $inf; \
            fi\
        done
    # step 3: make zipball
    # -9 maximum compression
    # -y Store symbolic links as such in the zip archive
    # -r recursive
    # -q quiet
    (cd $ztmpdir; zip -9 -y -r -q $version.zip $version)
    mv $ztmpdir/$version.zip $zipfile

    # cleanup
    rm -rf $ztmpdir
}

# This command must be run at the top level of the h4h5tools source directory.
# Verify this requirement.
if [ ! \( -f configure -a -f bin/release \) ]; then
    echo "$0 must be run at the top level of the h4h5tools source directory"
    exit 1
fi

# Defaults
DEST=releases
VERS=`bin/h4h5tools_vers`
VERS_OLD=
test "$VERS" || exit 1
verbose=yes
check=yes
release_date=`date +%F`
today=`date +%Y%m%d`
pmode='no'
tmpdir="../#release_tmp.$$"    # tmp work directory
DOC_URL=http://svn.hdfgroup.uiuc.edu/hdf5doc/trunk
CPPLUS_RM_NAME=cpplus_RM

# Command-line arguments
while [ -n "$1" ]; do
    arg=$1
    shift
    case "$arg" in
    -d)
        DEST=$1
        shift
        ;;
    --nocheck)
        check=no
        ;;
    -h)
        USAGE
        exit 0
        ;;
    --private)
        pmode=yes
        ;;
        --docver)
            DOCVERSION=$1
            shift
            ;;
    -*)
        echo "Unknown switch: $arg" 1>&2
        USAGE
        exit 1
        ;;
    *)
        methods="$methods $arg"
        ;;
    esac
done

# Default methods are tar and md5
if [ "X$methods" = "X" ]; then
    methods="gzip md5"
fi

# Create the temporay work directory.
if mkdir $tmpdir; then
    echo "temporary work directory for release.  "\
         "Can be deleted after release completes." > $tmpdir/README
else
    echo "Failed to mkdir tmpdir($tmpdir)"
    exit 1
fi


# Store h4h5tools-$VERS ("h4h5tools-1.7.51", e.g.) to a variable to avoid typos
SW_VERS=h4h5tools-$VERS

test "$verbose" && echo "Releasing $SW_VERS to $DEST" 1>&2
if [ ! -d $DEST ]; then
    echo "   Destination directory $DEST does not exist" 1>&2
    exit 1
fi

# Check the validity of the MANIFEST file.
bin/chkmanifest || fail=yes
if [ "X$fail" = "Xyes" ]; then
    if [ $check = yes ]; then
        exit 1
    else
    echo "Continuing anyway..."
    fi
fi

# Create a manifest that contains only files for distribution.
MANIFEST=$tmpdir/H5_MANIFEST
grep '^\.' MANIFEST | grep -v _DO_NOT_DISTRIBUTE_ >$MANIFEST

# Prepare the source tree for a release.
ln -s `pwd` $tmpdir/$SW_VERS || exit 1
# Save a backup copy of Makefile if exists.
#test -f Makefile && mv Makefile $tmpdir/Makefile.x
#cp -p Makefile.dist Makefile

# Update README.txt and release_docs/RELEASE.txt with release information in
# line 1.
for f in README.txt release_docs/RELEASE.txt; do
    echo "H4H5TOOLS version $VERS released on $release_date" >$f.x
    sed -e 1d $f >>$f.x
    mv $f.x $f
    # Make sure new files are of the right access mode
    chmod 644 $f
done

# trunk is different than branches.
if [ "${DOCVERSION}" ]; then
    DOC_URL=http://svn.hdfgroup.uiuc.edu/hdf5doc/branches/${DOCVERSION}
fi

# Create the tar file
test "$verbose" && echo "   Running tar..." 1>&2
( \
    cd $tmpdir; \
    tar cf $SW_VERS.tar \
    `sed 's/^\.\//h4h5tools-'$VERS'\//' $MANIFEST` || exit 1 \
)

# Compress
for comp in $methods; do
    case $comp in
    tar)
        cp -p $tmpdir/$SW_VERS.tar $DEST/$SW_VERS.tar
        ;;
    compress)
        test "$verbose" && echo "   Running compress..." 1>&2
        compress -c <$tmpdir/$SW_VERS.tar >$DEST/$SW_VERS.tar.Z
        ;;
    gzip)
        test "$verbose" && echo "   Running gzip..." 1>&2
        gzip -9 <$tmpdir/$SW_VERS.tar >$DEST/$SW_VERS.tar.gz
        ;;
    bzip2)
        test "$verbose" && echo "   Running bzip2..." 1>&2
        bzip2 -9 <$tmpdir/$SW_VERS.tar >$DEST/$SW_VERS.tar.bz2
        ;;
    zip)
        test "$verbose" && echo "   Creating zip ball..." 1>&2
        tar2zip $SW_VERS $tmpdir/$SW_VERS.tar $DEST/$SW_VERS.zip 1>&2
        ;;
    md5)
        test "$verbose" && echo "   Creating checksum..." 1>&2
        (cd $tmpdir; md5sum $SW_VERS.tar ) > $DEST/$SW_VERS.tar.md5
        ;;
    doc)
            if [ "${DOCVERSION}" = "" ]; then
                DOCVERSION=trunk
            fi
        test "$verbose" && echo "   Creating docs..." 1>&2
        # Check out docs from svn repo
        (cd $tmpdir; svn co $DOC_URL > /dev/null) || exit 1
        # Create doxygen C++ RM
        (cd c++/src && doxygen cpp_doc_config > /dev/null ) || exit 1
        # Replace version of C++ RM with just-created version
            rm -rf $tmpdir/${DOCVERSION}/html/$CPPLUS_RM_NAME
            mv c++/src/$CPPLUS_RM_NAME $tmpdir/${DOCVERSION}/html/$CPPLUS_RM_NAME
            # Compress the docs and move them to the release area
            mv $tmpdir/$DOCVERSION $tmpdir/${SW_VERS}_docs
        (cd $tmpdir && tar cf ${SW_VERS}_docs.tar ${SW_VERS}_docs)
        mv $tmpdir/${SW_VERS}_docs.tar $DEST
        ;;
    esac
done

# Copy the RELEASE.txt to the release area.
cp release_docs/RELEASE.txt $DEST/$SW_VERS-RELEASE.txt

# Remove distributed Makefile and restore previous Makefile if existed.
rm -f Makefile
test -f $tmpdir/Makefile.x && mv $tmpdir/Makefile.x Makefile

# Restore OLD version information, then no need for trap.
if [ X$pmode = Xyes ]; then
    RESTORE_VERSION
    trap 0
fi

# Remove temporary things
rm -rf $tmpdir

exit 0
