#!/usr/bin/env python
# -*- Mode: Python; coding: iso-8859-1 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Copyright (C) 2005 Async Open Source <http://www.async.com.br>
## All rights reserved
##
## This program 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.
##
## 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 General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
## USA.
##
"""
sbin/init-database:

    Script to delete all the tables in an existent database or simple 
    create all the tables in an empty database. Note that you must have a 
    database already created to run this script properly.
"""

import getpass
import os
import sys

TEST_MODE = '-t' in sys.argv

from stoq.lib.runtime import set_test_mode, set_verbose, print_msg

# XXX This function must be called right before any other stoq domain 
# module since initialize_connection has been called after
# importing these modules
set_test_mode(TEST_MODE)

from stoq.examples.createall import create
from stoq.lib.admin import initialize_system, setup_tables

def main(args):
    LIST_TABLES = '-l' in sys.argv
    VERBOSE = LIST_TABLES or '-v' in sys.argv

    set_verbose(VERBOSE)
    setup_tables(delete_only=True, list_tables=LIST_TABLES,
                 verbose=TEST_MODE)
    if 'delete' in sys.argv:
        raise SystemExit

    name, username = "Superuser", "administrator"
    if not TEST_MODE:
        msg = "Enter administrator user password for database: "
        password = getpass.getpass(msg)
    else:
        password = ''

    initialize_system(user=name, username=username, password=password)

    if '-e' in args:
        create()

if __name__ == "__main__":
    sys.exit(main(sys.argv))
    
