#!/bin/sh
#
# Copyright © 2004-2005 The Trustees of Indiana University and Indiana
#                         University Research and Technology
#                         Corporation.  All rights reserved.
# Copyright © 2004-2005 The University of Tennessee and The University
#                         of Tennessee Research Foundation.  All rights
#                         reserved.
# Copyright © 2004-2005 High Performance Computing Center Stuttgart,
#                         University of Stuttgart.  All rights reserved.
# Copyright © 2004-2005 The Regents of the University of California.
#                         All rights reserved.
# Copyright © 2008-2013  Cisco Systems, Inc.  All rights reserved.
# Copyright © 2012-2020 Inria.  All rights reserved.
# $COPYRIGHT$
# 
# Additional copyrights may follow
# 
# $HEADER$
#

#########################################################################
# Make greek and non-greek distribution (i.e., non-snapshot) tarballs
#########################################################################

#
# Version of auto tools that we want
#

AM_TARGET_VERSION=1.16.2
AC_TARGET_VERSION=2.69
LT_TARGET_VERSION=2.4.6

#
# First things first -- check that the auto versions that we have are
# the ones that we want.
#

check_gnu_version() {
    prog="$1"
    target="$2"

    ver="`$prog --version | head -n 1 | cut -d\  -f 4`"
    if test "$ver" != "$target"; then 
        cat <<EOF
ERROR: Program "$prog" does not have the correct/expected version:
       Found: $ver

Expected versions:
Automake: $AM_TARGET_VERSION
Autoconf: $AC_TARGET_VERSION
Libtool:  $LT_TARGET_VERSION

Either change this script to match the found version, or install
the correct version of the tools.
EOF
        exit 1
    fi
}

#
# Subroutine to actually make a tarball
#

make_tarball() {
    #
    # Autogen
    #
    echo "*** Removing old configure..."
    # Ensure that configure is re-generated -- sometimes it won't be
    # (if it hasn't changed), and therefore VERSION could be newer
    # than configure, and that leads to all kinds of problems
    rm -f configure

    echo "*** Running autogen.sh..."
    rm -f success
    (./autogen.sh 2>&1 && touch success) | tee auto.out
    if test ! -f success; then
        echo "Autogen failed.  Aborting"
        exit 1
    fi

    #
    # Configure
    #
    echo "*** Running configure..."
    rm -f success
    (./configure 2>&1 && touch success) | tee config.out
    if test ! -f success; then 
        echo "Configure failed.  Aborting"
        exit 1
    fi

    #
    # Explicitly create the doxygen docs
    #
    echo "*** Making doxygen docs..."
    cd doc
    rm -rf doxygen-doc
    rm -f success
    (make doc 2>&1 && touch success) | tee doc.out
    if test ! -f success; then 
        echo "Making doxygen docs failed.  Aborting"
        exit 1
    fi

    #
    # Explicitly create the README file
    #
    echo "*** Making README..."
    rm -rf ../README
    rm -f success
    (make readme 2>&1 && touch success) | tee readme.out
    if test ! -f success; then 
        echo "Making README failed.  Aborting"
        exit 1
    fi
    rm -f success
    cd ..

    #
    # make tarball
    #
    echo "*** Running make distcheck..."
    save_LD=$LD_LIBRARY_PATH
    LD_LIBRARY_PATH=
    rm -f success
    (make distcheck 2>&1 && touch success) | tee dist.out
    if test ! -f success; then
        echo "Make distcheck failed.  Aborting"
        exit 1
    fi
    rm -f success
    LD_LIBRARY_PATH=$save_LD

    echo "*** All done"
}

#########################################################################
# main
#########################################################################

echo "*** Checking GNU tools versions..."
check_gnu_version automake $AM_TARGET_VERSION
check_gnu_version autoconf $AC_TARGET_VERSION
check_gnu_version libtool $LT_TARGET_VERSION

#
# Verify that we're in a top hwloc dir
#
echo "*** Checking to ensure in top-level HWLOC directory..."
if test -f VERSION -a -f configure.ac -a -f config/hwloc.m4 -a -e .git; then
    happy=1
else
    echo "Do not appear to be in a hwloc git clone top directory.  Abort!"
    exit 1
fi

#
# Set VERSION to not build a snapshot
#
echo "*** Git reverting VERSION files..."
git diff VERSION | patch -p1 -R

#
# Substitute in the release date and disable snapshot build
#
echo "*** Substituting in release date and disabling snapshot..."
release_date=`export LANG=; export LC_ALL=; export LC_TIME=; date '+%b %d, %Y'`
sed -e "s/^date=.*/date=\"$release_date\"/" \
    -e "s/^snapshot=.*/snapshot=0/" \
    VERSION > VERSION.new
cp -f VERSION.new VERSION
rm -f VERSION.new

#
# Make 2 tarballs:
#
# - one with the greek
# - one without the greek
#
# unless the user specifically said --greekonly, then only make the
# greek tarball.  Making both tarballs at once allows us to guarantee
# to have two tarballs -- one greek and one not -- that have exactly
# the same git describe (as opposed to, for example, running this
# script to make a greek tarball, then running it again to make a
# non-greek tarball -- there is a race condition that someone could
# commit in the meantime and change the git describe number in the 2nd
# tarball)
#

# First, make greek tarball

echo "*** Making greek tarball"
make_tarball

# Now if ! --greekonly, make the non-greek tarball

if test "$1" != "-greekonly" -a "$1" != "--greekonly"; then
    echo "*** REMOVING ALL GREEK FROM VERSION NUMBERS!!"
    sed -e 's/^greek=.*/greek=/' VERSION > VERSION.new
    cp -f VERSION.new VERSION
    rm -f VERSION.new
    echo "Making non-greek tarball"
    make_tarball
fi

# Put the VERSION and README files back the way they were
echo "*** Reverting VERSION and README files"
git diff VERSION README | patch -p1 -R
