#!/usr/local/bin/perl

# posts guest comment to web page

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

# grab values passed from form:
&ReadParse(*in);

print "Content-type: text/html\n";
print "Pragma: no-cache\n\n";

# print the top part of the response
print "<HTML><HEAD>\n";
print "<TITLE>Post Results</TITLE>\n";
print "</HEAD><BODY>\n";

# resolve return directory
$year_day = (localtime)[7];	# days of year
$first_day = 94;		# first day of seimei (April 4)
$moon_day = $year_day - $first_day + 1;	# day of sekki
@sekki = ('seimei','koku-u','rikka','shouman','boushu','geshi','shousho',
	  'taisho','risshu','shosho','hakuro','shubun','kanro','soukou',
	  'rittou','shousetsu','taisetsu','touji');
@dsekki = (16,15,16,15,16,16,15,16,16,15,16,15,15,15,15,15,14,10);
$csekki = 0;			# sekki counter
$cmoon_day = $moon_day;		# moon day counter
while ($cmoon_day > 0) {
    $cmoon_day = $cmoon_day - $dsekki[$csekki];
    $csekki = $csekki + 1;
}
$ddir = $sekki[$csekki - 1];
$month_html = join('',"SEKKI/",$ddir,"/renga_home.html");

# change to your favorite date format:
$date = `date`;
chop($date); # remove "\n"

# Grab the HTML file and make a file name for the temp file.
$file = "$ENV{'PATH_TRANSLATED'}browse_renga.html";
$tmp = "$ENV{'PATH_TRANSLATED'}browse_renga.html.tmp";
$tmp =~ s/\//@/g; # make a unique tmp file name from the path
$tmp = "/tmp/$tmp";
$file2 ="$ENV{'PATH_TRANSLATED'}browse_renga2.html"; 
$tmp2 = "$ENV{'PATH_TRANSLATED'}browse_renga2.html.tmp";
$tmp2 =~ s/\//@/g; # make a unique tmp2 file name from the path
$tmp2 = "/tmp/$tmp2";

# if any fields are blank, then skip the post and inform user:
if ( !$in{'name'} || !$in{'email'} || !$in{'renga'} ) {
  &err("句とお名前とE-mailアドレスにご記入漏れがありますと登録できません。<BR>もう一度お確かめの上、ご登録ください。");
}

# get an exclusive open on the tmp file, so
# two posts at the same time don't clobber each other.
for ($count = 0; -f "$tmp"; $count++) {
# oh no. someone else is trying to update the message file.  so we wait.
  sleep(1);
  &err("他の人が書き込み中です。しばらくお待ち下さい。(1)") if ($count > 4);  # but not for long
}
# open temp file
open(TMP,">$tmp") || &err("作業ファイルが開けません。(1)");
# open the HTML file
open(FILE,"<$file") || &err("$fileが開けません。(1)");
# an HTMLBBS file.  look through it for the HTML comments
# that denote stuff we want to change:

$guestbook = 0;
while(<FILE>) {
  if (/<!--GUESTBOOK-->/) {
    print TMP "<!--GUESTBOOK-->\n";
    $guestbook = 1;
  }
  elsif (/<!--POINTER-->/) {
    # add this post
    print TMP "<HR>\n";
    print TMP "<P>\n";
    print TMP "$in{'renga'}<BR>\n";
    if ($in{'disp'} =~ /on/ ) {
      print TMP "（$in{'name'}　$in{'email'}）\n";	#email display : on
    }
    else {
      print TMP "（$in{'name'}）\n";	#email display : off
    }
    print TMP "</P>\n";
    print TMP "<!--POINTER-->\n";
  }
  else { print TMP $_; }  # copy lines
}
if ($guestbook == 0) {
## print "$guestbook\n";
  &err("連歌ノートではありません。(1)"); 
} 

# move the new file over the old:
open(TMP,"<$tmp") || &err("作業ファイルが開けません。(1)");
# open the HTML file
open(FILE,">$file") || &err("$fileが開けません。(1)");
while(<TMP>) {
  print FILE $_;
}
close(FILE);
close(TMP);

# get an exclusive open on the tmp file, so
# two posts at the same time don't clobber each other.
for($count = 0; -f "$tmp2"; $count++) {
# oh no. someone else is trying to update the message file.  so we wait.
  sleep(1);
  &err("他の人が書き込み中です。しばらくお待ち下さい。(2)") if ($count > 4);  # but not for long
}
# open temp file
open(TMP2,">$tmp2") || &err("作業ファイルが開けません。(2)");
# open the HTML file
open(FILE2,"<$file2") || &err("$file2が開けません。(2)");
# an HTMLBBS file.  look through it for the HTML comments
# that denote stuff we want to change:

$guestbook = 0;
while(<FILE2>) {
  if (/<!--LASTDATE-->/)  { print TMP2 "<!--LASTDATE--> $date \n"; }
  elsif (/<!--GUESTBOOK-->/) {
    print TMP2 "<!--GUESTBOOK-->\n";
    $guestbook = 1;
  }
  elsif (/<!--POINTER-->/) {
    # add this post
    print TMP2 "<HR>\n";
    print TMP2 "<P>\n";
    print TMP2 "$in{'renga'}<BR>\n";
    print TMP2 "（$in{'name'}　$in{'email'}）<BR>\n";
    print TMP2 "［$in{'comment'}］\n";
    print TMP2 "</P>\n";
    print TMP2 "<!--POINTER-->\n";
  }
  else { print TMP2 $_; }  # copy lines
}
if ($guestbook == 0) {
  &err("連歌ノートではありません。(2)");
} 

# move the new file over the old:
open(TMP2,"<$tmp2") || &err("作業ファイルが開けません。(2)");
# open the HTML file
open(FILE2,">$file2") || &err("$file2が開けません。(2)");
while(<TMP2>) {
  print FILE2 $_;
}
close(FILE2);
close(TMP2);

unlink "$tmp";
unlink "$tmp2";

# print the rest of the response HTML
print "<P><H2>登録しました。</H2></P>\n";
print "<A HREF=\"$ENV{'PATH_INFO'}$month_html\" TARGET=\"_top\"><B>表示する</B></A>\n";
print "<P><FONT size=-1>表示されない場合は、NetscapeのReloadボタンをクリックして下さい。</FONT>\n";
print "</BODY></HTML>\n";
exit;

#
# if we got an error, print message, close & clean up
#
sub err {
  local($msg) = @_;
  print "<P>$msg</P>\n";
  close FILE;
  close FILE2;
  close TMP;
  close TMP2;
  unlink "$tmp";
  unlink "$tmp2";
  print "<A HREF=\"$ENV{'PATH_INFO'}reg_renga.html\"><B>戻る</B></A>\n";
  print "</BODY></HTML>\n";
  exit;
}