#!/usr/bin/perl
# dbipg_file_upload
#    $Id: dbipg_file_upload,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 Getopt::Long;

use DBIPgSystem::DB;

my $VERSION = '1.1.01';

# 
my $opt = { state => 'active',
			force => '',
		  };
my $optlist = [ 'help!', 'state=s', 'force!' ];
my $init = { basename => basename($0),
		   };

sub usage {
  my $ver = "(version $VERSION)" if($VERSION);
print <<USAGE;
$init->{basename} $ver
Usage: $init->{basename} [ץ] conffile dirname
ARGS:
  conffile     ǡե
  dirname      åץɤե뤬ǥ쥯ȥ
Options:
  --state      ǡξ ('active' or 'wait'; default: '$opt->{state}')
  --force      ΥǡͭǤʤƤ⽤ (default: '$opt->{force}')
USAGE
  exit;
}

{
  GetOptions($opt, @$optlist);
  &usage if(@ARGV != 2 or $opt->{help});

  # 
  my ($conffile, $dir) = @ARGV;
  my $dbi = DBIPgSystem::DB->new
	($conffile, 'staff', 'root', test => 1, test_db_commit => 1,
	admin => 1);

  die "Option state is invalid"
	unless($opt->{state} eq 'active' or $opt->{state} eq 'wait');

  $dir =~ s!/$!!;
  opendir(DIR, $dir) or die "Couldn't opendir $dir: $!";
  my $files = [ readdir(DIR) ];
  closedir(DIR);

  $ENV{SCRIPT_NAME} = $init->{basename};
  $ENV{REMOTE_ADDR} = '127.0.0.1';

  my $re_state = qr{2};

  my ($rid, $rid2, $dbt, $dbnum, $col, $data, $system, $presys, $cpage,
	  $errwarn, $qsdata, $err, $file, $exist, $fs);
 WHILE:
  while($file = shift(@$files)) {
	next unless($file =~ m/^0*(\d+)-/);
	$rid = $1;
	next if($exist->{$rid});
	$exist->{$rid} = 1;
	$qsdata = { };

	# ǡɤ߹
	eval {
	  ($data, $system, $presys) = $dbi->search_detail($rid, 1);
	  $cpage = $dbi->get_colpage();
	  $errwarn = $dbi->get_errwarn($data, $cpage);
	};
	if($@) {
	  print "Error ($rid): $@";
	  next WHILE;
	}
	unless(not $opt->{force} and
		   $presys->{system_state} eq $dbi->{db}{sysstate}{active}) {
	  print "Error ($rid): system_state is invalid: $presys->{system_state}\n";
	  next WHILE;
	}

	# åץ RID Ʊեϰ٤˽򤹤
	$fs = [ $file, grep /^0*$rid-/, @$files ];
	foreach my $f (@$fs) {
	  if($f =~ m/^0*$rid-(\w+)-([^0]\d*)-(\w+)(?:\.\w+)?$/) {
		($dbt, $dbnum, $col) = ($1, $2, $3);
		undef $dbt if($dbt eq $dbi->{global}{primary} and $dbnum != 1);
	  } elsif($f =~ m/0*$rid-(\w+)(?:\.\w+)?$/) {
		($dbt, $dbnum, $col) = ($dbi->{global}{primary}, 1, $1);
	  }
	  unless(defined $dbt) {
		print "Error ($rid): file is invalid: $f\n";
		next WHILE;
	  }
	  $qsdata->{$dbt}[$dbnum-1]{$col} = "$dir/$f";
	  $dbi->data_check
		($data, $qsdata, $errwarn, $system, { }, $cpage,
		 $cpage->{$dbt}{$col}, { }, islast => 1, isfilename => 1);
	  if(defined $errwarn->{error}{$dbt}[$dbnum-1]{$col}) {
		print "Error ($rid): $col - ",
		  $errwarn->{error}{$dbt}[$dbnum-1]{$col}[0]{msg}, "\n";
		next WHILE;
	  }
	}

	# եΥåץɤԤ
	$system->{system_state} = $dbi->{db}{sysstate}{ $opt->{state} };
	$err = $dbi->data_verify_check($data, $presys, $system);
	if($err) {
	  print "Error ($rid): ", $dbi->get_errmsg($err), "\n";
	  next WHILE;
	}

	# ǡ
	($rid2, $err) = $dbi->data_insert($data, $presys, $system, $errwarn);
	if($err) {
	  print "Error ($rid): ", $dbi->get_errmsg($err), "\n";
	  next WHILE;
	}

	print "Success ($rid -> $rid2): ", join(', ', @$fs), "\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_file_upload</h1></div>

=for html
<div class="pod">

=head1 NAME

dbipg_file_upload - ޥɥ饤󤫤ե򥢥åץɤ

=head1 DESCRIPTION

  $ dbipg_file_upload dir

ޥɥ饤󤫤ե򥢥åץɤơꤷRIDΥǡ롣

ǥ顼ʤǡΤߤ롣
顼äϡΥǡνǤ顼åϤ롣
Ʊǡ˥åץɤե뤬ʣϡ٤˽褦Ȥ롣
ξϡʣΥեΥåץɤƾ꤯ԤäȤΤ
ǡ롣

ǥեȤǤϡξ֤ͭǤʤǡϽʤ

åץ褬ơ֥ξ硢ե̾ (RID)-(̾)
Τ褦ˡϥեǷҤˤʤäƤɬפ롣
åץ褬Ϣơ֥ξϡե̾
ʥåץ褬դ˷褦ˡ
(RID)-(ơ֥̾)-(system_num)-(̾)
ηǤʤФʤʤ
ơ֥ǸԤΥեޥåȤˤ뤳ȤǤ뤬
ξ system_num  1 ǤʤФʤʤ
˳ĥҤդƤƤ褯ޤ RID  '0' դƤƤ褤
㤨С'1-column_a', '0035-column_b.bmp', '09-table_b-2-column_c.jpg'
ʤɤΥե̾ǧ롣

ǥեȤǤϡξ֤ͭǤ롣
̾Υƥư˽ͭ俽äǡ̵ˤʤ롣

åץɻ˹ԤϡCGI ե򥢥åץɤ
ƱǤ롣
ʥե륿Ф̤ꡢɬפ˱ƥͥ뤬졢
Υǡϥסߤ롣

ե򥢥åץɤƤ⡢ΥեѲʤ

=head1 SEE ALSO

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
