#!/usr/bin/env perl
#
# Read a db schema stored in YAML format and create an empty database
# with those tables.
# Albert Danial August 21 2003
#
use lib '../util';  # TopsFEA.pm
use TopsFEA ( build_empty_db );
use YAML ( LoadFile, Dump );
use DBI;
use DBD::SQLite;
use Getopt::Std;
use vars qw ( $opt_d $opt_v );
use strict;
getopts('dv');


die "$0 [-d] <schema>.yaml

              Creates the file <schema>.db, an SQLite database file, from
              the schema defined in the input file.  Will delete <schema>.db
              if it exists.

             -d  Debug; show SQL statements but don't execute them.
             -v  Verbose
" unless @ARGV;
my $schema_file = shift;
die "Cannot read $schema_file\n" unless -r $schema_file;

my $extension   = "";
my $root_name   = "";
if ($schema_file =~ /^(.*?)\.(\w+)$/) {
    $root_name = $1;
    $extension = $2;
}
die "Schema file must have a .yaml extension (got $schema_file).\n"
    unless $extension eq "yaml";
my $DB_File = $root_name . ".db";
unlink $DB_File if -r $DB_File;

build_empty_db($schema_file, $opt_v, $DB_File);
