#! /usr/bin/perl

# As Command Line:
# csv2html.cgi file=hanshin.csv page=0 > hanshin.html
#
# http://honstname/csv2html.cgi?file=hanshin.csv&s=0&e=10

# No,店名,地域コード,駅コード,住所,電話番号,営業時間,概要,アクセス,その他
#
# option:
#  file=Csv File Name
#  s=Start No.
#  e=End No.
#  page=Page No.
#  name=
#  station=
#  update=

$ItemsPerPage = 10;

%file_areaname_table = (
  "rh" => "阪神",
  "ramenha.csv" => "阪神",
  "rh.txt" => "阪神",
  "ramenko" => "神戸",
  "ramenko.csv" => "神戸",
  "rk.txt" => "神戸",
  "ramenetc" => "その他",
  "ramenetc.csv" => "その他",
  "rf.txt" => "その他",
  );

#### Main routin start ####################################

if($#ARGV == -1) {
  $flag_CGI = 1;
  &parse_cgi();
} else {
  &parse_option();
}
&initalize();

open(INPUT, $FORM{'file'});
$_ = <INPUT>; # if first line is header then comment out this.

&print_header();
$counter = 0;
while(<INPUT>) {
  &parse_line($_);
  if(&isHit()) {
    $counter++;
    if($counter > $start) {
      &print_item();
      last;
    }
  }
}
while(<INPUT>) {
  &parse_line($_);
  if(&isHit()) {
    $counter++;
    if(($end > 0) && ($counter > $end)) {
      last;
    }
    &print_item();
  }
}
while(<INPUT>) {
  &parse_line($_);
  if(&isHit()) {
    $counter++;
  }
}
if($counter == 0) {
  print "検索結果: なし<br>\n";
}
&print_footer();
close(INPUT);
exit;

#### Main routin end ####################################

sub initalize {
  $start = 0;
  $end = 0;
  if($FORM{'page'} ne "") {
    $start = $FORM{'page'}*$ItemsPerPage;
    $end = $start + $ItemsPerPage;
  } else {
    if($FORM{'s'} ne "") {
      $start = $FORM{'s'};
    }
    if($FORM{'e'} ne "") {
      $end = $FORM{'e'};
    }
  }
}

sub isHit {
  local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
  local($month, $station_name);
  local($flag_Hit);

  $flag_Hit = 1;
  if($FORM{'name'} ne "") {
    if($name !~ /$FORM{'name'}/) {
      $flag_Hit = 0;
    }
  }
  if($flag_Hit && ($FORM{'macg'} ne "")) {
    if($station !~ /$FORM{'macg'}/) {
      $flag_Hit = 0;
    }
  }
  if($flag_Hit && ($FORM{'ccode'} ne "")) {
    if($ccode !~ /$FORM{'ccode'}/) {
      $flag_Hit = 0;
    }
  }
  if($flag_Hit && ($FORM{'station'} ne "")) {
    if(! &isHitStation($FORM{'station'})) {
      $flag_Hit = 0;
    }
  }

  if($flag_Hit && ($FORM{'update'} ne "")) {
    if($lastdate eq "") {
      $flag_Hit = 0;
    } else {
      $update_width = $FORM{'update'};
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
      $month = $year*12 + $mon + 1;
      if($lastdate =~ m|([0-9]+)/([0-9]+)/([0-9]+)|) {
        if(($1-1900)*12 + $2 < $month - $update_width) {
          $flag_Hit = 0;
        }
      }
    }
  }
  return $flag_Hit;
}

sub isHitStation {
  local($keyword) = @_;
  local($key, $value, $hd);

  if($station =~ /$keyword/) {
    return 1;
  }
  if($access =~ /$keyword/) {
    return 1;
  }
  while(($key, $value) = each(%station_table)) {
    if($key eq $keyword) {
      if($station =~ /$value/) {
        return 1;
      }
      if($access =~ /$value/) {
        return 1;
      }
      return 0;
    }
  }
  return 0;
}

sub parse_line {
  local($line) = @_;
  local(@ff);

  @ff = split(/[,\r\n]/, $line);
  $number = $ff[0];
  $name = $ff[1];
  $ccode = $ff[2];
  $station = $ff[3];
  $address = $ff[4];
  $tel = $ff[5];
  $acttime = $ff[6];
  $comment = $ff[7];
  $access = $ff[8];
  $person = $ff[9];
}

sub print_item {
        print "<li><a href=hyogoetcsearch.cgi?s1=$number>$name</a>\n";
}

sub print_header {
  local($title_area, $title_no);
  local($key, $value);

  $title_area = "その他";
  while(($key, $value) = each(%file_areaname_table)) {
    if($key eq $FORM{'file'}) {
      $title_area = $value;
      last;
    }
  }
  if($FORM{'page'} eq "") {
    $title_no = "";
  } else {
    $title_no = "（その".($FORM{'page'}+1)."）";
  }

  print "<HTML>\n";
  print "<HEAD>\n";
  print "<TITLE>兵庫のラーメン−".$title_area.$title_no."</TITLE>\n";
  print "</HEAD>\n";
  print "<BODY>\n";
  print "検索結果<hr>\n";
  print "<ul>\n";
}

sub print_footer {
  local($from, $query);

  print "</ul><p>\n";
  if(($end > 0) && ($counter > $end)) {
    $from = $start + 1;
    print "$counter件中 $from-$end件<br>\n";
    if($FORM{'page'} ne "") {
      $query = "file=$FORM{'file'}&page=".($FORM{'page'}+1);
    } else {
      $query = "file=$FORM{'file'}&s=$end&e=".($end+$ItemsPerPage);
    }
    if($FORM{'name'} ne "") {
      $query .= "&name=$FORM{'name'}";
    }
    if($FORM{'mgcg'} ne "") {
      $query .= "&mgcg=$FORM{'mgcg'}";
    }
    if($FORM{'ccode'} ne "") {
      $query .= "&ccode=$FORM{'ccode'}";
    }
    if($FORM{'station'} ne "") {
      $query .= "&station=$FORM{'station'}";
    }
    if($FORM{'update'} ne "") {
      $query .= "&update=$FORM{'update'}";
    }
    print "<a href=\"rf.cgi?$query\">次へ</a>\n";
  }
  print "<hr>\n";
  print "<a href=\"http://kamo.pncn.net/i/ramen/index.html\">トップへ戻る</a>\n";
  print "</BODY>\n";
  print "</HTML>\n";
}

sub parse_option {
  local($i);

  for($i=0; $i<=$#ARGV; $i++) {
    if($ARGV[$i] =~ /(.+)=(.+)/) {
      $FORM{$1} = $2;
    }
  }
}

sub parse_cgi {
  local(@options, $opt, $buffer, $key, $value);

  print "Content-type: text/html\n\n";
  if($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  } else {
    $buffer =$ENV{'QUERY_STRING'};
  }
  @options = split(/&/, $buffer);
  foreach $opt (@options) {
    ($key, $value) = split(/=/, $opt);
    $value =~ tr/+/ /;
    $value =~ s/\%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#    $value =~ s/</&lt;/g;
#    $value =~ s/>/&gt;/g;
#    $value =~ s/\"/&quot;/g;
#    $value =~ s/\n//g;
#    $value =~ s/\,//g;

    #&jcode'convert(*value,'euc');
    $FORM{$key} = $value;
  }
}
