#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright 2015, Fabrice Laporte
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Uses the `keroaek` program to add tag fields to lyrics
"""

import io
import argparse
import sys

from keroaek.editor import Editor


def main(argv=None):
    if argv is None:
        argv = sys.argv
    parser = argparse.ArgumentParser(
        description='Lrc files creator'
        'See https://github.com/Kraymer/keroaek for more infos.')
    parser.add_argument('audio', metavar='MP3_FILE',
                        help='Music file', default='')
    parser.add_argument('-l', '--lyrics', dest='src',
                        help='src lyrics'
                        'DEFAULT: consider audio embedded lyrics', default='')
    parser.add_argument('-o', '--output', dest='dest',
                        help='output filename. '
                        'DEFAULT: edit input file in-place', default='')

    args = vars(parser.parse_args())
    if not args['dest']:
        args['dest'] = args['src']

    if not args['src']:
        pass
    else:
        with io.open(args['src'], 'r', encoding='utf-8') as f:
            lyrics = f.read()
    lrc = Editor().sync(args['audio'], lyrics)
    with io.open(args['dest'], 'w', encoding='utf-8') as f:
        f.write(lrc)


if __name__ == "__main__":
    sys.exit(main())
