#!/usr/bin/env python
# ============================================================================
# This file is part of Pwman3.
#
# Pwman3 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2
# as published by the Free Software Foundation;
#
# Pwman3 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 Pwman3; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# ============================================================================
# Copyright (C) 2012-2014 Oz Nahum Tiram <nahumoz@gmail.com>
# ============================================================================
# Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
# ============================================================================
from __future__ import print_function
import sys
from pwman import get_conf_options, get_db_version
from pwman import parser_options
from pwman.ui import get_ui_platform
from pwman.ui.tools import CLICallback
import pwman.data.factory
from pwman.exchange.importer import Importer
from pwman.util.crypto_engine import CryptoEngine

if sys.version_info.major > 2:
    raw_input = input


def main(args):
    PwmanCli, OSX = get_ui_platform(sys.platform)
    xselpath, dbtype, config = get_conf_options(args, OSX)
    dbver = get_db_version(config, dbtype, args)
    CryptoEngine.get(dbver)

    fname = config.get_value('Database', 'filename')
    db = pwman.data.factory.create(dbtype, dbver, fname)

    if args.import_file:
        importer = Importer((args, config, db))
        importer.run()
        sys.exit(0)

    cli = PwmanCli(db, xselpath, CLICallback, config)

    try:
        cli.cmdloop()
    except KeyboardInterrupt as e:
        print(e)


if __name__ == '__main__':
    args = parser_options().parse_args()
    main(args)