#!/usr/bin/env python
#
# pyful - Python File Manager
# This file is the execute file of pyful.
#
# Copyright (C) 2010-2011 anmitsu <anmitsu.s@gmail.com>
#
# 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 2 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.

import imp
import locale
import optparse
import os
import shutil
import sys

def add_pyful_path():
    libdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    if os.path.exists(os.path.join(libdir, "pyful")):
        sys.path.insert(0, libdir)
    sys.argv[0] = os.path.abspath(__file__)

def set_encoding():
    locale.setlocale(locale.LC_ALL, "")
    if sys.version_info < (3, 0):
        imp.reload(sys)
        sys.setdefaultencoding("utf-8")

def create_config():
    confdir = os.path.join(os.getenv("HOME"), ".pyful")
    if not os.path.exists(confdir):
        os.makedirs(confdir, 0o700)
    rcfile = os.path.join(confdir, "rc.py")
    if not os.path.exists(rcfile):
        from pyful import __file__ as __initfile__
        default = os.path.join(os.path.dirname(__initfile__), "rc.py")
        shutil.copy(default, rcfile)
        print("Success.\n")
        print("Configuration file was created to `{0}'.".format(rcfile))
        print("From this, the configuration file is read preferentially.")
    else:
        print("Failure.\n")
        print("Configuration file (rc.py) already exists `{0}'.".format(rcfile))

def option_parse():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage)
    parser.add_option(
        "-v", "--version",
        action="store_true",
        dest="version",
        help="display program version")
    parser.add_option(
        "-c", "--config",
        dest="config",
        help="load configuration file")
    parser.add_option(
        "-C", "--create-config",
        action="store_true",
        dest="create_config",
        help="create configuration file")
    parser.add_option(
        "-e",
        action="store_true",
        dest="enter",
        help="wait of the user input")
    return parser.parse_args()[0]

def main(options):
    if options.version:
        from pyful import __version__
        print("pyful v{0}".format(__version__))
        sys.exit(0)
    elif options.enter:
        from pyful import util
        util.wait_restore()
        sys.exit(0)
    elif options.config:
        if not os.path.exists(options.config):
            print("No such rcfile `{0}'".format(options.config))
            sys.exit(1)
    elif options.create_config:
        create_config()
        sys.exit(1)

    from pyful import Pyful
    from pyful import widget
    try:
        widget.start_curses()
        pyful = Pyful()
        pyful.setup(options.config)
        pyful.run()
    finally:
        widget.end_curses()


if __name__ == "__main__":
    add_pyful_path()
    set_encoding()
    main(option_parse())
