<?

$bncount = 99;
$ubbdir = "./";

## initialize w-agora stuff (database access, ...)
## -----------------------------------------------
if (empty($bn) ) {
	die ("bn parameter not set : usage $PHP_SELF?bn=forum_name");
}
include "./init.inc";

## loop over all threads in the directory
## --------------------------------------
for ($filecount=1; $filecount<=$bncount; $filecount++ ) {

	$filename = sprintf("%06d", $filecount);

	$file = "$ubbdir/$filename.cgi";
	if (!file_exists($file) ) {
		continue;
	}

	## Read the complete thread from UBB file
	## --------------------------------------

	echo "<p>read file : $filename" ;
	$fd = fopen ("$file", "r");

	$sz =filesize($file);
	$thread = 0;

	while ($line = fgets($fd, $sz)) {

		## explode each element into a fields array
		if (!$fields = split("\|\|",chop($line))) {
			echo "Can't explode line: $line<br>";
			break;
		} else {

			if ($fields[0] == "A") {
				// first "A" entry => it's a new topic, initialize topic subject then skip to next entry
				$topic = $fields[4];
			} else {
				## populate array w/ all fields
				## ----------------------------

				$note["username"] = $fields[2];
				// get date/time of post
				list ($m, $d, $y) = explode("-", $fields[3]);
				$hr = substr($fields[4], 0, 2);
				$min = substr($fields[4], 3, 2);
				$unixdate = mktime($hr, $min, $i, $m, $d, $y);

				$note["useraddress"] = $fields[5];
				$note["body"] = $fields[6];

				$note["unixdate"] = $unixdate;
				$note["userid"] = "guest";
				$note["password"] = md5(uniqid(""));
				$note["attachment"] = "none";
				$note["att_size"] = 0;
				$note["att_type"] = "unknown";

				// check if it is the first entry from the new topic
				if ($fields[1] == 0) {
					// 00000 = first "Z" entry => topic
					$note["subject"] = $topic;
					$thread = $unixdate;
					printf ("<br> insert %s - %s (%s)", $unixdate, $note["subject"], $note["username"]);
					$key = $db->InsertNote ($bn_db, $unixdate, $note);
				} else {
					$note["subject"] = "Re: $topic";
					printf ("<br> link %s to %s - %s (%s)", $unixdate, $thread, $note["subject"], $note["username"]);
					$key = $db->ThreadNote ($bn_db, $unixdate, $thread, $note);
				}
				flush ();
			}
		}
	}

	fclose($fd);
}

?>
