#!/usr/bin/python
# -*- encoding: utf8 -*-

#
# #############################################################################
# ## Alladdin, the all-add-in web server                                     ##
# #############################################################################
#
# @author Laurent GAERTNER <garthh@bagsbug.net>
# @version $Revision$
# @modifiedby $Author$
# @lastmodified $Date$
#
# #############################################################################
# ## randomstring: generate a random string                                  ##
# #############################################################################
#
# LICENSE:
# Alladdin 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 2 of the License, or
# (at your option) any later version.
#
# Alladdin 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 this program.  If not, see <http://www.gnu.org/licenses/>.

# TODO: ajouter un filtre pour complexRS

from optparse import OptionParser
import sys
import random

# Script options

options = {}
options[('-l', '--length')] = { 'type': 'int',
                                'dest': 'lengthRS',
                                'default': 8,
                                'help': '''String lenght\n
                                           [default: %default]''' }
options[('-c', '--complexity')] = { 'type': 'string',
                                    'dest': 'complexRS',
                                    'default': 'dlus',
                                    'help': '''Random string complexity\n
                                             d: decimal,\n
                                             h: hexadecimal\n
                                             u: uppercase letters\n
                                             l: lowercade letters\n
                                             s: special chars (eg. #,!...)\n
                                             [default: %default]''' }
options[('-p', '--prefix')] = { 'type': 'string',
                               'dest': 'prefix',
                               'help': 'String prefix'}
options[('-s', '--suffix')] = { 'type': 'string',
                               'dest': 'suffix',
                               'help': 'String suffix'}

def randomString(lengthRS,complexRS):
    
    # Initialize values
    chain = ""
    chars = list()
    complexRS = complexRS.lower()
    
    # Define chars list
    # h: HEXADECIMAL -> add from A to F
    if "h" in list(complexRS):
        chars = chars + range(65,71)
        complexRS = complexRS+"d"
    
    # d: DECIMAL -> add from 0 to 9
    if "d" in list(complexRS):
        chars = chars + range(48,58)
    
    # u: UPPERCASE -> add from A to Z
    if "u" in list(complexRS):
        if "h" in list(complexRS):
            chars = chars + range(71, 91)
        else:
            chars = chars + range(65,91)
    
    # l: LOWERCASE -> add from a to z
    if "l" in list(complexRS):
        chars = chars + range(97,123)
    
    # s: SPECIAL CHARS -> adds !,#,$,&,+,-,.,=,@,_
    if "s" in list(complexRS):
        chars = chars + [33, 35, 36, 38, 43, 45, 46, 61, 64, 95]
    
    for genchar in range (0,lengthRS):
        chain = chain + chr(random.choice(chars))
    
    return chain

def main(options, arguments):
    complexRS = options.complexRS
    lengthRS = options.lengthRS
    genchain = ""
    
    if options.prefix:
        genchain = genchain + options.prefix
    
    genchain = genchain + randomString(lengthRS, complexRS)
    
    if options.suffix:
        genchain = genchain + options.suffix
        
    print genchain

if __name__ == '__main__':
    parser = OptionParser()
    for param, option in options.items():
        parser.add_option(*param, **option)
    options, arguments = parser.parse_args()
    main(options, arguments)