#! /usr/bin/perl
#
#	$Id: evlookup,v 1.5 1997/08/14 16:42:42 doi Exp $
#
#	evlookup - look up event information
#
#	this program will look up the information in the anime event
#	database, which is maintained by Hitoshi Doi <doi@usagi.org>,
#	and print out information for each voice actor.
#	it is mainly used for creating parts of the WWW seiyuu pages.
#
#	the seiyuu information is available via WWW at:
#		http://www.tcp.com/doi/seiyuu/seiyuu.html
#
#	original hack by Hitoshi Doi (doi@usagi.org), 1995.02.05
#
#	this program is public domain.  do whatever you want with it.
#	this program is offered "as is", and does not have any warrantee.
#
#	---------------------------------------------------------------
#	modifications
#
#	1995.06.30	Hitoshi Doi
#		use associative arrays to speed things up
#
#	1996.03.07	Hitoshi Doi
#		modifications to process non-event databases
#		the video DB and CD DB might also use this format
#	---------------------------------------------------------------

	$me = $0;
	$me =~ s/.*\///;

	# the file containing the voice actor information
	$evfile = $ENV{'EVENT'};

	$s_count = 0;
	@s_name = ();
	@s_url = ();
	@s_events = ();
	@s_date = ();
	$www_all = 0;
	@ev_lookup = ();
	$ev_ct = 0;
	$nodate = 0;
	$listall = '';
	$match = '';

	$WWW_subdir = 'EV';		# subdirectory for WWW data

	while ($ai = shift(@ARGV)) {
		if ($ai =~ m/^-db/i) {
			$evfile = shift(@ARGV);
		}
		elsif ($ai =~ m/^-sub/i) {
			$WWW_subdir = shift(@ARGV);
		}
		elsif ($ai =~ m/^-www/i) {
			$www_all = 1;
		}
		elsif ($ai =~ m/^-printall/i) {
			$printall = 1;
		}
		elsif ($ai =~ m/^-nodate/i) {
			$nodate = 1;
		}
		elsif ($ai =~ m/^-listall/i) {
			$listall = shift(@ARGV);
		}
		elsif ($ai =~ m/^-match/i) {
			$match = shift(@ARGV);
		}
		elsif ($ai =~ m/^-help/i) {
			print "$me - event information lookup

usage: $me [options]
options:  NAME         display all events for the actor NAME
         -www          create the WWW event data for all voice actors
         -sub SUB      set the WWW subdirectory
         -nodate       do not write out the date for the WWW data
         -listall PATH list all of the events with URLs, preceeded by PATH
         -db FILE      use the file FILE as the event database
";
			print "the event database is ";
			if ($evfile) {
				print "$evfile.\n";
			}
			else {
				print "currently undefined.
please set the environment variable EVENT,
or specify the database with the -db option.\n";
			}
			exit 0;
		}
		elsif ($ai =~ m/^-debug/i) {
			$debug = 1;
		}
		else {
			$ev_lookup[$ev_ct++] = $ai;
		}
	}

	if (!$evfile) {
		print "$me: the event database is undefined.
please set the environment variable EVENT,
or specify the database with the -db option.\n";
		exit -1;
	}
	$ev_db = "event database $evfile";
	if (!open(EV, "<$evfile")) {
		print "$me: can't open the $ev_db.\n";
		exit -1;
	}

	#
	# go through the event file and do processing
	#
	$read_done = 0;
	$ev_proc = 0;
	while (1) {
		if (!$read_done) {
			$l = <EV>;
			last if (!$l);	# end of file
			chop($l);
		}
		$read_done = 0;
		next if ($l =~ m/^$/);
		next if ($l =~ m/^#/);

		if ($l =~ m/^[1-9][0-9]/) {	# new event
			$event_OK = ($match) ? ($l =~ m/$match/i) : 1;
			($ev_date, $ev_name) = split(/[ ]+/, $l, 2);
			$ev_url = '';
			if ($debug) {
				print "event $ev_name, date $ev_date\n";
			}
			next;
		}
		$l =~ s/^[ ]+//i;

		#
		# process keywords
		#
		if ($l =~ m/^URL/i) {		# URL keyword
			$l =~ s/^URL[ ]*//i;
			$ev_url = $l;
			if ($debug) {
				print "event URL $ev_url\n";
			}
			if ($listall && $event_OK) {
				print "<LI>$ev_date ";
				print "<A HREF=\"$listall/$ev_url\">";
				print "$ev_name</A>\n";
			}
			next;
		}
		#
		# others are seiyuu names
		#
		if ($printall) {
			print "$ev_date - $l - ";
			if ($ev_url) {
				print "<A HREF=\"/~doi/$ev_url\">$ev_name</A>";
			}
			else {
				print "$ev_name";
			}
			print "\n";
			next;
		}
		$s_id = do set_seiyuu($l);
		if ($debug) {
			print "seiyuu ($s_id) $s_name[$s_id]\n";
		}
		$s_events[$s_id] .= "$ev_name\t";
		$s_url[$s_id] .= "$ev_url\t";
		$s_date[$s_id] .= "$ev_date\t";
	}

	#
	# print out information
	#
	if ($www_all) {
		do write_www();
	}
	if ($ev_ct) {
		for ($i = 0; $i < $ev_ct; $i++) {
			print "\n";
			$s_id = do find_seiyuu($ev_lookup[$i]);
			if ($s_id == $s_count - 1) {
				$s_count--;
				print "cannot find $ev_lookup[$i]\n";
				next;
			}
			print "$s_name[$s_id]\n";
			@e_name = split(/\t/, $s_events[$s_id]);
			@e_date = split(/\t/, $s_date[$s_id]);
			while ($en = shift(@e_name)) {
				$ed = shift(@e_date);
				print " $ed  $en\n";
			}
		}
	}
	exit 0;

#
# subroutines
#

sub set_seiyuu
{
	local($name) = $_[0];
	local($i);

	if (!($i = $seiyuu_id{$name})) {
		$s_count++;
		$seiyuu_id{$name} = $s_count;
		$i = $s_count;
		$s_name[$i - 1] = $name;
	}
	return($i - 1);
}

sub find_seiyuu
{
	local($name) = $_[0];
	local($k);

	foreach $k (sort keys(%seiyuu_id)) {
		if ($k =~ m/$name/i) {
			return($seiyuu_id{$k} - 1);
		}
	}
	return($s_count + 1);
}

#
# write data for WWW use
#
sub write_www
{
	local($i, @e_name, @e_url, @e_date);
	local($en, $eu, $ed);
	local($www_index) = "$WWW_subdir/Index";
	local($www_out);

	system "/bin/mv $WWW_subdir $WWW_subdir.old";
	mkdir($WWW_subdir, 0755);
	open(INDEX, ">$www_index");
	for ($i = 0; $i < $s_count; $i++) {
		printf INDEX "%05d:%s\n", $i, $s_name[$i];
		$www_out = sprintf("$WWW_subdir/%05d", $i);
		open(WWWOUT, ">$www_out");
		@e_name = split(/\t/, $s_events[$i]);
		@e_url = split(/\t/, $s_url[$i]);
		@e_date = split(/\t/, $s_date[$i]);
		while ($en = shift(@e_name)) {
			$ed = shift(@e_date);
			$eu = shift(@e_url);
			print WWWOUT "<TR valign=top><TD>";
			print WWWOUT "$ed</TD><TD>" if (!$nodate);
			if ($eu) {
				print WWWOUT "<A HREF=\"../$eu\">";
			}
			print WWWOUT "$en";
			if ($eu) {
				print WWWOUT "</A>";
			}
			print WWWOUT "</TD></TR>\n";
		}
		close(WWWOUT);
	}
	close(INDEX);
}

