#!/var/local/gna/bin/perl5

use lib "/var/local/gna/uu-gna/forms";

$CODE_DIR = "/var/local/gna/uu-gna/forms";
$TABLE_DIR = "/var/local/gna/uu-gna/tables";

$table_name = $ARGV[0];
$output_file = $ARGV[1];

# Figure out my perl name
open(ME, $0);
$perlPath = <ME>;
$perlPath =~ s/^#\!\s*(.*)\/(perl\S*)(.*)\n$/$1/;
$perlBin = $2 if !defined($perlBin);
close(ME);


require 'gna-lib.pl';
use DBstorage::RDB;

$dbh = DBstorage::RDB->new();

print <<EOP;
GNA Database Editor 

Copyright (c) 1995-1997 Globewide Network Academy

This software is distributed under the terms of the GNU Public License.
This software interactively the forms needed to edit a database.
EOP

if ($table_name eq "") {
    print "\nThe database information for this package is stored in a text file\nEnter the name of that file.\n";
    $table_name = <STDIN>;
    chop $table_name;
}
    $file_name = $TABLE_DIR ."/". $table_name;

if (!-e $file_name) {
    print "That file doesn't seem to exist.\n";
    print "Would you like to create the database file\n";
    $_ = <STDIN>;
    if ($_ !~ /^[yY]/) {
	print "Bye Bye!!!";
    } else {
	print <<EOP;
Enter the fields that you wish the table to contain.
Please separate them with spaces

EOP
	$_ = <STDIN>;
	chop;
	@fields = split(/\s+/);
    }
} else {
	print "$file_name found\n\n";
	%type = $dbh->attrib($file_name);
	@fields = @{$type{'fields'}};
}

$dir = `pwd`; chop $dir;

print "Fields found:\n", &gna_wordwrap(0,0,72, join(" ", @fields)), "\n";

print <<EOP;

The key fields are the fields which uniquely identify which entry.  Each
entry should have values for the key files which are different from the
other entries.

What are the key fields in your table?
EOP

$_ = <STDIN>; chop; @keycols = split(/\s+/);

print <<EOP;

Most of the fields are short entry input fields.  If you want you can
enter the fields that have long text entry.

Which fields do you want long text entry for?
EOP

$_ = <STDIN>; chop; @longcols = split(/\s+/);

foreach (@longcols) {
    $field_attrib{$_} = "long_entry";
}

if ($output_file eq "") {
    print "Enter the name of the CGI script file\n";
    $output_file = <STDIN>;
    chop $output_file;
}

if ($form_file eq "") {
    print "Enter the name of the html file to fill in\n";
    $form_file = <STDIN>;
    chop $form_file;
}				# 


if (-e "$dir/$output_file" || -e "$dir/$output_file.pl") {
   print  "Outfile $output_file already exists.  Overwrite?";
   $_ = <STDIN>;
   if (!/^[Yy]/) { 
	die "$output_file already exists";
    }
}


if (!-e "$dir/$form_file") {
   print  "Outfile $form_file does not exist.  Create?";
   $_ = <STDIN>;
   if (/^[Yy]/) { 
	&gna_mkrdbedit_create_html_file("$dir/$form_file",
	    \@fields, \%field_attrib);
        print "$dir/$output_file.html created\n";
   }
}

open(OUTPUT, ">$output_file") || die "Cannot open $output_file";
print OUTPUT <<EOP;
#!$perlPath/$perlBin
\$table = '$TABLE_DIR/$table_name';
\$dir = '$dir';
\$form_file = \$dir . '/$form_file';

use lib \"$CODE_DIR\";

use CGI qw(:cgi-lib);
require 'gna-lib.pl';
require 'gna-rdb-edit.pl';
require 'gna-fill-form.pl';
require '$output_file.pl';

\# This is the list of key columns.  Entries with identical key columns are 
\# assumed to be identical
EOP

print OUTPUT "\@keycols = (";

$last = pop(@keycols);
foreach (@keycols) {
    print OUTPUT '"' . $_ . '", ';
}
print OUTPUT '"' . $last . '");' . "\n";
push(@keycols, $last);

print OUTPUT <<'EOP';
# This is the list of attributes
#
# Attributes are invisibles tags which are included in the form
#

%attrib = ();

# This is the list of parameters
# Parameters control various elements of the form
# 

%params = (
   "scan_table", $table,
    "scan_page_length", 0, # length of page 0 if no paging
   "scan_marker", "(edit)",
   "form_file", $form_file,
   "append_marker", "Append record");


&gna_rdb_edit($table, "", *keycols, *scancols, *attrib, *params);
EOP

close(OUTPUT);
chmod 0775, "$dir/$output_file";
print "$dir/$output_file created\n";

