#!/usr/bin/python
# Copyright (c) 2010 OpenStack, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import gettext
from subprocess import call
from sys import argv, exit

import sqlite3


if __name__ == '__main__':
    gettext.install('swift', unicode=1)
    if len(argv) != 2:
        exit('Syntax: %s <path_to_auth.db>' % argv[0])
    _junk, auth_db = argv
    conn = sqlite3.connect(auth_db)
    try:
        listing = conn.execute('SELECT account, cfaccount, user, password, '
                               'admin, reseller_admin FROM account')
    except sqlite3.OperationalError, err:
        listing = conn.execute('SELECT account, cfaccount, user, password, '
                               '"f", "f" FROM account')
    for account, cfaccount, user, password, admin, reseller_admin in listing:
        cmd = ['swauth-add-user', '-K', '<your_swauth_key>', '-s',
               cfaccount.split('_', 1)[1]]
        if admin == 't':
            cmd.append('-a')
        if reseller_admin == 't':
            cmd.append('-r')
        cmd.extend([account, user, password])
        print ' '.join(cmd)
