#!/usr/bin/env python3
# Usage:
#   ./bin/version
#       Prints the current version

import os
import re
import subprocess
import sys
from distutils.version import LooseVersion

ENV_LINODE_CLI_VERSION = "LINODE_CLI_VERSION"


def get_version_env():
    return os.getenv(ENV_LINODE_CLI_VERSION)


def get_version(ref="HEAD"):
    # We want to override the version if an environment variable is specified.
    # This is useful for certain release and testing pipelines.
    describe = get_version_env()

    if describe is None:
        describe = call('git describe {} --tags'.format(ref))

    # Strip the `v` prefix if specified
    if describe.startswith("v"):
        describe = describe[1:]

    parts = LooseVersion(describe).version[:3]
    return tuple(parts)


def call(cmd):
    _, output = subprocess.getstatusoutput(cmd)
    return output


major, minor, patch = get_version()
print("{}.{}.{}".format(major, minor, patch))
