#!/usr/bin/pike7.4

/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
 *
 *  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
 * 
 * $Id: setup.in,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
 */

constant cvs_version="$Id: setup.in,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";

int main(int argc, array argv)
{
    Sql.Sql handle;
    mapping conf = ([ 
	"rootpw": "", 
	"password": "steam", 
	"user":"steam", 
	"db":"steam",]);

    for ( int i = 1; i < argc; i++ ) {
      string val;
      
      if ( sscanf(argv[i], "--newroot=%s", val) == 1 ) {
	Process.system("mysqladmin -u root password " + val);
      }
      else if ( sscanf(argv[i], "--password=%s", val) == 1 )
	conf["password"] = val;
      else if ( sscanf(argv[i], "--user=%s", val) == 1 )
	conf["user"] = val;
      else if ( sscanf(argv[i], "--rootpw=%s", val) == 1 )
	conf["rootpw"] = val;
      else if ( sscanf(argv[i], "--db=%s", val) == 1 )
	conf["db"] = val;
      else if ( argv[i] == "--drop" )
        conf->drop = "true";
      else if ( argv[i] == "--help" ) {
	write("sTeam Setup utility creates an empty database. Options: \n"+
	      " --db= Choose the name for the new database (default: steam)\n"+
	      " --rootpw= You MySQL root password\n"+
	      " --user= The mysql user for the database (default: steam)\n"+
	      " --password= The password for the user (default: steam)\n"+
              " --drop  Drops an old database with name specified by --db\n"+
	      " The utility returns the database access string for steam.cnf\n");
	return 0;
      }
      
    }  
    if ( catch(handle = Sql.Sql("mysql://root:"+conf->rootpw+"@localhost/mysql")) )
    {
	werror("Failed to connect to database:\n"+
		"1) Make sure mysql is running.\n"+
	        "2) Is a root pw for mysql set? Use --rootpw=pw to login.\n");
	return 1;
    }
    array tables = handle->list_dbs();
    if ( search(tables, conf->db) >= 0 ) {
	write("The database "+ conf->db + " allready exists !\n");
	if ( conf->drop ) {
	    handle->big_query("drop database " + conf->db);
	    write("Dropped database - creating new...\n");
        }
	else
	    return 1;
    }
    handle->big_query("create database " + conf->db);
    handle->big_query("use mysql");
    handle->big_query("grant all privileges on " + conf->db + ".* to "+
		      conf->user + " @localhost identified by '" + conf->password+
		      "' with grant option;");
    write("Database "+ conf->db + " has been create successfully.\n");
    write("User " + conf->user + " has all privileges to new DB.\n");
    write("You acces string is: mysql://" + conf->user + ":"+conf->password+"@localhost/"+conf->db+"\n");
    return 0;
}
