#!/usr/athena/bin/perl -lw use 5.006_000; use strict; do "./dexmaster"; # must be what Panthercomm locks in mitsfs/dex/ undef $@; use vars qw( %category %dex %parts ); my $file = shift or die "Please specify the input filename on the command line."; my $withdraw = datadex_order (loaddex ($file, 'temp')) or die "failed reading batch withdrawals from $file"; ### needful if and only if we use dexmaster add() #use vars qw( $restoring $inven ); #$restoring = 1; #$inven = 0; # might want to do these earlier lock_file("dexmaster") or exit 1; # datadex would probably be better target print "Loading dex..."; loaddex ('datadex', 'main') || die "Cannot open datadex"; my ($n, $bk) = undef; my $badbooks = ""; for my $took (reverse @$withdraw) { ($bk, $n) = findbook (-find => $took, -which => "main", -start => $n); $bk or die panther($took), "\nnot known"; my %have = codetohash ($$bk{'code'}); my %take = codetohash ($$took{'code'}); exists $have{'@'.$_} and $take{'@'.$_} ||= delete $take{$_} for keys %take; $have{$_} -= $take{$_} for keys %take; #print panther($bk); # die "withdrew below soft limit taking $$took{code} from\n", panther($bk) # if grep { $have{$_} < $category{basecode($_)}{'soft'} } keys %take; my @bad = grep { $have{$_} < $category{basecode($_)}{'soft'} } keys %take; # die "withdrew below soft limit taking $$took{code} from\n", panther($bk), # "\n@bad" $badbooks = ("withdrew below soft limit taking $$took{code} from\n" . panther($bk) . "\n" . $badbooks) if @bad; $$bk{'code'} = hashtocode (%have); } if ($badbooks ne "") { print $badbooks; unlock_all(); exit 0; } elsif (!yesno("No errors. Apply and save changes? ")) { unlock_all(); exit 0; } # now HERE's a nasty hack # #*realsavedex = *savedex; #*savedex = sub { realsavedex() unless @_ }; #savedex(); # # ARGH! Why can't I get some variant on the above to work, dammit? # mysavedex(); unlock_all(); exit 0; sub mysavedex { # datadex-format stuff only my $which = shift || "main"; my %file = ( "main" => "datadex.tmp", ); my $file = $file{$which} or return warn "Don't know how to save $which"; print "Writing $file"; my $out = new IO::File $file, "w" or return warn "Error $! opening $file for output"; for my $bk (@{$dex{$which}}) { $out->print(panther($bk)) } $out->close(); if ($which eq "main") { (my $timesuffix = scalar localtime) =~ s/ /_/g; print "Making diffs"; `diff $file datadex > back.$timesuffix`; `diff datadex $file > forw.$timesuffix`; `chmod 400 back.$timesuffix forw.$timesuffix`; print "Moving $file in"; rename $file, "datadex" or die; } }