#!/bin/sh
#
# Copyright by The HDF Group.
# Copyright by the Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of HDF.  The full HDF copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the COPYING file, which can be found at the root of the source code
# distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
#

# Make a release of hdf4.
#
# Programmer: Robb Matzke
# Creation date: on or before 1998-01-29.
#
# Modifications
#   Robb Matzke, 1999-07-16
#   The SunOS 5.6 sed *must* have slashes as delimiters. I changed things like
#   `sed s+/CVS++' to `sed 's/\/CVS//'
#
#   Albert Cheng, 1999-10-26
#   Moved the MANIFEST checking to a separate command file so that
#   it can be invoked individually.
#
#   Albert Cheng, 2004-08-14
#   Added the --private option.
#
#   James Laird, 2005-09-07
#   Added the md5 method.
#
#   Larry Knox, 2017-06-16
#   Added cmake-tgz and cmake-zip methods
#   Replaced optional md5 method with automatic md5 checksum generation for
#   all archives (from hdf5 release script)

# Function definitions
#
# Print Usage page
USAGE()
{
cat << EOF
Usage: $0 -d <dir> [-h] [--nocheck] [--private] <methods> ...
   -d <dir>    The name of the directory where the releas(es) should be
                placed.
   -h           print the help page.
   --nocheck    Ignore errors in MANIFEST file.
   --private    Make a private release with today's date in version information.

This must be run at the top level of the source directory.
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" is assumed):
    tar         -- use tar and don't do any compressing.
    gzip        -- use gzip with "-9" and append ".gz" to the output name.
    cmake-tgz   -- create a tar file using the gzip default level with a
                   build-unix.sh command file and all other CMake files needed
                   to build HDF4 source using CMake on unix machines.
    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.
    cmake-zip   -- convert all text files to DOS style and create a zip file
                   including cmake scripts and .bat files to build HDF4 source
                   using CMake on Windows.

An md5 checksum is produced for each archive created and stored in the md5 file.

Examples:

    $ bin/release -d /tmp
    /tmp/hdf-4.2.11-RELEASE.txt
    /tmp/hdf-4.2.11.md5
    /tmp/hdf-4.2.11.tar

    $ bin/release -d /tmp gzip
    /tmp/hdf-4.2.11-RELEASE.txt
    /tmp/hdf-4.2.11.md5
    /tmp/hdf-4.2.11.tar.gz

    $ bin/release -d /tmp tar gzip zip
    /tmp/hdf-4.2.11-RELEASE.txt
    /tmp/hdf-4.2.11.md5
    /tmp/hdf-4.2.11.tar
    /tmp/hdf-4.2.11.tar.gz
    /tmp/hdf-4.2.11.zip

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/tmpdir$$
    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
}

# Function name: tar2cmakezip
# Convert the release tarball to a Windows zipball with files to run CMake build.
#
# Programmer: Larry Knox
# Creation date: 2017-02-20
#
# 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. add build-unix.sh script.
# 3. add SZIP.tar.gz, ZLib.tar.gz and cmake files to top level directory.
# 4. create gzipped tar file with these contents:
#        build-unix.sh          script
#        hdf4-<version>         source code directory extracted from tar file
#        CTestScript.cmake      cmake file copied from <hdf4 source code>/config/cmake/scripts
#        HDF4config.cmake       cmake file copied from <hdf4 source code>/config/cmake/scripts
#        HDF4options.cmake      cmake file copied from <hdf4 source code>/config/cmake/scripts
#        SZip.tar.gz            copied from /mnt/scr1/pre-release/hdf4/CMake
#        ZLib.tar.gz            copied from /mnt/scr1/pre-release/hdf4/CMake
#        JPEG8d.tar.gz          copied from /mnt/scr1/pre-release/hdf4/CMake


# Parameters:
# $1 version
# $2 release tarball
# $3 output zipball file name
#
# Returns 0 if successful; 1 otherwise
#

