#!/usr/bin/php5
<?php
########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn 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 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# MuGLIn 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 MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

include("/usr/local/lib/muglin/php/functions.php");
get_conf("/etc/muglin/base.conf",&$sql);
if($argv[1]=="--verbose"||$argv[1]=="-v") $VERB=true;
if(!($CON=mysql_connect("$sql[MYS_HOST]","$sql[MYS_USER]","$sql[MYS_PASS]")))
{
	if($VERB) die ("Error at creating connection (".mysql_error().")");
	else die("toller Returncode");
}
if(!mysql_query("CREATE DATABASE ".$sql[MYS_DB]))
{
	mysql_close($CON);
	if($VERB) die("Error creating database (".mysql_error().")");
	else die("toller Returncode");
}
if(!mysql_select_db($sql[MYS_DB],$CON))
{
	mysql_close($CON);
	if($VERB) die("Error changing database (".mysql_error().")");
	else die("toller Returncode");
}
if(!mysql_query("CREATE TABLE Clients (CID int not null primary key auto_increment, MAC varchar(17) not null, Hostname varchar(50), SID int, IP varchar(15), Status int, Unattend varchar(150));",$CON))
{
	mysql_close($CON);
	if($VERB) die("Error creating Clients table (".mysql_error().")");
	else die("toller Returncode");
}
if(!mysql_query("CREATE TABLE Config (Var varchar(25) not null primary key, Value varchar(250))",$CON))
{
	mysql_close($CON);
	if($VERB) die("Error creating Config table (".mysql_error().")");
	else die("toller Returncode");
}
if(!mysql_query("CREATE TABLE Images (IID int not null primary key auto_increment, Name varchar(100) not null, Comment varchar(250), Architectur varchar(3) not null, Enabled bool not null, Type int not null, Path varchar(150) not null)",$CON))
{
	mysql_close($CON);
	if($VERB) die("Error creating Images table (".mysql_error().")");
	else die("toller Returncode");
}
if(!mysql_query("CREATE TABLE Sessions (SID int not null primary key auto_increment, IID int not null, State int, Name varchar(50), Baseport int not null)",$CON))
{
	mysql_close($CON);
	if($VERB) die("Error creating Sessions table (".mysql_error().")");
	else die("toller Returncode");
}
mysql_close($CON);
echo "Gratulations! Your database had been correctly set up";
