#!/usr/bin/env python

import os
import argparse
import subprocess
import datetime
from subprocess import call, STDOUT

unreleased_path='changelogs/unreleased'
changelog_filepath='CHANGELOG.md'

parser = argparse.ArgumentParser(prog='changelog', description='Changelog administration tool.')
parser.add_argument('-c', '--create',
                    metavar='message',
                    type=str,
                    help='creates an entry with this actual message to be added to the changelog')

parser.add_argument('-r', '--release',
                    metavar='version',
                    type=str,
                    help='releases the changelog by gathering all entries')

parser.add_argument('-f', '--filename',
                    type=str,
                    help='the filename to put the message in if you don\'t want to use the branch name')

parser.add_argument('-t', '--commit',
                    action='store_true',
                    help='commit the created/deleted/modified files by this command')


args = parser.parse_args()

if args.create == args.release:
    print('Need one of --create or --release option')
    parser.print_help()
    exit(1)

git = True
if call(["git", "branch"], stderr=STDOUT, stdout=open(os.devnull, 'w')) != 0:
    git = False

if args.create:
    filename = args.filename
    if filename == None:
        if git:
            head = subprocess.check_output(
                ["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode('utf-8')
            filename = head.split('/').pop().rstrip()
        else:
            print('Need a filename in non git repositories: \'-f\' or \'--filename\'')
            parser.print_help()
            exit(1)

    filename += '.md'

    if not os.path.isdir('changelogs/unreleased'):
        os.makedirs('changelogs/unreleased')
    
    changelog_entry_filepath = 'changelogs/unreleased/%s' % filename

    f = open(changelog_entry_filepath,'w')
    f.write(args.create)
    f.close()

    if git:
        subprocess.check_output(['git', 'add', changelog_entry_filepath])

    if git and args.commit:
        subprocess.check_output(['git', 'commit', '-m', 'Created changelog entry', changelog_entry_filepath])

    print('Changelog entry added to \'%s\'' % changelog_entry_filepath)

if args.release:
    if not os.path.isdir('changelogs/unreleased'):
        os.makedirs('changelogs/unreleased')

    changelog = '## %s _%s_\n' % (args.release, datetime.datetime.today().strftime('%Y/%m/%d'))

    for root, dirs, files in os.walk('changelogs/unreleased'):
        for filename in files:
            f = open('changelogs/unreleased/%s' % filename,'r')
            changelog += '* %s\n' % f.read()
            f.close()

    changelog += '\n\n'

    if os.path.exists(changelog_filepath):
        f = open(changelog_filepath, 'r')
        changelog += f.read()
        f.close()
        
    f = open(changelog_filepath, 'w')
    f.write(changelog)
    f.close()

    if git:
        subprocess.check_output(['git', 'add', changelog_filepath])

    for root, dirs, files in os.walk('changelogs/unreleased'):
        for filename in files:
            if git:
                subprocess.check_output(['git', 'rm', '-f', 'changelogs/unreleased/%s' % filename])
            else:
                os.remove('changelogs/unreleased/%s' % filename)

    if git and args.commit:
        subprocess.check_output(['git', 'commit', '-m', 'Created release entry for %s' % args.release, changelog_filepath])
        
    print('Changelog entry for release \'%s\' added to changelog file \'%s\'' % (args.release, changelog_filepath))


