#!/usr/bin/python
"""

Referencing current branch in github README.md [1]
This pre-commit hook [2] updates the README.md file's
Travis badge with the current branch. Based on [4].

[1] http://stackoverflow.com/questions/18673694/referencing-current-branch-in-github-readme-md
[2] http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
[3] https://docs.travis-ci.com/user/status-images/
[4] https://gist.github.com/dandye/dfe0870a6a1151c89ed9

Copy this file to .git/hooks/

"""

import os
import sys
import re
import subprocess

BASE = os.path.dirname(os.path.realpath(__file__)) + "/../../"
README = BASE + "README.md"
remote = subprocess.check_output(["git", "remote", "-v"]).strip().split('\n')[0]
parts = re.split('[/\.: ]', remote)
REPO = parts[ len(parts) - 3]
USER = parts[ len(parts) - 4]
BRANCH = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip()

def getVersion():
    file_name = BASE + "code/espurna/config/version.h"
    lines = open(file_name).readlines()
    for line in lines:
        if "APP_VERSION" in line:
            parts = line.split('"')
            return parts[1]
    return "unknown"
VERSION = getVersion()

version = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n".format(
    VERSION = VERSION
)

branch = "![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)\n".format(
    BRANCH = BRANCH
)

travis = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \
    "(https://travis-ci.org/{USER}/{REPO})\n".format(
    USER = USER,
    REPO = REPO,
    BRANCH = BRANCH
)

codacy = "[![codacy](https://img.shields.io/codacy/grade/{HASH}/{BRANCH}.svg)]" \
    "(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n".format(
    HASH = "c9496e25cf07434cba786b462cb15f49",
    USER = USER,
    REPO = REPO,
    BRANCH = BRANCH
)

lines = open(README).readlines()
with open(README, "w") as fh:
    for line in lines:
        if "![travis]" in line:
            fh.write(travis)
        elif "![version]" in line:
            fh.write(version)
        elif "![branch]" in line:
            fh.write(branch)
        elif "![codacy]" in line:
            fh.write(codacy)
        else:
            fh.write(line)

subprocess.check_output(["git", "add", README ])