#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       kazam
#
#       Copyright 2012 David Klasinc <bigwhale@lubica.net>
#       Copyright 2010 Andrew <andrew@karmic-desktop>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

from argparse import ArgumentParser
import logging
import os
import sys

from gi.repository import Gtk

if __name__ == "__main__":

    logger =  logging.getLogger()
    logger.name = "Kazam"
    handler = logging.StreamHandler()
    formatter = logging.Formatter('%(levelname)s %(name)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)

    logger.info("Logger intialized.")

    if os.path.exists("../data"):
        logger.info("Running locally, AppIndicator icons might be missing.")
        datadir = "../data"
        sys.path.insert(0, "..")
    else:
        # A bit more flexible setting of datadir, it works
        # when base install path is not /usr
        curpath = os.path.abspath(__file__)
        curpath = os.path.realpath(curpath)
        datadir = curpath.split('bin/')[0] + "share/kazam/"

    try:
        import platform
        dist = platform.linux_distribution()
        logger.info("Running on: {0} {1}".format(dist[0], dist[1]))
    except:
        # Fallback to the almighty Ubuntu 12.10 ;)
        dist = ('Ubuntu', '12.10', 'quantal')
        logger.warning("Failed to correctly detect operating system.")

    from kazam.version import *
    version = "%(prog)s {0} '{1}'".format(VERSION, CODENAME)
    parser = ArgumentParser(description = "Screen recording program.")

    parser.add_argument("-d", "--debug",
                        action = "store_true",
                        help = "enable debug mode",
                        default = False)

    parser.add_argument("-v", "--version",
                        action = "version",
                        version = version)

    parser.add_argument("-t", "--test",
                        action = "store_true",
                        help = "generate test video signal",
                        default = False)

    parser.add_argument("-n", "--nosound",
                        action = "store_true",
                        help = "disable PulseAudio",
                        default = False)

    parser.add_argument("-s", "--silent",
                        action = "store_true",
                        help = "silent start",
                        default = False)

    parser.add_argument("-f", "--fullscreen",
                        action = "store_true",
                        help = "instant screenshot of full screen",
                        default = False)

    parser.add_argument("-a", "--area",
                        action = "store_true",
                        help = "instant screenshot of preselected screen area",
                        default = False)

    parser.add_argument("-w", "--window",
                        action = "store_true",
                        help = "instant screenshot of active window",
                        default = False)

    parser.add_argument("-p", "--preferences",
                        action = "store_true",
                        help = "show preferences window",
                        default = False)

    parser.add_argument("-g", "--godmode",
                        action = "store_true",
                        help = "god mode of capture",
                        default = False)

    args = parser.parse_args()
    if args.debug:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    logger.debug("Starting ...")

    if args.fullscreen:
        from kazam.instant import InstantApp
        app = InstantApp(datadir, dist, args.debug, 1) # MODE_ALL
    elif args.area:
        from kazam.instant import InstantApp
        app = InstantApp(datadir, dist, args.debug, 2) # MODE_AREA
    elif args.window:
        from kazam.instant import InstantApp
        app = InstantApp(datadir, dist, args.debug, 4) # MODE_ACTIVE
    elif args.godmode:
        from kazam.instant import InstantApp
        app = InstantApp(datadir, dist, args.debug, 666) # MODE_ACTIVE
    elif args.preferences:
        from kazam.instant import InstantApp
        app = InstantApp(datadir, dist, args.debug, 0, preferences=True)
    else:
        from kazam.app import KazamApp
        appWindow = KazamApp(datadir, dist, args.debug, args.test, args.nosound, args.silent)

    Gtk.main()
    logger.debug("Finishing ...")
