use strict;
use FileHandle;

my $file = "datadex";
my $which = "main";

do "./dexmaster";
undef $@;
# vars from dexmaster
use vars qw( %dex $mobdir %parts );
# vars for here
use vars qw( %curbook );

my @parts = @{$parts{$which}};
my (%author, %title, %series);

# change out from under, bwa ha
*rdlnget = sub {
  my ($prompt, $default, $field) = @_;
  return $curbook{$field};
};
*yesno = sub { 1 };
*tempsave = sub {};

my $lockfile = "dexmaster.lock";
if (-e $lockfile) {
  open LOCK, $lockfile or die "Lock file exists, but cannot open.\n";
  die "dexmaster locked at ", <LOCK>, "\n";
}
my $moblock = "$mobdir/lock";
if (-e $moblock) {
  open LOCK, $moblock or die "Mobcomm lock exists, but cannot open.\n";
  print "\nWarning:   mobcomm in use as of ", <LOCK>, "\n",
  "It can be unpleasant if datadex is used simultaneously.\n",
  "If you continue, please WARN them before saving.";
  die unless yesno ("Continue?  ");
  close LOCK;
}
open LOCK, ">$lockfile" or die "Cannot create $lockfile";
print LOCK scalar localtime, " by $ENV{USER}";
close LOCK;

my $fh = new FileHandle $file or die "cannot read $file";
$dex{$which} = [];  
while (<$fh>) {
  chomp;
s/\{/\(/g;
s/\}/\)/g;
  @curbook{ @parts } = split /</;
  my $bk = specify("new") or die "specify failed on $_";
  my %val = inputval() or die "inputval failed on $_";
  @$bk{ keys %val } = values %val;
  $$bk{notnew} = 1;  # don't keep a newdex of all these...
  add($bk, undef, $which);
} 
$fh->close() or die "failed closing $file";

loaddex('lostdex', 'lost'); # since saving 'main' will write lostdex out
savedex($which);
unlink $lockfile;

print "Done!\n";
exit;
