#!/usr/bin/env python2
"""A fast note-taking app for the UNIX terminal"""
import argparse
import ConfigParser
import os
import logging
import logging.handlers
import sys

import terminal_velocity.urwid_ui as urwid_ui


def main():

    # Parse the command-line option for the config file.
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("-c", "--config", dest="config", action="store",
            default="~/.tvrc",
            help="the config file to use (default: %(default)s)")
    args, remaining_argv = parser.parse_known_args()

    # Parse the config file.
    config_file = os.path.abspath(os.path.expanduser(args.config))
    config = ConfigParser.SafeConfigParser()
    config.read(config_file)
    defaults = dict(config.items('DEFAULT'))

    # Parse the rest of the command-line options.
    description = __doc__
    epilog = """
the config file can be used to override the defaults for the optional
arguments, example config file contents:

  [DEFAULT]
  editor = vim
  extension = txt
  notes_dir = ~/Notes

if there is no config file (or an argument is missing from the config file)
the default default will be used"""
    parser = argparse.ArgumentParser(description=description, epilog=epilog,
            parents=[parser],
            formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("-e", "--editor", dest="editor", action="store",
        default=defaults.get("editor", "$EDITOR"),
        help="the text editor to use (default: %(default)s)")
    parser.add_argument("-x", "--extension", dest="extension", action="store",
        default=defaults.get("extension", "txt"),
        help="the filename extension for new notes (default: %(default)s)")
    parser.add_argument("-d", "--debug", dest="debug", action="store_true",
        default=defaults.get("debug", False),
        help="debug logging on or off (default: off)")
    parser.add_argument("-l", "--log_file", dest="log_file", action="store",
        default=defaults.get("log_file", "~/.tvlog"),
        help="the file to log to (default: %(default)s)")
    parser.add_argument("notes_dir", action="store", nargs="?",
        default=defaults.get("notes_dir", "~/Notes"),
        help="the notes directory to use (default: %(default)s)")
    args = parser.parse_args()

    logger = logging.getLogger("terminal_velocity")
    # Send all messages to handlers, let them decide.
    logger.setLevel(logging.DEBUG)
    fh = logging.handlers.RotatingFileHandler(
                os.path.abspath(os.path.expanduser(args.log_file)),
                maxBytes=1000000,  # 1 megabyte.
                backupCount=0)
    if args.debug:
        fh.setLevel(logging.DEBUG)
    else:
        fh.setLevel(logging.WARNING)
    logger.addHandler(fh)
    sh = logging.StreamHandler(sys.stdout)
    sh.setLevel(logging.CRITICAL)
    logger.addHandler(sh)

    urwid_ui.launch(notes_dir=args.notes_dir, editor=args.editor,
            extension=args.extension)

if __name__ == "__main__":
    main()
