#!/usr/bin/perl
# dbipg_remove_files
#    $Id: dbipg_remove_files,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 DBIPgSystem::DB;

my $VERSION = '0.1.00';

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

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

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

  my $dbi = DBIPgSystem::DB->new
	($conffile, 'staff', 'root', test => 1, no_db => 1);

  my @dir;
  push(@dir, $dbi->{global}{filetype}{dir}, $dbi->{global}{filetype}{tmpdir},
	   $dbi->{global}{filetype}{pooldir})
	if(defined $dbi->{global}{filetype});
  push(@dir, "$dbi->{global}{basedir}/$dbi->{global}{tmpdir}");

  my (@files, $exp_file, $ans);
  # export_file  .html ե
  foreach my $group (keys %{ $dbi->{global}{export_file} }) {
	($exp_file = $dbi->{global}{export_file}{$group}) =~ s!/[^/]*$!!;
	opendir(DIR, $exp_file) or die "Cannot open dir $exp_file: $!";
	@files = grep -f, map { "$exp_file/$_" } grep /\.html$/, readdir DIR;
	closedir(DIR);
	print "$exp_file/*.html : ", scalar(@files), " files\n";
	if(@files) {
	  $ans = &inputstr
		("Would you like to remove files in $exp_file/*.html (yes/no)? ",
		 undef, pattern => qr{^(y|yes|n|no)$}i);
	  unless($ans =~ m/^(y|yes)$/i) { print "Aborted.\n"; next; }
	  foreach (@files) {
		print "Remove: $_\n";
		unlink($_);
	  }
	}
  }

  # @dir ե
  foreach my $dir (@dir) {
	opendir(DIR, $dir) or die;
	@files = grep -f, map { "$dir/$_" } grep !/^\.\.?$/, readdir DIR;
	closedir(DIR);
	print "$dir/* : ", scalar(@files), " files\n";
	next unless @files;
	$ans = &inputstr
	  ("Would you like to remove files in $dir (yes/no)? ",
	   undef, pattern => qr{^(y|yes|n|no)$}i);
	unless($ans =~ m/^(y|yes)$/i) { print "Aborted.\n"; next; }
	foreach (@files) {
	  print "Remove: $_\n";
	  unlink($_);
	}
  }
}
exit;

sub inputstr {
  my ($msg, $default, %opt) = @_;

  my $in;
  while(1) {
	print $msg;
	print "[$default] " if(defined $default);
	$in = <STDIN>;
	chomp($in);
	return $default if($in eq '' and defined($default));
	return $in if(not defined($opt{pattern}) or $in =~ m/$opt{pattern}/);
	print STDERR "Unrecognized string: $in\n\n";
  }
}

__END__

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

=for html
<div class="pod">

=head1 NAME

dbipg_remove_files - ƥݥե䡢ݡȤե롢
åץɤ줿ե

=head1 DESCRIPTION

  $ dbipg_remove_files conffile

ǡƾõˡɬפˤʤե롣
ƥǥ쥯ȥˤĤơե䤤碌Ԥ
ŪˤϡΥե :

  * $dbi->{global}{export_file}{*}  .html ե
  * $dbi->{global}{filetype}{dir} ե
  * $dbi->{global}{filetype}{tmpdir} ե
  * $dbi->{global}{filetype}{pooldir} ե
  * $dbi->{global}{tmpdir} ե

=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

