#!/usr/bin/pike7.6

/* 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 2006/03/27 12:40:05 exodusd Exp $
 */

constant cvs_version="$Id: setup.in,v 1.1.1.1 2006/03/27 12:40:05 exodusd Exp $";

string brand_name = "steam";
string config_dir = "/etc/steam";
string log_dir = "/var/log/steam";
string steam_dir = "/usr/share/steam";
string install_dir = "@install_dir@";

int replace_db_string_in_file ( string filename, string db_access_string )
{
    mixed err = catch {
        string config_text = Stdio.read_file( filename );
        if ( !stringp(config_text) ) return 0;
        array config_lines = config_text / "\n";
        for ( int i=0; i<sizeof(config_lines); i++ )
        {
            string line = config_lines[i];
            if ( stringp(line) && sizeof(line)>=9 && line[0..8]=="database=" )
            {
                config_lines[i] = "database=" + db_access_string;
                config_text = (config_lines * "\n") + "\n";
                Stdio.write_file( filename, config_text );
                return 1;
            }
        }
    };
    return 0;
}

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

    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: "+brand_name+")\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 created successfully.\n");
    write("User " + conf->user + " has all privileges to new DB.\n");
    string db_access_string = "mysql://" + conf->user + ":" + conf->password + "@localhost/" + conf->db;

    // MySQL 4.1 and newer use a new password authentication hash. If login
    // doesn't work, then try to use the old password format:
    if ( objectp(handle) ) handle = 0;  // disconnect
    if ( catch( handle = Sql.Sql( db_access_string ) ) ) {
        if ( catch( handle = Sql.Sql("mysql://root:"+conf->rootpw+"@localhost/mysql") ) == 0 ) {
            write("Setting old password format for database user '"
                + conf->user + "'.\n");
            handle->big_query("set password for " + conf->user +
                "@localhost = OLD_PASSWORD('" + conf->password + "');" );
        }
    }

    write("You acces string is: " + db_access_string + "\n");

    replace_db_string_in_file( config_dir + "/steam.cnf", db_access_string );
    replace_db_string_in_file( config_dir + "/config.template", db_access_string );

    return 0;
}
