#! /bin/sh

##  Build a snapshot of the current tree.
##
##  Meant to be run on a fresh Git checkout, this script does the necessary
##  work to generate a snapshot.  It expects to be invoked from the top level
##  of the source tree and leaves the generated snapshot in that same
##  directory as a .tar.gz file.
##
##  Snapshot generation will fail if the tree will not compile or if make test
##  fails.  In either case, the output is left in snapshot.log.
##
##  This script takes one argument, a string representing what tree the
##  snapshot is being taken from.  Generally this string is GIT, CURRENT, or
##  STABLE.  If this argument is BETA or RC, a second argument is needed: the
##  number of the beta version (for instance b1 or b2) or the release
##  candidate.
##
##  Examples of use:
##
##    support/mksnapshot GIT
##    support/mksnapshot CURRENT
##    support/mksnapshot STABLE
##    support/mksnapshot BETA b1
##    support/mksnapshot RC rc2

set -e

# Show detailed testing results.
export C_TAP_VERBOSE=1

date=$(date -u +%Y%m%d)
tree="$1"

if [ -z "$tree" ]; then
    echo "$0: no tree name specified" >&2
    exit 1
fi

if [ "$tree" = "BETA" ]; then
    number="$2"
    extension=beta
    releasename="beta release"
    if [ -z "$number" ]; then
        echo "$0: no version specified for $tree" >&2
        exit 1
    fi
elif [ "$tree" = "RC" ]; then
    number="$2"
    extension=release-candidate
    releasename="release candidate"
    if [ -z "$number" ]; then
        echo "$0: no version specified for $tree" >&2
        exit 1
    fi
else
    exec >snapshot.log 2>&1

    # Run a first build (Perl and Python are the only optional packages
    # that need being explicitly passed to configure so as to be used,
    # even though libraries are installed on the system).
    ./autogen
    ./configure --with-perl --with-python
    make warnings
    cd contrib
    make warnings
    cd ../frontends
    make optional
    cd ../tests
    make warnings
    make
    cd ..
    make check-manifest

    # Run another build, with other configure options.
    make distclean
    ./configure --disable-shared
    make warnings
    make test

    # Run a third build, with other configure options.
    make distclean
    ./configure --enable-tagged-hash
    make warnings
    make test

    # Run yet another build, with other configure options.
    # This one is without features requiring external libraries.
    make distclean
    ./configure --enable-keywords --without-bdb --without-blacklist \
        --without-canlock --without-krb5 --without-openssl --without-sasl \
        --without-sqlite3 --without-zlib
    make warnings
    make test

    # Run a last build, with other configure options.
    make distclean
    ./configure --enable-largefiles
    make warnings
    make test
fi

GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_HASH=$(git log -1 --pretty=%H)
GIT_COMMIT_DATE=$(git log -1 --pretty=%cd --date=format:%Y-%m-%d)

if [ "$tree" = "BETA" -o "$tree" = "RC" ]; then

    cat >README.$extension <<EOF
This is a $releasename based on the current development version
of INN.  It was made on:

    $(LC_TIME=C date -u +"%B %e, %Y @ %I:%M %p %Z")

based on Git commit hash $GIT_HASH
made on $GIT_COMMIT_DATE in the $GIT_BRANCH branch.

Although it should work fine, this code should still be considered
experimental.  If you find any bugs, we'd like to know at
<inn-workers@lists.isc.org>.

Your feedback is also more than welcome on the mailing list
<inn-workers@lists.isc.org>.  See README for more information.
EOF

else

    cat >README.snapshot <<EOF
This is a snapshot of the current development version of INN, pulled
automatically from the Git repository.  It was made on:

    $(LC_TIME=C date -u +"%B %e, %Y @ %I:%M %p %Z")

based on Git commit hash $GIT_HASH
made on $GIT_COMMIT_DATE in the $GIT_BRANCH branch.

This code should be considered experimental.  Only a default compile and
automated testing is done before it is made available.  If it breaks, we'd
like to know at <inn-workers@lists.isc.org>, but if it causes your system to
explode, don't blame us.

If you are using this code, it's highly recommended that you be on the
<inn-workers@lists.isc.org> mailing list.  See README for more information.
EOF

fi

if [ "$tree" = "BETA" -o "$tree" = "RC" ]; then
    make release RELEASENUMBER="$number" RELEASEEXTENSION="$extension"
elif [ "$tree" = "GIT" ]; then
    if [ "$GIT_BRANCH" = main ]; then
        VERSION=$(grep -w '^VERSION' Makefile.global.in | awk '{ print $3 }')
        VERSION=$(echo "$VERSION" | sed 's/.[0-9]*$//')
        date=$(git log -1 --pretty=%cd --date=format:%Y%m%d)
        SNAPDIR=inn-"$VERSION"-"$date"
    else
        # Do not take into account tags of beta or release candidate versions.
        SNAPDIR=inn-"$(git describe --tags --exclude '*[A-Za-z]*')"
    fi
    make snapshot SNAPDIR="$SNAPDIR" SNAPDATE="$date"
else
    make snapshot SNAPSHOT="$tree" SNAPDATE="$date"
fi
