#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  privacyIDEA is a fork of LinOTP
#  May 08, 2014 Cornelius Kölbel
#  contact:  http://www.privacyidea.org

#
#  Copyright (C) 2010 - 2014 LSE Leading Security Experts GmbH
#  License:  GPLv2
#  contact:  http://www.linotp.org
#            http://www.lsexperts.de
#            linotp@lsexperts.de
"""
  Description:  This tool generates passwords for the sql resolver

  Dependencies: ./.


"""

import sys
from getopt import getopt, GetoptError
import getpass
import crypt
import random

__version__ = '0.1'


def usage():
    print('''
    Parameter:
    -u username 
    -p password
    ''')


def main():

    user = ""
    password = ""

    try:
        opts, args = getopt(sys.argv[1:], "u:p:h", ["help"])

    except GetoptError:
        print("There is an error in your parameter syntax:")
        usage()
        sys.exit(1)

    for opt, arg in opts:
        if opt in ('-u',):
            user = arg
        elif opt in ('-p',):
            password = arg
        elif opt in ('-h', '--help'):
            usage()
            sys.exit(1)

    if password == "":
        password = getpass.getpass("Please enter a password: ")

    # the salt are two characters from: [./a-zA-Z0-9]
    pool = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    salt = pool[random.randrange(0, len(pool))] + pool[random.randrange(0, len(pool))]
    encryptedPW = crypt.crypt(password, salt)

    print("|%s|%s|%s|" % (user, encryptedPW, salt))


if __name__ == '__main__':
    main()