# need function to create another temporary directory, extract the
# $tmpdir/$HDF4_VERS.tar into it, add (create) build-unix.sh,
# CTestScript.cmake, HDF5config.cmake, SZIP.tar.gz, ZLib.tar.gz,
# and JPEG8d.tar.gz, and then zip it.
tar2cmakezip()
{
    if [ $# -ne 3 ]; then
        echo "usage: tar2cmakezip <tarfilename> <zipfilename>"
        return 1
    fi
    cmziptmpdir=/tmp/cmziptmpdir
    cmziptmpsubdir=$cmziptmpdir/CMake-$HDF4_VERS
    mkdir -p $cmziptmpsubdir
    version=$1
    tarfile=$2
    zipfile=$3

    # step 1: untar tarball in cmgztmpdir
    (cd $cmziptmpsubdir; tar xf -) < $tarfile
    # sanity check
    if [ ! -d $cmziptmpsubdir/$version ]; then
        echo "untar did not create $cmziptmpsubdir/$version source dir"
        # cleanup
        rm -rf $cmziptmpdir
        return 1
    fi

    # step 2: add batch file for building CMake on window
    (cd $cmziptmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=VS2013 -C Release -V -O hdf4.log" > build-VS2013-32.bat; chmod 755 build-VS2013-32.bat)
    (cd $cmziptmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=VS201364 -C Release -V -O hdf4.log" > build-VS2013-64.bat; chmod 755 build-VS2013-64.bat)
    (cd $cmziptmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=VS2015 -C Release -V -O hdf4.log" > build-VS2015-32.bat; chmod 755 build-VS2015-32.bat)
    (cd $cmziptmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=VS201564 -C Release -V -O hdf4.log" > build-VS2015-64.bat; chmod 755 build-VS2015-32.bat)
    (cd $cmziptmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=VS2017 -C Release -V -O hdf4.log" > build-VS2017-32.bat; chmod 755 build-VS2017-32.bat)
    (cd $cmziptmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=VS201764 -C Release -V -O hdf4.log" > build-VS2017-64.bat; chmod 755 build-VS2017-64.bat)

    # step 3: add SZIP.tar.gz, ZLib.tar.gz and cmake files
    cp /mnt/scr1/pre-release/hdf4/CMake/SZip.tar.gz $cmziptmpsubdir
    cp /mnt/scr1/pre-release/hdf4/CMake/ZLib.tar.gz $cmziptmpsubdir
    cp /mnt/scr1/pre-release/hdf4/CMake/JPEG8d.tar.gz $cmziptmpsubdir
    cp $cmziptmpsubdir/$version/config/cmake/scripts/CTestScript.cmake $cmziptmpsubdir
    cp $cmziptmpsubdir/$version/config/cmake/scripts/HDF4config.cmake $cmziptmpsubdir
    cp $cmziptmpsubdir/$version/config/cmake/scripts/HDF4options.cmake $cmziptmpsubdir

    # step 4: 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 $cmziptmpsubdir/$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 $cmziptmpdir; zip -9 -y -r -q CMake-$version.zip *)
    mv $cmziptmpdir/CMake-$version.zip $zipfile

    # cleanup
    rm -rf $cmziptmpdir
}

# Function name: tar2cmaketgz
# Convert the release tarball to a Windows zipball with files to run CMake build.
#
# Programmer: Larry Knox
# Creation date: 2017-02-20
#
# 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. add build-unix.sh script.
# 3. add SZIP.tar.gz, ZLib.tar.gz and cmake files to top level directory.
# 4. create gzipped tar file with these contents:
#        build-unix.sh          script
#        hdf4-<version>         source code directory extracted from tar file
#        CTestScript.cmake      cmake file copied from <hdf4 source code>/config/cmake/scripts
#        HDF4config.cmake       cmake file copied from <hdf4 source code>/config/cmake/scripts
#        HDF4options.cmake      cmake file copied from <hdf4 source code>/config/cmake/scripts
#        SZip.tar.gz            copied from /mnt/scr1/pre-release/hdf4/CMake
#        ZLib.tar.gz            copied from /mnt/scr1/pre-release/hdf4/CMake
#        JPEG8d.tar.gz            copied from /mnt/scr1/pre-release/hdf4/CMake


# Parameters:
# $1 version
# $2 release tarball
# $3 output zipball file name
#
# Returns 0 if successful; 1 otherwise
#

tar2cmaketgz()
{
    if [ $# -ne 3 ]; then
        echo "usage: tar2cmaketgz <tarfilename> <tgzfilename>"
        return 1
    fi
    cmgztmpdir=/tmp/cmgztmpdir$$
    cmgztmpsubdir=$cmgztmpdir/CMake-$HDF4_VERS
    mkdir -p $cmgztmpsubdir
    version=$1
    tarfile=$2
    tgzfile=$3

    # step 1: untar tarball in cmgztmpdir
    (cd $cmgztmpsubdir; tar xf -) < $tarfile
    # sanity check
    if [ ! -d $cmgztmpsubdir/$version ]; then
        echo "untar did not create $cmgztmpdir/$version source dir"
        # cleanup
        rm -rf $cmgztmpdir
        return 1
    fi


    # step 2: add build-unix.sh script
    (cd $cmgztmpsubdir; echo "ctest -S HDF4config.cmake,BUILD_GENERATOR=Unix -C Release -V -O hdf4.log" > build-unix.sh; chmod 755 build-unix.sh)

    # step 3: add SZIP.tar.gz, ZLib.tar.gz and cmake files
    cp /mnt/scr1/pre-release/hdf4/CMake/SZip.tar.gz $cmgztmpsubdir
    cp /mnt/scr1/pre-release/hdf4/CMake/ZLib.tar.gz $cmgztmpsubdir
    cp /mnt/scr1/pre-release/hdf4/CMake/JPEG8d.tar.gz $cmgztmpsubdir
    cp $cmgztmpsubdir/$version/config/cmake/scripts/CTestScript.cmake $cmgztmpsubdir
    cp $cmgztmpsubdir/$version/config/cmake/scripts/HDF4config.cmake $cmgztmpsubdir
    cp $cmgztmpsubdir/$version/config/cmake/scripts/HDF4options.cmake $cmgztmpsubdir
    tar czf $DEST/CMake-$HDF4_VERS.tar.gz -C $cmgztmpdir . || exit 1

    # cleanup
    rm -rf $cmgztmpdir
}


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

# Defaults
DEST=releases
VERS=`perl bin/h4vers`
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

# Restore previous Version information
RESTORE_VERSION()
{
    if [ X-${VERS_OLD} != X- ]; then
    echo restoring version information back to $VERS_OLD
    bin/h4vers -s $VERS_OLD
    VERS_OLD=
    fi
}


# 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
        ;;
    -*)
        echo "Unknown switch: $arg" 1>&2
        USAGE
        exit 1
        ;;
    *)
        methods="$methods $arg"
        ;;
    esac
done

# Default methods is tar and md5
if [ "X$methods" = "X" ]; then
    methods="tar"
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

# setup restoration in case of abort.
trap RESTORE_VERSION 0

if [ X$pmode = Xyes ]; then
    VERS_OLD=$VERS
    # Set version information to m.n.r-of$today.
    # (h4vers does not correctly handle just m.n.r-$today.)
    VERS=`echo $VERS | sed -e s/-.*//`-of$today
    echo Private release of $VERS
    bin/h4vers -s $VERS
fi

# Store hdf4-$VERS ("hdf-4.2.11", e.g.) to a variable to avoid typos
HDF4_VERS=hdf-$VERS

test "$verbose" && echo "Releasing $HDF4_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/H4_MANIFEST
grep '^\.' MANIFEST | grep -v _DO_NOT_DISTRIBUTE_ >$MANIFEST

# Prepare the source tree for a release.
ln -s `pwd` $tmpdir/$HDF4_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_notes/RELEASE.txt with release information in
# line 1.
for f in README.txt release_notes/RELEASE.txt; do
    echo "HDF 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

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

# Compress
MD5file=$HDF4_VERS.md5
cp /dev/null $DEST/$MD5file
for comp in $methods; do
    case $comp in
    tar)
        cp -p $tmpdir/$HDF4_VERS.tar $DEST/$HDF4_VERS.tar
            (cd $DEST; md5sum $HDF4_VERS.tar >> $MD5file)
        ;;
    gzip)
        test "$verbose" && echo "   Running gzip..." 1>&2
        gzip -9 <$tmpdir/$HDF4_VERS.tar >$DEST/$HDF4_VERS.tar.gz
            (cd $DEST; md5sum $HDF4_VERS.tar.gz >> $MD5file)
        ;;
        cmake-tgz)
            test "$verbose" && echo "   Creating CMake tar.gz file..." 1>&2
            tar2cmaketgz $HDF4_VERS $tmpdir/$HDF4_VERS.tar $DEST/CMake-$HDF4_VERS.tar.gz 1>&2
            (cd $DEST; md5sum CMake-$HDF4_VERS.tar.gz >> $MD5file)
            ;;
    bzip2)
        test "$verbose" && echo "   Running bzip2..." 1>&2
        bzip2 -9 <$tmpdir/$HDF4_VERS.tar >$DEST/$HDF4_VERS.tar.bz2
            (cd $DEST; md5sum $HDF4_VERS.tar.bz2 >> $MD5file)
        ;;
    zip)
        test "$verbose" && echo "   Creating zip ball..." 1>&2
        tar2zip $HDF4_VERS $tmpdir/$HDF4_VERS.tar $DEST/$HDF4_VERS.zip 1>&2
            (cd $DEST; md5sum $HDF4_VERS.zip >> $MD5file)
        ;;
        cmake-zip)
            test "$verbose" && echo "   Creating CMake-zip ball..." 1>&2
            tar2cmakezip $HDF4_VERS $tmpdir/$HDF4_VERS.tar $DEST/CMake-$HDF4_VERS.zip 1>&2
            (cd $DEST; md5sum CMake-$HDF4_VERS.zip >> $MD5file)
            ;;
    *)
        echo "***Error*** Unknown method $comp"
        exit 1
        ;;
    esac
done

# Copy the RELEASE.txt to the release area.
cp release_notes/RELEASE.txt $DEST/$HDF4_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
