#!/usr/bin/perl

use Mysql;

$dbh=Mysql->Connect("localhost","forum","root","");

$sth=$dbh->Query("SELECT smilietext,smiliepath from smilie");
$i=0;
while (@arr = $sth->FetchRow) {
	@smilest[$i]=@arr[0];
	@smilesp[$i]=@arr[1];
	$i++;
	}
$scount=$i;

$sth=$dbh->Query("select forumid,title,threadcount,replycount from forum order by replycount desc;");
open(FILE,">index.shtm") or die("open error");
print FILE "<HTML>\n<TITLE>\n";
print FILE "<!--#include virtual=\"top.htm\" -->\n";
print FILE "<!--#include virtual=\"forum.htm\" -->\n";
while (@arr = $sth->FetchRow) {
	if ($class ne "t1") {$class="t1";} else {$class="t2";}
	print FILE "<tr class=$class><td width=100%><a href=forum_".@arr["0"].".shtm>".@arr["1"]."</a></td><td align=right>".@arr[2]."</td><td align=right>".@arr[3]."</td></tr>\n";

	$sth1=$dbh->Query("select threadid,title,postusername,lastposter,dateline,replycount from thread where forumid='".@arr["0"]."' order by dateline desc;");
	open(FILE1,">forum_".@arr["0"].".shtm") or die("open error");
	print @arr["1"]."\n";
	print FILE1 "<HTML>\n<TITLE>".@arr["1"]." / ";
	print FILE1 "<!--#include virtual=\"top.htm\" -->\n";
	print FILE1 "<!--#include virtual=\"thread.htm\" -->\n";
	while (@arr1 = $sth1->FetchRow) {
		$dname=substr(@arr1[0],-2);
		if ($class1 ne "t1") {$class1="t1";} else {$class1="t2";}
		($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime(@arr1[4]);
		print FILE1 "<tr class=$class1><td width=100% valign=top><a href=posts/$dname/thread_".@arr1["0"].".shtm>".@arr1["1"]."</a></td><td valign=top>".@arr1[2]."<br>".@arr1[3]."</td><td align=right valign=top nowrap>$hour:$min:$sec $mday/$mon/".(1900+$year)."</td><td align=right valign=top>".@arr1[5]."</td></tr>\n";

		$sth2=$dbh->Query("select u.username,t.title,t.dateline,t.pagetext,t.signature from post t,user u where u.userid=t.userid and t.threadid='".@arr1[0]."' order by dateline");
		print $arr1[0]."\n";
		
		if (! -d "posts/".$dname) {
			if (! -d "posts") {$a=`mkdir posts;touch index.htm`;}
			$a=`cd posts;mkdir $dname;touch index.htm;cd ..`;
			}
		open(FILE2,">posts/".$dname."/thread_".@arr1["0"].".shtm") or die("open error");
		print FILE2 "<HTML>\n<TITLE>".@arr1["1"]." / ".@arr["1"]." / ";
		print FILE2 "<!--#include virtual=\"../../top.htm\" -->\n";
		print FILE2 "<H1>".@arr1[1]." <a href=../../forum_".@arr[0].".shtm>(".@arr[1].")</a></H1>\n";
        print FILE2 "<!--#include virtual=\"../../post.htm\" -->\n";

		while (@arr2 = $sth2->FetchRow) {
			if ($class2 ne "t1") {$class2="t1";} else {$class2="t2";}
			($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime(@arr2[2]);
			$msg=@arr2[3];
			$msg=~s/\[br\]/<br>/gim;
			$msg=~s/\[b\]/<b>/gim;
			$msg=~s/\[\/b\]/<\/b>/gim;
			$msg=~s/\[u\]/<u>/gim;
			$msg=~s/\[\/u\]/<\/u>/gim;
			$msg=~s/\[i\]/<i>/gim;
			$msg=~s/\[\/i\]/<\/i>/gim;
			$msg=~s/\[QUOTE\]/<blockquote><hr size=1>/gim;
			$msg=~s/\[\/QUOTE\]/<hr size=1><\/blockquote>/gim;
			$msg=~s/\[img\]/<br><img src=/gim;
			$msg=~s/\[\/img\]/><br>/gim;
			$msg=~s/^(.*)\[url\]([^\]\[]+)\[\/url\](.*)$/$1<a href=\"$2\">$2<\/a>$3/gim;
			$msg=~s/\n/<br>\n/gi;
			
			for ($i=0;$i<$scount;$i++) {
				$text=@smilest[$i];
				$text=~s/\)/\\\)/gi;
				$text=~s/\(/\\\(/gi;
				$text=~s/\]/\\\]/gi;
				$text=~s/\[/\\\[/gi;
				$text=~s/\./\\\./gi;
				$text=~s/\:/\\\:/gi;
				$path=@smilesp[$i];
				$msg=~s/$text/<img src=\"http\:\/\/forum.novgorod.ru\/$path\"><\/a>/gim;
				}
			
			print FILE2 "<tr class=$class2><td width=100% valign=top><b>".@arr2[0]."</b><br><i>\n".@arr2[1]." ($hour:$min:$sec $mday/$mon/".(1900+$year).")</i><br>\n<br>\n".$msg."</td></tr>\n";
			}
			
		print FILE2 "</table>\n";
		print FILE2 "<!--#include virtual=\"../../bottom.htm\" -->\n";
		}
	print FILE1 "</table>\n";
	print FILE1 "<!--#include virtual=\"bottom.htm\" -->\n";
	}
print FILE "</table>\n";
print FILE "<!--#include virtual=\"bottom.htm\" -->\n";
close(FILE);
