#!/usr/bin/python
import binascii
from getopt import getopt, GetoptError
import sys
from linotputilsee.etokenng import initetng 
from linotputilsee.etokenng import etngError 

'''
TODO:
    * we could write encrypted PSKC files
'''

def usage():
    print  '''
    Parameter:
    --pin=<user pin of the tokens>, if not set, a random user pin is user per token.
    --label=<label for the tokens>, if not set, the default label "eTokenNG" is used.
    --file=<filename of xml file>,  the filename of the xml file. 
    
    program can be called this way:
    python etng-enrollment.py --pin=secret --file=seeds.xml
    
'''

def main():
    try:
        opts, args = getopt(sys.argv[1:], "", 
                ["help", "pin=", "file=", "label="])
        
    except GetoptError:
        print "There is an error in your parameter syntax:"
        usage()
        sys.exit(1)
        
    
    filename = ""
    pin = "1234567890"
    label = "eTokenNG"
    
    for opt, arg in opts:
        if opt in ('--help'):
            usage()
            sys.exit(0)
        elif opt in ('--pin'):
            pin = arg
        elif opt in ('--file'):
            filename = arg
        elif opt in ('--label'):
            label = arg
            
            
    if ""==filename:
        print "missing parameter"
        usage()
        sys.exit(1)
    
    randomPin = False
    if ""==pin:
        randomPin= True
        
    token_dict = {}        
    
    while True:
        try:
            data = initetng( {'userpin':pin, 'label':label, 'randomUserPIN':randomPin, 'debug' : False })
            token_dict[data['serial']] = { 'otpkey' : data['hmac'] }
            
            print "Successfully initilized token %s" % data['serial']
        except etngError, e:
            print "Error: %s" % str(e)
            
            
        print "---------------------------------------"
        print "Insert next Token and hit enter."
        x = raw_input("If you are done, press 'D': ")
        
        if "D" == x.upper():
            break
            
    print "Now writing token file..."    
    
        
    f = open(filename, 'w')
    f.write("<Tokens>\n")
    for key in token_dict:
        f.write('''<Token serial="%s">
<CaseModel>eTokenNG OTP</CaseModel>
<Model>eTokenNG OTP</Model>
<ProductionDate>06/15/2011</ProductionDate>
<ProductName>eTokenNG OTP</ProductName>
<Applications>
<Application ConnectorID="a61c4073-2fc8-4170-99d1-9f5b70a2cec6">
<Seed>%s</Seed>
<MovingFactor>1</MovingFactor>
</Application>
</Applications>
</Token>''' % (key, token_dict[key]['otpkey']))
    
    f.write("</Tokens>\n")
    f.close()
    


if __name__ == '__main__':
    main()
