#!/usr/bin/perl -w

use strict;
use DBI;

sub do_error($) {
    exit(1);
}

if(@ARGV != 3) {
  print("Usage: $0 <user> <password> <database>\n");
  exit(1);
}

my ($user, $pass, $dbname) = @ARGV;

my $db = DBI->connect("dbi:mysql:$dbname", "$user", "$pass");

if (!defined($db)) {
	print "did not connect to mysql\n";
	exit(1);
}

my $res; 
my $sth;
my $rows;

$res = $db->do("set character_set_client=utf8;");
if(!$res) {
  do_error(1);
}
$res = $db->do("set character_set_connection=utf8;");
if(!$res) {
  do_error(1);
}
$res = $db->do("set character_set_results=utf8;");

if(!$res) {
  do_error(1);
}

print "Creating new tables.\n";

$res = $db->do("
CREATE TABLE `users_new` (
`id` int(11) NOT NULL auto_increment,
`isgroup` tinyint(4) NOT NULL default '0',
`nonactive` int(1) NOT NULL default '0',
`externid` varbinary(255),
PRIMARY KEY  (`id`)
) TYPE=MyISAM;"
);

$res = $db->do("
CREATE TABLE object
(
  `id` int(11) NOT NULL auto_increment,
  `objecttype` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`, `objecttype`)
) TYPE=MyISAM;"
);

$res = $db->do("
CREATE TABLE objectproperty
(
  `objectid` int(11) NOT NULL default '0',
  `propname` varchar(255) binary NOT NULL,
  `value` text,
  PRIMARY KEY (`objectid`, `propname`)
) TYPE=MyISAM;"
);

$res = $db->do("
CREATE TABLE objectrelation
(
  `objectid` int(11) NOT NULL default '0',
  `parentobjectid` int(11) NOT NULL default '0',
  PRIMARY KEY  (`objectid`, `parentobjectid`)
) TYPE=MyISAM;
");

print "Converting users and groups..\n";

$sth = $db->prepare("SELECT id, name, password, fullname, isgroup, nonactive, emailaddress, isadmin FROM users");
if(!$sth) {
  do_error(1);
}

$sth->execute();

my @row;
my $n = 1;

while(@row = $sth->fetchrow_array()) {
  # Add an object for each user/group
  $res = $db->do("INSERT INTO users_new (id, isgroup, nonactive, externid) VALUES ($row[0], $row[4], $row[5], $row[0]);");
  
  if($row[4]) {
      # Add external group object
      $res = $db->do("INSERT INTO object (id, objecttype) VALUES($row[0], 2);");
      # Add external group properties
      $res = $db->do("INSERT INTO objectproperty (objectid, propname, value) VALUES ($row[0], 'groupname', '$row[1]');");       
  } else {
      # Add external user object
      $res = $db->do("INSERT INTO object (id, objecttype) VALUES($row[0], 1);");
      # Add external group properties
      $res = $db->do("INSERT INTO objectproperty (objectid, propname, value) VALUES ($row[0], 'loginname', '$row[1]');");       
      # Add external group properties
      $res = $db->do("INSERT INTO objectproperty (objectid, propname, value) VALUES ($row[0], 'password', '$row[2]');");       
      # Add external group properties
      $res = $db->do("INSERT INTO objectproperty (objectid, propname, value) VALUES ($row[0], 'fullname', '$row[3]');");       
      # Add external group properties
      $res = $db->do("INSERT INTO objectproperty (objectid, propname, value) VALUES ($row[0], 'emailaddress', '$row[6]');");       
      # Add external group properties
      $res = $db->do("INSERT INTO objectproperty (objectid, propname, value) VALUES ($row[0], 'isadmin', '$row[7]');");       
  }
} 

$res = $db->do("DROP TABLE users");
$res = $db->do("ALTER TABLE users_new RENAME users");

print "Converting group memberships..\n";

$res = $db->do("INSERT INTO objectrelation (objectid, parentobjectid) SELECT id, parent_id FROM usergroup_acl where parent_id!=1 and parent_id!=id;");

print "done.\n";
