# Neutrino Framework
#
# Copyright (c) 2016-2016, Christiaan Frans Rademan
# All rights reserved.
#
# LICENSE: (BSD3-Clause)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENTSHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import os
import subprocess

from setuptools import setup

NULL = open(os.devnull, 'w')

# allow update_version.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

# Versioning
# Also, when git is not available (PyPi package), use stored version.py.
# Version format A.B.CX0
# A = Major Release
# B = Minor Release - New features
# C = Fixes
# X Options are:
#   a = Alpha Release
#   b = Beta Release
#   rc = Release Candidate
# The number followed by X is the Version of X
# Note excluding X its final release
# When commiting and not adding new tag it becomes:
#   A.B.C.X0-dev0
# The number followed by dev is the number of commits.
# GET Version from GIT TAGS
version_py = os.path.join(os.path.dirname(__file__), 'nfw/version.py')
version_msg = "# Do not edit this file," +\
              "pipeline versioning is governed by git tags"
try:
    version_git = subprocess.check_output(["git",
                                           "describe",
                                           "--tags"],
                                          stderr=NULL).rstrip()
    with open(version_py, 'w') as fh:
        if len(version_git.split('-')) == 1:
            version_git = version_git.split('-')[0]
        elif len(version_git.split('-')) > 1:
            version = version_git.split('-')[0]
            commit = version_git.split('-')[1]
            commit = int(commit) + 1
            version_git = "%s.dev%s" % (version, commit,)
        fh.write(version_msg +
                 os.linesep +
                 "__version__='%s'" % (version_git,)
                 )
except:
    if os.path.isfile(version_py):
        with open(version_py, 'r') as fh:
            version_git = open(version_py).read()
            version_git = version_git.strip()
            version_git = version_git.split('=')[-1]
            version_git = version_git.replace('\'', '')
    else:
        with open(version_py, 'w') as fh:
            fh.write(version_msg +
                     os.linesep +
                     "__version__='0.0.0'"
                     )
            version_git = '0.0.0'

print version_git
