#!/usr/bin/php
<?
/*                                                                                                          
 *  (C) Copyright 2006 Johan Verrept (jove@users.berlios.de)                                            
 *
 *  This file is subject to the terms and conditions of the GNU General
 *  Public License.  See the file COPYING in the main directory of this
 *  distribution for more details.     
 *  
 */

	if (isset ($argv[1])) {
		if (in_array ($argv[1], array ("-h", "--help"))) {
			printf( "Usage: " . $argv[0] . " [<file>]\n");
			printf(" <file> this is your verlihub dbconfig file.\n");
			printf("        defaults to /etc/verlihub/dbconfig\n");;
			exit;
		}
		$file = $argv[1];
	} else {
		$file = '/etc/verlihub/dbconfig';
	};

	$fp = fopen ($file, "r");
	while ($line = fgets ($fp)) {
		$fields = split (" *= *", $line);
		
		$$fields[0] = rtrim ($fields[1]);
	}
	fclose ($fp);

	printf ("T regs 28160 5\nT vips 290305 4\nT ops 2911759 3\nT cheefs 2911823 2\nT admins 2912223 1\nT owner 2150395871 0\n");

	$sql = mysql_connect ($db_host, $db_user, $db_pass);
	mysql_select_db ($db_data, $sql);
	
	$res = mysql_query ("select nick, class, pwd_crypt, login_pwd from reglist;");
	
	$classmap = array ( 10 => 0, 5 => 1, 4 => 2, 3 => 3, 2 => 4, 1 => 5 );
	$groupid = array();
	while ($array = mysql_fetch_array ($res)) {
		$group = $classmap [$array["class"]];
		switch ($array["pwd_crypt"]) {
			case 0:
				$pwd = crypt ($array["login_pwd"]);
				break;
			case 1:
				$pwd = $array["login_pwd"];
				break;
			default:
				printf ("Unsupported encryption: %d\n", $array["pwd_crypt"]);
				$pwd = "\1";
		}
		printf ("A %s %s 0 %lu %lu %s %lu %lu\n", $array["nick"], $pwd, $group, $groupid[$group]++, "verli_import", time(), 0);
	}

	mysql_close ($sql);
?>
