# This file is part of gpgpass.
#
# simple command line password manager using gnupg
# Copyright (c) 2015, Markus Klein
# MIT Licensed

from gpgpass.password_manager import PasswordManager
from getpass import getpass
import argparse
import pyperclip
import sys


def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='''
    gpgpass is a password simple command line password manager.
    It encrypts the passwords using gpg and stores the passwords in a file.
    ''',
        epilog='''
    gpgpass is free software and licensed under the MIT license.
    Copyright (c) 2015, Markus Klein
    '''
    )

    parser.add_argument('id', help='fingerprint of your gpg key')

    aliases = {}
    subparsers = parser.add_subparsers(dest='command', title='commands', description='tells gpgpass what to do')
    if sys.version_info[0] > 2:
        aliases['aliases'] = 's'
    store_parser = subparsers.add_parser('store', help='stores a new password to the password safe', **aliases)
    store_parser.add_argument('name')
    store_parser.add_argument('-P', '--password')

    if sys.version_info[0] > 2:
        aliases['aliases'] = 'r'
    retrieve_parser = subparsers.add_parser('retrieve', help='retrieves a password', **aliases)
    retrieve_parser.add_argument('name')
    retrieve_parser.add_argument('-c', '--clipboard', action='store_true', help='copy the password to the clipboard instead of printing it')

    if sys.version_info[0] > 2:
        aliases['aliases'] = 'd'
    delete_parser = subparsers.add_parser('delete', help='deletes a password from the safe', **aliases)
    delete_parser.add_argument('name')

    if sys.version_info[0] > 2:
        aliases['aliases'] = 'l'
    list_parser = subparsers.add_parser('list', help='prints a list of all password names', **aliases)
    list_parser.add_argument('-P', '--with-passwords', help='show also the passwords (very dangerous!!)', action='store_true')

    password_group = parser.add_mutually_exclusive_group()
    password_group.add_argument('-p', help='interactively ask for the passphrase', action='store_true')
    password_group.add_argument('--passphrase', help='your key\'s passphrase')

    parser.add_argument('-H', '--homedir', help='path to your pgp home directory (the directory wich contains keyrings), default is "~/.gnupg"')
    parser.add_argument('-f', '--file', help='path to the password file, default is "~/.gpgpass"')
    args = parser.parse_args();

    while args.p and not args.passphrase:
        args.passphrase = getpass(prompt='Enter your key\'s passphrase: ')

    manager_args = {}
    if args.homedir:
        manager_args['homedir'] = args.homedir
    if args.file:
        manager_args['password_file'] = args.file

    manager = PasswordManager(args.id, args.passphrase, **manager_args)

    if args.command == 'store' or args.command == 's':
        if args.password is None:
            args.password = getpass(prompt='Enter the password: ')
        manager.store_password(args.name, args.password)
        manager.save()
    elif args.command == 'retrieve' or args.command == 'r':
        if args.clipboard:
            pyperclip.copy(manager.retrieve_password(args.name))
        else:
            print(manager.retrieve_password(args.name))
    elif args.command == 'delete' or args.command == 'd':
        manager.delete_password(args.name)
        manager.save()
    elif args.command == 'list' or args.command == 'l':
        if args.with_passwords:
            passwords = manager.retrieve_passwords()
            max_length = len(max(passwords, key=len))
            for name in passwords:
                print(('{0:' + str(max_length) + '}: {1}').format(name, passwords[name]))
        else:
            for name in manager.retrieve_passwords():
                print(name)

@@ -254,16 +254,31 @@
 ort sys%0A
+import os.path%0A
 %0A%0Adef ma
@@ -383,16 +383,51 @@
 matter,%0A
+        fromfile_prefix_chars='@',%0A
         
@@ -2525,16 +2525,175 @@
 gpass%22')
+%0A%0A    if os.path.isfile('~/.gpgpass_args'):%0A        fp = open('~/.gpgpass_args', 'r')%0A        file_arguments = fp.read()%0A    else :%0A        file_arguments = ''
 %0A    arg
@@ -2714,16 +2714,40 @@
 se_args(
+%5Bfile_arguments.split()%5D
 );%0A%0A    

read argument also from file ~/.gpgpass_args