#!/usr/bin/perl
# dbipg_test_dicos_template
#    $Id: dbipg_test_template,v 1.1.1.1 2002/12/26 04:37:47 nakahira Exp $
#    Last updated: 12/22/2002
#
# Copyright (C) 2002 The Nagoya University Consumers' Co-operative Association
#   Written by K.Nakahira
#
#  This program is licensed under the GNU GPL.
#  See the following URL for more details:
#    http://www.gnu.org/licenses/gpl.txt
#

use strict;
use File::Basename;
use File::Copy;

use DBIPgSystem::DB;

my $VERSION = '1.1.00';

# 
my $init = { basename => basename($0),
		   };

sub usage {
  my $ver = "(version $VERSION)" if($VERSION);
  print <<USAGE;
$init->{basename} $ver
Usage: $init->{basename} basefile
ARGS:
  basefile    ե
USAGE
  exit;
}

{
  &usage if(@ARGV != 1 or $ARGV[0] =~ m/^--?h(elp)?$/i);
  my $basefile = shift;

  die "new: Cannot open $basefile'" unless(-f $basefile and -r $basefile);
  require $basefile;
  my $baseconf = &baseconf;

  $| = 1;
  print "*** Template files ***\n";

  my ($grpconf, $error, $warning, $msg, $dbi, $templates, $basedir, $files,
	  $file);
  foreach my $group (keys %{ $baseconf->{group} } ) {
	$grpconf = $baseconf->{group}{$group};
	($error, $warning) = (0, 0);
	($msg, $templates) = ([ ], { });
	$dbi = DBIPgSystem::DB->new
	  ($baseconf->{conffile}, $group, 'root', test => 1, no_db => 1);
	print "Group: $group\n";
	print "Testing template files ... ";
	# search, insert, detail Υƥץ졼Ȥ
	foreach my $key (values %{ $dbi->{template}{search} }) {
	  $templates->{$key} = 1;
	}
	foreach my $p (1 .. $dbi->{template}{numpage_insert}) {
	  $templates->{ $dbi->get_template_insert($p) } = 1;
	}
	foreach my $p (1 .. $dbi->{template}{numpage_detail}{$group}) {
	  $templates->{ $dbi->get_template_detail($p) } = 1;
	}
	if($dbi->{template}{numpage_insert} > 0) {
	  $templates->{ $dbi->{template}{insert_verify} } = 1;
	  $templates->{ $dbi->{template}{insert_success} } = 1;
	}
	if($dbi->{userinfo}{staff} and $dbi->{template}{remove}) {
	  $templates->{ $dbi->{template}{remove}{verify} } = 1;
	  $templates->{ $dbi->{template}{remove}{noverify} } = 1;
	}
	if($dbi->{userinfo}{staff} and $dbi->{global}{enable_file_insert}) {
	  $templates->{ $dbi->{template}{file_head} } = 1;
	  $templates->{ $dbi->{template}{file_verify} } = 1;
	  $templates->{ $dbi->{template}{file_success} } = 1;
	  $templates->{ $dbi->{template}{file_cancel} } = 1;
	}

	$files = [ ];
	$basedir = $dbi->{template}{decoded} ?
	  $grpconf->{template_fromdir} : $dbi->{template}{dir}{$group};
	opendir(DIR, "$dbi->{global}{basedir}/$basedir")
	  or die "Cannot open directory $dbi->{global}{basedir}/$basedir";
	while(defined($file = readdir(DIR))) {
	  next if($file !~ s/\.html$//);
	  next if($file =~ m/$baseconf->{group}{$group}{template_notest}/);
	  push(@$files, [ $file, $basedir ]);
	}
	closedir(DIR);
	# Ū˽񤭽ФΥƥץ졼ȥե
	foreach my $val (values %{ $grpconf->{template_static} }) {
	  push(@$files, [ $val, $grpconf->{template_static_dir} ]);
	}

	# ƥץ졼ȥեΥå
	open(NULL, "> /dev/null") or die;
	foreach my $f (@$files) {
	  eval { $dbi->print_template
			   ($f->[0], { }, fh => \*NULL, noexit => 1, dir => $f->[1],
				decode => 1) };
	  if($@) {
		push(@$msg, "Error: $@");
		$error++;
	  }
	  delete $templates->{$f->[0]};
	}
	close(NULL);
	if(keys %$templates) {
	  foreach my $key (sort keys %$templates) {
		push(@$msg, "Not found: $key\n");
	  }
	  $error += keys %$templates;
	}

	if($dbi->{template}{numpage_insert} > 1) {
	  my $cpage = $dbi->get_colpage();
	  foreach my $dbt (keys %{ $dbi->{dbseq} }) {
		foreach my $key (keys %{ $dbi->{dbseq}{$dbt} }) {
		  unless(exists $cpage->{$dbt}{$key}) {
			push(@$msg, "Warning: '\$cpage->{$dbt}{$key}' is not defined.\n");
			$warning++;
		  }
		}
	  }
	}
	print ($error ? "Failed.\n" : "Ok.\n");
	print @$msg;
	print "$error errors, $warning warnings.\n";
  }
}
exit;

__END__

=for html
<div class="header">
<div class="bar">
<a href="../index.html">Top</a>
<a href="index.html">ޥ˥奢</a>
</div>
<h1>dbipg_test_template</h1></div>

=for html
<div class="pod">

=head1 NAME

dbipg_test_template - ƥץ졼ȥեå

=head1 DESCRIPTION

  $ dbipg_test_template basefile

桼ˤĤơƥץ졼ȥեεҤ˴ְ㤤ʤå򤹤롣
ŪˤϡνԤ

  1. DBIPgSystem::DB::print_template ᥽åɤ̤ƥ顼
     λݤϤ롣
  2. DBIPgSystem::DB::get_colpage ᥽åɤƤӽФǡե
     ȥ᥽åɤ֤ͤȤӤ롣

=head1 SEE ALSO

F<DBIPgSystem>, F<DBIPgSystem::DB>

=head1 COPYRIGHT

Copyright (C) 2002 The Nagoya University Consumers' Co-operative Association

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the following URL for more details:
  http://www.gnu.org/licenses/gpl.txt

Written by Kenji Nakahira <nakahira@coop.nagoya-u.ac.jp>

=for html
</div>

=cut

