#!/bin/bash

# THIS FILE IS PART OF THE CYLC SUITE ENGINE.
# Copyright (C) 2008-2017 NIWA
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This script determines the cylc version string in a repository clone
# and echoes it to stdout. It is executed by lib/cylc/version.py when
# cylc is run out of a repository.  The version string is returned by
# 'git describe' and then "-dirty" is appended if the working directory
# is not clean.

LF='
'

# move to the repository
cd $( dirname "$0" )/..
VN=$(git describe --abbrev=4 --tags HEAD 2>/dev/null)
case "$VN" in
    *$LF*) (exit 1) ;;
    [0-9]*)
        # If uncommitted changes exist append "-dirty".

        if [[ -n "$(git status --untracked-files=no --porcelain)" ]]; then
            VN="$VN-dirty"
        fi
esac
# echo to stdout
echo "$VN"
