#!/usr/bin/perl
use DBI;
use Carp;
use POP::Environment;
use POP::Schema_parser;
my $dsn = "dbi:Sybase:server=$POP_DB_SERVER;database=$POP_DB_DB";
my $dbh = DBI->connect($dsn, $POP_DB_USER, $POP_DB_PASSWD,
		{ RaiseError => 1 }) or
  croak "Couldn't connect to [$dsn]: $DBI::errstr";

my $parse = new POP::Schema_parser;
$parse->parse(\*STDIN);
if ($ARGV[0] eq '-init') {
  @ARGV = ('INIT');
}
unless (@ARGV) { @ARGV = grep {$_ ne 'INIT'} keys %$parse }
foreach my $schema (@ARGV) {
  push(@tables, map {$_->{'sql'}} grep {$_->{'type'} eq 'TABLE'} @{$parse->{$schema}});
  push(@indices, map {$_->{'sql'}} grep {$_->{'type'} eq 'INDEX'} @{$parse->{$schema}});
  push(@procs, map {$_->{'sql'}} grep {$_->{'type'} eq 'PROC'} @{$parse->{$schema}});
  push(@misc, map {$_->{'sql'}} grep {$_->{'type'} eq 'MISC'} @{$parse->{$schema}});
}
for (@misc, @tables, @indices, @procs) {
  eval {
    $dbh->do($_);
  };
  if ($@) { print STDERR $@ }
} 
$dbh->disconnect;
