#!/usr/local/bin/perl

# theme_add_comment_translated.pl
# Comments construction program in Japan theme pavilion
# to add translated comment to English comments file
#
# 96/03/08 by haruki@st.rim.or.jp
#
# THIS CODE USES THESE LIBRARIES
#
#   cgi-lib.pl  Copyright 1994 Steven E. Brenner
#
#   jcode.pl    Copyright (c) 1991,1992 Software Research Associates, Inc.
#               Original by srekcah@sra.co.jp, Feb 1992
#               Maintained by Kazumasa Utashiro <utashiro@sra.co.jp>
#

require 'cgi-lib.pl';
require 'jcode.pl';

&ReadParse(*in);		# initialize for cgi-lib

#
# いろいろな変数のセット
#
$date = `date`;
chop($date);
#
# コメントを追記するファイルの位置を得る
#
$ac_comment_file = "/stage/htdocs/Japan/Theme/logs/theme_ac_comments_test.html";
$ac_comment_url = "http://chat.expo96.ad.jp/Japan/Theme/sense";
$ac_tmp = "/stage/htdocs/Japan/Theme/logs/theme_ac_comments_test.html.new";

#
# HTMLヘッダ出力
#
#print "Content-type: text/html\n\n";
print "Location: http://chat.expo96.ad.jp/Japan/Theme/forStaff/\n\n";


#
# コメントが空なら終了
#
if (!$in{'theme_comment_translated'}) {
    &err("Sorry, your comment seems contains no data.\n");
}

#
# だれかが書き込んでいる間は待つ
# あまり待つなら終了
#
for($count = 0; -f "$ac_tmp"; $count++) {
    sleep(1);
    &err("Sorry, another person is writing now.") if ($count > 5);
}


#
# コメントの整形(改行改行を<p>に)
#
$in{'theme_comment_translated'} =~ s/\r//g;
$in{'theme_comment_translated'} =~ s/\n\n/<\/p><p>/g;
$in{'theme_comment_translated'} =~ s/\n//g;
$in{'theme_comment_translated'} =~ s/<p><\/p>//g;

#
# 追記ファイルに埋め込んで、tmpに書きだす
# 
open (TMP, ">$ac_tmp") || &err("Can not open temporary file");
open (FILE, "$ac_comment_file") || &err("Can not open comment file");
while(<FILE>) {
    if (/<!---INSERT HERE--->/) {
	 if ($in{'commenter'} eq "Readers' Comment") {
		print TMP "<!---INSERT HERE--->\n";
		print TMP "<!------------ Comments from user ---------->\n";
		print TMP "<table width=450>\n<tr><td width=33 valign=top>\n";
		print TMP "<img src=\"$ac_comment_url/image/zo1.gif\"";
		print TMP "width=32 height=23>\n</td>\n<td width=300>\n";
		if ($in{'theDate'}) {
		     print TMP "<font size=1><b>$in{'theDate'}<br>\n";
		} else {
		     print TMP "<font size=1><b>$date (Japan)<br>\n";
		}
		if ($in{'theName'}) {
		     print TMP "From: $in{'theName'}<br>";
		}
		print TMP "translated at: $date<br>";
		print TMP "</b></font><br>";
		$code = &jcode'convert($in{'theme_comment_translated'}, 'jis');
		print TMP "$in{'theme_comment_translated'}";
		print TMP "</p></td><td width=67></td></tr>\n</table>\n";
	} elsif ($in{'commenter'} eq "Staff Comment") {
		print TMP "<!---INSERT HERE--->\n";
		print TMP "<!------------ Comments from staff ---------->\n";
		print TMP "<table width=450>\n<tr><td width=107></td>\n";
		print TMP "<td width=280>\n";
		if ($in{'theDate'}) {
		     print TMP "<font size=1><b>$in{'theDate'}<br>\n";
		} else {
		     print TMP "<font size=1><b>$date (Japan)<br>\n";
		}
		print TMP "translated at: $date<br>";
		print TMP "From: \"sensorium\" staff</b></font><br><br>";
		$code = &jcode'convert($in{'theme_comment_translated'}, 'jis');
		print TMP "$in{'theme_comment_translated'}";
		print TMP "</p>";
		print TMP "<td width=33 valign=top><img src=\"$ac_comment_url/image/zo2.gif\"></td>\n";
		print TMP "</tr>\n</table>\n";

	}
     } else {			
	  print TMP $_;		
    }				
}
close(FILE);
close(TMP);

#
# tmp の内容をコメントのHTMLファイルへ上書き
#
open (TMP, "<$ac_tmp") || &err("Can not open tmp file");
open (FILE, ">$ac_comment_file") || &err("Can not open comment file");
while(<TMP>) {
    print FILE $_;
}
close(FILE);
close(TMP);


#
# 新しい内容を標準出力へ出力
#
#open (FILE, "<$ac_comment_file") || &err("Can not open comment file");
#while(<FILE>) {
#    &ac_kprint($_);
#}

#
# tmp ファイル削除
#
unlink "$ac_tmp";

#
# 終了
#
exit(0);

### sub routines ###
#
# JIS でprintする
#
sub ac_kprint {
    ($ac_print_line) = @_;
    $code = &jcode'convert(*ac_print_line, 'jis');	
    print "$ac_print_line";
}

#
# エラーの場合
#
sub err{
    print "<html><head>\n";
    print "<title>result</title>";
    print "</head><body bgcolor=\"#ffffff\">\n";
    local($msg) = @_;
    print "<p>$msg</p>\n";
    close FILE;
    close TMP;
    unlink "$ac_tmp";
    print "</BODY></HTML>\n";
    exit;
}
