#!/usr/bin/perl
# dbipg_create_table
#    $Id: dbipg_create_table,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;
use DBIPgSystem::Passwd;
use DBSession;

my $VERSION = '1.1.01';

# 
my $opt = { drop => '',
			sessiononly => '',
			passwd => '',
		  };
my $optlist = [ 'help!', 'drop!', 'sessiononly!', 'passwd!' ];
my $init = { basename => basename($0),
		   };

sub usage {
  my $ver = "(version $VERSION)" if($VERSION);
print <<USAGE;
$init->{basename} $ver
Usage: $init->{basename} [ץ] basefile [filename]
ARGS:
  basefile      ե
  filename      Υե
Options:
  --drop        DROPʸϤ (default: '$opt->{drop}')
  --sessiononly åѤTABLEΤߤ (default: '$opt->{sessiononly}')
  -passwd      ѥѤTABLEΤߤ (default: '$opt->{passwd}')
USAGE
  exit;
}

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

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

  my $file = shift;
  my $fh = \*STDOUT;
  open($fh, "> $file") or "Couldn't open $file: $!" if(defined $file);

  # إåν
  if(defined $file) {
	print $fh <<HEADER;
--
-- This file was made by $init->{basename}. Don\'t edit this file !
--\n
HEADER
  }

  my $dbi = DBIPgSystem::DB->new
	($baseconf->{conffile}, 'staff', 'root', test => 1, no_db => 1);
  my $session = DBSession->new
	("dbi:Pg:$dbi->{global}{db}{data_source}",
	 $dbi->{global}{db}{username}, $dbi->{global}{db}{passwd}, undef,
	 noconnect => 1);
  my $dbname = $dbi->{global}{db}{data_source};
  $dbname =~ s/^(?:.*;)?dbname=([^;]+)(?:;.*)?$/$1/;

  if($opt->{passwd}) {
	if(defined $baseconf->{authconffile}) {
	  my $dbpass = DBIPgSystem::Passwd->new
		($dbi, $baseconf->{authconffile}, test => 1);
	  print $fh $dbpass->create_table(drop => $opt->{drop});
	  print $fh $dbi->create_userinfo_table(drop => $opt->{drop});
	  if(defined $file) {
		print STDERR <<COMMENT;
To refresh the tables which depend on authorization, type
  \$ psql -f $file -U $dbi->{global}{db}{username} $dbname\n
COMMENT
	  }
	}
  } else {
	print $fh $dbi->create_table(drop => $opt->{drop})
	  unless($opt->{sessiononly});
	print $fh $session->droptable(noexec => 1) if($opt->{drop});
	print $fh $session->createtable(noexec => 1);
	if(defined $file) {
	  print STDERR <<COMMENT;
You can refresh the all tables in the database $dbname used by
DBIPgSystem::DB module except the table sys_userinfo using the
following Unix command:
  \$ psql -f $file -U $dbi->{global}{db}{username} $dbname\n
COMMENT
	}
  }

  close($fh) if(defined $file);
}
exit;

__END__

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

=for html
<div class="pod">

=head1 NAME

dbipg_create_table - ƥƯ뤿ɬפʥơ֥

=head1 DESCRIPTION

  $ dbipg_create_table basefile create.sql
  $ psql -U pguser -f create.sql dbname

  $ dbipg_create_table -drop basefile create.sql
  $ dbipg_create_table -drop -passwd basefile create_auth.sql

ե뤫ɤ߹򸵤ˡ
ɬפʥơ֥뤿 SQL ʸϤ롣
-drop ץդСơ֥뤿 SQL ʸϤ롣

=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