open(OUTPUT, ">$dir/$output_file.pl") || die "Cannot open $dir/$output_file.pl";
print OUTPUT <<'EON';
#!/usr/bin/perl
# 
# This is a template file for the program dbedit.  It contains a set of 
# procedures which are called when the database is asked to perform 
# certain functions.  
#

# Space for local definitions

# ************************************************************************
#    Set permissions
# ************************************************************************

# &gna_rdb_edit_permissions
# This procedure sets the permission of the database.  It returns a string
# contains zero or more of the following characters
#
#     r - read permission (This flag currently does nothing)
#     w - write permission
#     a - append permission
#     d - delete permission

sub gna_rdb_edit_permissions {
    return "rwad";
}

# ************************************************************************
#    Process database entry
# ************************************************************************

# &gna_rdb_edit_process_append
# &gna_rdb_edit_process_replace
# &gna_rdb_edit_process_delete
#
# These procedures are called when a request to append, replace, or delete
# a database entry is made

sub gna_rdb_edit_process_append {
    local (*f) = @_;
}

sub gna_rdb_edit_process_replace {
    local (*f) = @_;
}

sub gna_rdb_edit_process_delete {
    local (*f) = @_;
}

sub gna_rdb_edit_process_fill_form {
    my($fref) = @_;
}

sub gna_rdb_edit_form_with_inserts {
    my($fref, $paramref, $insertref, $noedit) = @_;
    my ($returnval);
    $returnval .= $insertref->{'header'};
    $returnval .= $insertref->{'top'};
    &gna_rdb_edit_process_fill_form($fref);
    $returnval .= &gna_fill_form_with_ref($paramref->{'form_file'}, $fref, 
	$noedit);
    $returnval .= $insertref->{'bottom'};
    return $returnval;
}


# ************************************************************************
#    Postprocess database entry
# ************************************************************************

# &gna_rdb_edit_postprocess_append
# &gna_rdb_edit_postprocess_replace
# &gna_rdb_edit_postprocess_delete
#
# These procedures are called after a request to append, replace, or delete
# a database entry is made.  They are typically used to regenerate an
# index

sub gna_rdb_edit_postprocess_append {
    local (*f) = @_;
}

sub gna_rdb_edit_postprocess_replace {
    local (*f) = @_;
}

sub gna_rdb_edit_postprocess_delete {
    local (*f) = @_;
}
    
# ************************************************************************
#    Scan table
# ************************************************************************

# These routines control the display of the scanning table


# &gna_rdb_edit_print_scan_header 
#    Prints the scan table header
#    This <h6></h6> is a hack to make this look reasonable in text browsers

sub gna_rdb_edit_print_scan_header {
	print <<EOP;
<hr>
<table border=1>
<tr>
<td> </td><td><b>
EOP
EON
    print OUTPUT '     print join("</b></td> <td><b>", ';
$last = pop(@keycols);
foreach (@keycols) {
    print OUTPUT '"' . $_ . '", ';
}
print OUTPUT '"' . $last . '"), "</b></td></tr><h6></h6>";' . "\n";
push(@keycols, $last);

print OUTPUT <<'EON';
}

# &gna_rdb_edit_print_scan_line1
#    Prints the scan line

sub gna_rdb_edit_print_scan_line1 {
    local($name, $href, *f) = @_;
    print "<tr><td><a name=\"$name\" href=\"$href\">(edit)</a></td> <td>";
EON

    print OUTPUT '     print join("</td> <td>", @f{';
$last = pop(@keycols);
foreach (@keycols) {
    print OUTPUT '"' . $_ . '", ';
}
print OUTPUT '"' . $last . '"}), "</td></tr><h6></h6>";' . "\n";

print OUTPUT <<'EON';
}

sub gna_rdb_edit_print_scan_footer {
	print "</table><hr>\n";
}

    
# ************************************************************************
#    Print forms
# ************************************************************************
1;
EON
close(OUTPUT);
print "$dir/$output_file.pl created\n";


sub gna_mkrdbedit_create_html_file {
    my($filename, $fieldref, $field_attrib) = @_;
    local(*OUTPUT);
    open(OUTPUT, ">$filename")  || die "Cannot open $filename";
    
    print OUTPUT "<table>";
    foreach (@{$fieldref}) {
	if ($field_attrib->{$_} !~ "long_entry") {
	    print OUTPUT "<tr><td>$_</td><td><INPUT NAME=\"$_\" value=\"\"></td><h6></h6></tr>\n";
	}
    }
    print OUTPUT "</table>";
    foreach (@{$fieldref}) {
	if ($field_attrib->{$_} =~ "long_entry") {
	    print OUTPUT <<EON;
	    $_:<br>
		<textarea NAME=\"$_\" rows=5 cols=72>
		    </textarea>
EON
			}
       }
       close(OUTPUT);
 }
