#
# Copyright (c) 1992 The Ohio State University.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that: (1) source distributions retain this entire copyright
# notice and comment, and (2) distributions including binaries display
# the following acknowledgement:  ``This product includes software
# developed by The Ohio State University and its contributors''
# in the documentation or other materials provided with the distribution
# and in all advertising materials mentioning features or use of this
# software. Neither the name of the University nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

$options = "hd:";
$usage = 
"usage: edit-db-entry [-d debuglevel] [-h] 
    -d debuglevel	print debugging messages
    -h			display this message\n";

require 'global.defs';
require 'local.defs';
require $yagrip_perl;
require $backuplib_perl;

    #
    # Make sure that we clean up after ourselves if someone wants to kill us.
    #
$SIG{'INT'} = 'handle_interrupts';
$SIG{'HUP'} = 'handle_interrupts';
$SIG{'TERM'} = 'handle_interrupts';
$SIG{'QUIT'} = 'handle_interrupts';

    # 
    # Deal with args...we define *_string things to use as args for progs 
    # called from here so that we can pass on the right values (eg, -d
    # or "", etc). 
    #
die $usage if ! &getopt($options);
die $usage if defined($opt_h);
die $usage if $#ARGV != -1;

    #
    # Read the backup config file...
    #
&read_config();

db_loop:
    while (1) {
	$database = &askdef("Which database do you want to edit", 
			    "", 
			    "Give the name of the database that you want to edit, sans .dir or .db extensions\n");
	last db_loop if $database eq "";

	$type = &askdef("What is the type of that database", 
			    "", 
			    "Give the type of database (tape_db, run_db, run_host or backup_host)\n");

	$database = "$db_dir/$database";
	&edit_db($database, $type);
    }

exit(0);


sub edit_db {
    local($name, $type) = @_;
    local($command, @rest);

cmd_loop:
    while (1) {
	$command = &askdef("command (? for list)", "", 
			   "Edit command, one of:
    delete		to delete an entry
    edit		to update an entry
    show		to display an entry\n");
	last if $command eq "";

	($command, @rest) = split(/[ \t]+/, $command);

	if ($command eq "delete") {
	    &delete_me($name, $type, @rest);
	} elsif ($command eq "edit") {
	    &edit_me($name, $type, @rest);
	} elsif ($command eq "show") {
	    &show_me($name, $type, @rest);
	} else {
	    print "warning: unknown command.  ? for help.\n";
	}
    }
}

sub delete_me {
    local($name, $type, @rest) = @_;
    local($answer, $key);
    local($value, %array);

    if ($#rest == -1) {
	print "warning: must give at least one key to delete.\n";
	return;
    }

    if (&lock($name) != $LOCK_OK) {
	print "warning: couldn't lock $name.\n";
	return;
    }

    if (! &open_db(*DB, $name, $db_mode)) {
	print "warning: couldn't open $name.\n";
	&unlock($name);
	return;
    }

    foreach $key (@rest) {
	if (! &read_db(*DB, $key, *value, *array)) {
	    print "warning: there's no entry with key $key in $name\n";
	    &close_db(*DB, $name);
	    &unlock($name);
	    return;
	}

	print "The entry for $key looks like this:\n";
	&show_entry(*array);
	$answer = &askdef("Do you really want to nuke this", "no", "yes or no, fool\n");

	if ($answer ne "yes") {
	    &close_db(*DB);
	    &unlock($name);
	    return;
	}

	&delete_db(*DB, $key, $name);
    }

    &close_db(*DB);
    &unlock($name);
}

sub show_me {
    local($name, $type, @rest) = @_;
    local($value, %array, $key);

    if (! &open_db(*DB, $name, $db_mode)) {
	print "warning: couldn't open $name.\n";
	return;
    }

    if ($#rest == -1) {
	@rest = sort &keys_db(*DB);
    }

    foreach $key (@rest) {
	if (! &read_db(*DB, $key, *value, *array)) {
	    print "warning: $key\tno entry defined.\n";
	    next;
	}

	print "$key\n";
	&show_entry(*array);
    }

    &close_db(*DB, $name);
}

sub edit_me {
    local($name, $type, @rest) = @_;
    local(%replace, %add, %result);
    local($res, $field, $value, $mode, $key);
    local(@field_list);

    if ($#rest != 0) {
	print "warning: you can only edit one entry at a time.\n";
	return;
    }

    $key = $rest[0];

    $res = &update_db($name, $name, $key, $type, $DB_READ, *replace, *add, *result);
    if ($res == $DBERR_OK) {
	print "The current value for key $key is:\n";
	&show_entry(*result);
	$mode = $DB_UPDATE;
    } else {
	print "There's no entry for key $key in $name.\n";
	$mode = $DB_CREATE;
    }

    print "Please enter the list of field names and values that you want to add.
End your list with a blank name.\n";
    while (1) {
	$field = &askdef("Name value", "", "Field name and value\n");
	last if $field eq "";

	($field, @value) = split(/[ \t]+/, $field);
	$value = join(' ', @value);

	if (! defined($ok_keys{"$type $field"})) {
	    print "warning: That isn't a valid field name for this database.\n";
	    next;
	}

	$replace{$field} = $value;
    }

    @field_list = keys %replace;
    if ($#field_list >= 0) {
	$res = &update_db($name, $name, $key, $type, $mode, *replace, *add, *result);
	if ($res != $DBERR_OK) {
	    print "warning: blargh, update failed.\n";
	}
    }
}

sub show_entry {
    local(*result) = @_;
    local($name, $value);

    while (($name, $value) = each %result) {
	print "    $name\t$value\n";
    }
}
