def main():
    #Getting here implies all pre-requisites have been met (NOT FINISHED!)
    accountName = options.a
    ipAddress = options.i
    port = int(options.p)

    request = {}
    response = {}
    request["accountName"] = accountName
    request["pin"] = getCardPin()
    response["account"] = accountName
    if options.n:
        request["action"] = "new"
        request["amount"] = float(options.n)
        response["initial_balance"] = float(options.n)
    elif options.d:
        request["action"] = "deposit"
        request["amount"] = float(options.d)
        response["deposit"] = float(options.d)
    elif options.w:
        request["action"] = "withdraw"
        request["amount"] = float(options.w)
        response["withdraw"] = float(options.w)
    elif options.g:
        request["action"] = "get"

    request = {"request" : request}
    # print >> sys.stderr, request
    sys.stdout.flush()
    message = NetMsg(request)
    encodedMessage = message.encryptedJson(getAuthKey(), message.getJson())

    # Create socket and send data to bank
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect((ipAddress, port))
        sock.sendall(encodedMessage)

        sock.settimeout(10.0)
        received = sock.recv(1024)
        sock.settimeout(None)
        received = json.loads(NetMsg.decryptJson(getAuthKey(), received))
        if received["msg"] == "transaction_completed":
            if options.g:
                response["balance"] = received["balance"]
            print json.dumps(response)
            sys.stdout.flush()
        elif received["msg"] == "transaction_failed":
            sys.exit(255)
        elif received["msg"] == "protocol_error":
            sys.exit(63)
    except socket.error, e:
        print >> sys.stderr, e
        sys.stdout.flush()
        sys.exit(63)
def generate_auth_file(auth_file):
    global auth_key
    if not os.path.isfile(auth_file):
        f = open(auth_file, "wb")
        json_out = {}
        json_out["SecretKey"] = NetMsg.generateKey()
        auth_key = json_out["SecretKey"]
        f.write(json.dumps(json_out))
        f.close()
        # print "created"
    else:
        exit("Authenication file already exists\n")
try:
    if indexData[options.a] == options.c and options.n: #Trying to make a new account when it already exists
        sys.exit(255)
    elif indexData[options.a] != options.c: #The card file you passed does not match the file we expect
        sys.exit(255)
except KeyError:
    for entry in indexData:
        if indexData[entry] == options.c: #If card file is associated with another account
            sys.exit(255)

cardFilePath = os.path.join("./CardFiles", options.c)
try:
    if not os.path.exists(cardFilePath) and options.n:
        cardFile = open(cardFilePath, "w")
        cardPin = NetMsg.generateKey()
        cardFile.write(cardPin)
        cardFile.close()
        
        indexData[options.a] = options.c
        indexFile = open("./CardFiles/index", "w")
        indexFile.write(json.dumps(indexData))
        indexFile.close()
except IOError:
    sys.exit(255)


def getAuthKey():
    key = {}
    numLines = 0
    for line in authFile:
 def send_ack(self, ack):
     ack = NetMsg.encryptedJson(auth_key, ack)
     self.request.sendall(ack)
    def parse_request(self, data):
        try:
            # decrypt message
            net_msg = json.loads(NetMsg.decryptJson(auth_key, data))
            msg = net_msg["msg"]
            response = {}
            response["msgID"] = net_msg["msgID"] + 1
            response["code"] = 0

            # perform atm request
            request = msg["request"]
            if request["action"] == "new":
                if request["amount"] < 10:
                    self.print_to_stderr("Amount was less than 10\n")
                    response["msg"] = "transaction_failed"
                    self.send_ack(json.dumps(response))
                    # self.send_ack("transaction_failed")
                    return

                if self.get_acct_w_acct_name_n_card_key(request["accountName"],
                                                        request["pin"]):
                    self.print_to_stderr("Account already exists\n")
                    response["msg"] = "transaction_failed"
                    # self.send_ack("transaction_failed")
                    return

                new_account = Account(request["pin"],
                                      request["accountName"], request["amount"])
                self.accounts.append(new_account)
                response["msg"] = "transaction_completed"
                self.send_ack(json.dumps(response))
                # self.send_ack("transaction_completed")
            elif request["action"] == "deposit":
                if request["amount"] <= 0:
                    self.print_to_stderr("Amount was less than or equal to 0\n")
                    response["msg"] = "transaction_failed"
                    self.send_ack(json.dumps(response))
                    # self.send_ack("transaction_failed")
                    return

                account = self.get_acct_w_acct_name_n_card_key(request[
                                                               "accountName"],
                                                               request["pin"])

                if account is None:
                    self.print_to_stderr("Could not verify user account\n")
                    response["msg"] = "transaction_failed"
                    self.send_ack(json.dumps(response))
                    # self.send_ack("transaction_failed")
                    return
                else:
                    account.deposit(request["amount"])
                    response["msg"] = "transaction_completed"
                    response["balance"] = account.get_balance()
                    self.send_ack(json.dumps(response))
                    # self.send_ack("transaction_completed")
            elif request["action"] == "withdraw":
                # account = self.get_acct_w_acct_name_n_card_key(request["pin"])
                account = self.get_acct_w_acct_name_n_card_key(request[
                                                               "accountName"],
                                                               request["pin"])
                if account is None:
                    self.print_to_stderr("Could not verify user account\n")
                    response["msg"] = "transaction_failed"
                    self.send_ack(json.dumps(response))
                    # self.send_ack("transaction_failed")
                    return
                else:
                    if not account.withdraw(request["amount"]):
                        response["msg"] = "transaction_failed"
                        response["code"] = 419
                        self.send_ack(json.dumps(response))
                        # self.request.sendall("419")
                    else:
                        # self.send_ack("transaction_completed")
                        response["msg"] = "transaction_completed"
                        response["balance"] = account.get_balance()
                        self.send_ack(json.dumps(response))
            elif request["action"] == "get":
                account = self.get_acct_w_acct_name_n_card_key(request[
                                                               "accountName"],
                                                               request["pin"])

                if account is None:
                    self.print_to_stderr("Could not verify user account\n")
                    return
                else:
                    account.current_balance()
                    response["msg"] = "transaction_completed"
                    response["balance"] = account.get_balance()
                    self.send_ack(json.dumps(response))
                    # self.send_ack(str(account.get_balance()))
        except ValueError:
            # self.request.sendall("protocol_error")
            response["msg"] = "protocol_error"
            self.send_ack(json.dumps(response))
            print "protocol_error"