#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    LinOTP - the open source solution for two factor authentication
#    Copyright (C) 2010 - 2014 LSE Leading Security Experts GmbH
#
#    This file is part of LinOTP server.
#
#    This program is free software: you can redistribute it and/or
#    modify it under the terms of the GNU Affero General Public
#    License, version 3, as published by the Free Software Foundation.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the
#               GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#    E-mail: linotp@lsexperts.de
#    Contact: www.linotp.org
#    Support: www.lsexperts.de
#
"""This tool generates lines for a PasswdIdResolver user file """

VERSION = '0.1'
import os, sys
from getopt import getopt, GetoptError
import getpass
import crypt
import pprint
import random



def usage():
    print  '''
    Parameter:
    -u username
    -i uid (numerical)
    -p password
    -d description
    '''


def main():

    user = ""
    password = ""
    uid = ""
    description = ""

    try:
        opts, args = getopt(sys.argv[1:], "u:i:p:hd:", ["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 ('-i'):
            uid = arg
        elif opt in ('-p'):
            password = arg
        elif opt in ('-d'):
            description = 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)

    gid = uid
    home = ""
    shell = ""
    print("%s:%s:%s:%s:%s:%s:%s" % (user,
                encryptedPW,
                uid,
                gid,
                description,
                home,
                shell))

if __name__ == '__main__':
    main()
