~ubuntu-branches/ubuntu/raring/steam/raring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!@PIKE@

/* 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 = "@brand@";
string config_dir = "@configdir@";
string log_dir = "@logdir@";
string steam_dir = "@steamdir@";
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;
}