#!/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 (($argc < 2)|| (in_array ($argv[1], array ("-h", "--help")))) {
		printf( "Usage: " . $argv[0] . " <pwfile> <groupfile>\n");
		printf(" To generate <pwfile> and <groupfile>, open userdb.mdb and save the UsrStatic table as tab delimited text to generate the pwfile and UsrClass to generate the groupfile. If this is chinese, readd the accounts manually :P.");
		exit;
	} else {
		$pwfile = $argv[1];
		$groupfile = $argv[2];
	};

	$fp = fopen ($pwfile, "r");
	while ($line = fgets ($fp)) {
		$fields = explode ("\t", $line);
		$users[trim ($fields[0], '"')]['passwd'] = trim($fields[1], '"');
	}

	fclose ($fp);

	$classmap = array ( 11 => 8, 10 => 0, 9=>1, 8 => 1, 7=>3, 6 => 3, 5 => 4, 4=>1, 3=>1, 2=>1, 1=>1);

	$fp = fopen ($groupfile, "r");
	while ($line = fgets ($fp)) {
		$fields = explode ("\t", $line);
		$class = trim( $fields[1], '"');
		if ($class <= 0) continue;
		$users[trim ($fields[0], '"')]['group'] = strval($classmap[intval($class)]);
	}
	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");
	$groupid = array();
	foreach ($users as $key => $data) {
		printf ("A %s %s 0 %lu %lu\n", $key, crypt($data['passwd']), $data['group'], $groupid[$data['group']]++);
	}
?>