#!/bin/sh
# Syntax:
#   $0 <path-to-top_srcdir> <version-stamp-file>
#
# <path-to-top_srcdir> may be relative
# <version-stamp-file> is relative to the dist top_srcdir
#
# Test this script by running
#     rm -rf autom4te.cache/ && autoreconf -vis . && sed -n "/^# Generated by GNU/p" configure
#
# If we run into an error, we cannot abort the "autoreconf" run by
# exiting with a non-0 error code. (We do exit non-0 in that case
# anyway for help when testing this script.)
# The only thing we can do to report an error is to write to stderr
# which appears as "autoreconf" output, and to write a version number
# to stdout which indicates an error. The "configure" script can
# then check whether $PACKAGE_VERSION indicates such an error.

# Parse the command line arguments
prog="$(basename "$0")"
top_srcdir="${1-.}"
test -d "$top_srcdir" || { \
    echo "$prog: Error: Could not change to top_srcdir '$1'" >&2; \
    echo "version_error_1" | ${TR-tr} -d '\012'
    exit 2; \
}
version_stamp="${2-version-stamp}"

# echo "$prog: Error: Some error happend." >&2
# echo "version_error_2" | ${TR-tr} -d '\012'
# exit 2

# Is this a dist source tree?
# If so, use the version number from the version_stamp file.
if test -f "$top_srcdir/$version_stamp"; then
    cat "$top_srcdir/$version_stamp" | ${TR-tr} -d '\012'
    exit
fi

# Is this part of a git checkout or an expanded github snapshot tarball?
test -f "$top_srcdir/../CMakeLists.txt" || { \
    echo "$prog: Error: top-level avrdude CMakeLists.txt file not found" >&2; \
    echo "version_error_3" | ${TR-tr} -d '\012'
    exit 2; \
}

if PROJECT_VERSION="$(${SED-sed} -n 's/project(avrdude[[:space:]]\{1,\}VERSION[[:space:]]\{1,\}\([0-9\.]\{1,\}\)[[:space:]]\{1,\}.*/\1/p' "$top_srcdir/../CMakeLists.txt")"; then
    :
else
    echo "$prog: Error parsing top-level avrdude 'CMakeLists.txt'." >&2
    echo "version_error_4" | ${TR-tr} -d '\012'
    exit 2
fi

test -n "$PROJECT_VERSION" || { \
    echo "$prog: Error: Could not find project(...) in top-level avrdude 'CMakeLists.txt'" >&2; \
    echo "version_error_5" | ${TR-tr} -d '\012'
    exit 2; \
}

# If GIT_DIR is set, use it. If not, try "$top_srcdir/../.git".
test -n "$GIT_DIR" || { \
    GIT_DIR="$top_srcdir/../.git"; \
    export GIT_DIR; \
}

# Working with a git source tree
if test -d "$GIT_DIR"; then
    GIT_COMMIT_HASH="$(${GIT-git} log -1 --format=%h)" || { \
        echo "$prog: Error: Cannot run 'git log' for commit hash" >&2; \
        echo "version_error_71" | ${TR-tr} -d '\012'; \
        exit 2; \
    }
    GIT_COMMIT_DATE="$(${GIT-git} log -1 --format=%ad --date=format:%Y%m%d)" || { \
        echo "$prog: Error: Cannot run 'git log' for commit date" >&2; \
        echo "version_error_72" | ${TR-tr} -d '\012'; \
        exit 2; \
    }
    GIT_TAG_HASH="$(${GIT-git} log -1 --tags --format=%h)" || { \
        echo "$prog: Error: Cannot run 'git log' for tag hash" >&2; \
        echo "version_error_73" | ${TR-tr} -d '\012'; \
        exit 2; \
    }
    if test "x$GIT_COMMIT_HASH" = "x$GIT_TAG_HASH"; then
        echo "${PROJECT_VERSION}" | ${TR-tr} -d '\012'
        exit
    else
        echo "${PROJECT_VERSION}-${GIT_COMMIT_DATE}" | ${TR-tr} -d '\012'
        exit
    fi
else
    # Building a github release tarball or github snapshot tarball.
    #
    # Presume this is a release version, because who would build a
    # non-release version from a snapshot tarball?
    echo "${PROJECT_VERSION}" | ${TR-tr} -d '\012'
    exit
fi

# If everything else has failed, call this version "devel"
echo "devel" | ${TR-tr} -d '\012'
exit 2
