#!/usr/bin/env python
# -*- coding: ascii -*-

###########################################################################
# clive, video extraction utility
# Copyright (C) 2007-2008 Toni Gundogdu
#
# This file is part of clive.
#
# clive 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.
#
# clive 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 clive.  If not, see <http://www.gnu.org/licenses/>.
###########################################################################

import os
import sys

from clive.opts import Options
from clive.passwd import Passwd
from clive.path import ConfigDir

## clive-passwd utility

class clivePasswd:
    def _usage(self):
        print 'Usage: clive-passwd [option] [command] [entry]'
        print 'Options:'
        print '  -h     print this help'
        print '  -v     print version and exit'
        print 'Commands:'
        print '  add [ENTRY]        add new entry'
        print '  delete [ENTRY]     delete entry'
        print '  get [ENTRY]        decrypt entry password to stdout'
        print '  list               list existing entries'
        print '  purge              purge ~/.clive/passwd file'
        raise SystemExit

    def main(self):
        self._args = sys.argv[1:]
        if len(self._args) == 0:
            self._usage()
        d = {'-v':self._version, '-h':self._usage,
            'add':self._add, 'delete':self._delete,
            'get':self._get, 'list':self._list,
            'purge':self._purge}
        try:
            d[self._args[0]]()
        except KeyError:
            self._usage()

    def _add(self):
        if len(self._args) < 2:
            raise SystemExit('error: specify entry name')
        Passwd().add_entry(self._args[1])

    def _delete(self):
        if len(self._args) < 2:
            raise SystemExit('error: specify entry name')
        Passwd().delete_entry(self._args[1])

    def _get(self):
        if len(self._args) < 2:
            raise SystemExit('error: specify entry name')
        (usern,passw) = Passwd().get_entry(self._args[1])
        print 'username:%s\tpassword:%s' % (usern,passw)

    def _list(self):
        a = Passwd().entries()
        if len(a) == 0:
            raise SystemExit('error: no entries found')
        for e in a:
            usern,passw = e[1].split(':',1)
            print 'entry:%s  username:%s' % (e[0].ljust(10),usern)

    def _purge(self):
        fn = ConfigDir().passwdfile()
        if os.path.exists(fn):
            a = raw_input('> Confirm purge? (y/N):')
            if a.lower() == 'y':
                os.remove(fn)
        else:
            raise SystemExit('error: nothing to purge')

    def _version(self):
        print ''.join(Options()._ver) \
            .replace('clive','clive-passwd').rstrip('\n')

if __name__ == '__main__':
    c = clivePasswd()
    try:
        c.main()
    except KeyboardInterrupt:
        print
