#!/usr/bin/perl
# Created by Ben Okopnik on Fri Oct 31 08:38:58 EST 2003
use warnings;
use strict;
use Tie::File;

my $c = "[H[2J";	# "clear" without shelling out :)
my @fields = qw/Country Lang HTTP FTP Name Email Comments/;

tie my @new, 'Tie::File', "mirrors.csv" or die "Unable to tie mirrors.csv: $!\n";
tie my @old, 'Tie::File', "old_mirrors.csv" or die "Unable to tie old_mirrors.csv: $!\n";

sub add {
	my ( $line, $value );
	
	for ( @fields ){
		print "$_: ";
		chomp( $value = <STDIN> );
		$value =~ s/,/\\,/g;
		$line .= $value;
		$line .= /Comments/ ? "\n" : ",";
	}
	return $line;
}

sub edit {
	my ( $edit, $value, $line, @rec ) = shift;
	
	$edit =~ s/\\,/&comma;/g;
	@rec = split /,/, $edit;
	s/&comma;/\\,/g for @rec;
			
	print "Type new value, or just press 'Enter' to keep the original one.\n\n";
	for ( 0 .. $#fields ){
		$rec[$_] ||= "";
		print "$fields[$_]: $rec[$_]\nNew value? ";
		chomp( $value = <STDIN> );
		$value =~ s/,/\\,/g;
		
		# If the entry is empty, keep the original value
		if ( $value =~ /^$/ ){
			$line .= $rec[$_]; 
		}
		# If it contains a word char, assign new value
		elsif ( $value =~ /\w/ ){
			$line .= $value;
		}
		# ...otherwise, empty it out.
			
		$line .= ( $fields[$_] =~ /Comments/ ) ? "\n" : ",";
	}
	return $line;
}

sub only_one {
	my @db = @{ $_[0] };
	my ( $count, $match, @found );
	print "Enter a string to match: ";
	chomp( $match = <STDIN> );
	$count = @found = grep /$match/, @db;
	
	if ( $count > 1 ){
		print "$_\n" for @found;
		print "\n\nMore than one match!\nPress a key to continue";
		<>;
		return undef;
	}
	
	if ( ! $count ){
		print "\n\nNo matches!\nPress a key to continue";
		<>;
		return undef;
	}
	return $found[0];

}

{
	print "$c";
	print "$_\n" for @new;
	print "=" x 80, "\nTotal: ", scalar @new, " records\n\n";
	
	print "[A]dd, [C]opy, [D]elete, [E]dit, [F]ind, or [Q]uit? ";
	my $answer = <STDIN>;

	if ( $answer =~ /a/i ){

		my $new = add();
		print "$c$new";
		print "Does this look OK? [Yn] ";
		push @new, $new if <> !~ /n/;
	}

	if ( $answer =~ /c/i ){
		print "$c";
		print "$_\n" for @old;
		print "=" x 80, "\nOLD DATABASE Total: ", scalar @old, " records\n\n";
		
		my $found = only_one( \@old );
		redo unless $found;
		
		print "\n$found\n\nIs this the correct record to ADD? [yN] ";
		if ( <> =~ /y/i ){
			my $line = edit( $found );
			push @new, $line;
			print "\nRecord added. Press a key to continue\n";
			<>;
		}
	}

	if ( $answer =~ /d/i ){
		
		my $match = only_one( \@new );
		redo unless $match;
		
		print "\n$match\n\nIs this the correct record to DELETE? [yN] ";
		if ( <> =~ /y/i ){
			@new = grep $_ !~ /$match/, @new;
			print "\nRecord deleted. Press a key to continue\n";
			<>;
		}
	}

	if ( $answer =~ /e/i ){
		my $match = only_one( \@new );
		redo unless $match;
		
		print "\n$match\n\n";
		my $line = edit( $match );
		@new = grep !/$match/, @new;
		push @new, $line;
		
		print "\nPress a key to continue\n";
		
		<>;
	}
	
	if ( $answer =~ /f/i ){
		print "Enter a string to match the record[s] you're looking for: ";
		chomp( my $match = <STDIN> );
		my @find = grep /$match/, @new;
		
		unless ( @find ){
			print "\n\nNo matches!\nPress a key to continue";
			<>;
			redo;
		}
		
		print "\n";
		print "$_\n" for @find, "\nPress a key to continue";
		<>;
	}
	
	print "Goodbye.\n" and exit if $answer =~ /q/i;

	redo;
}

untie @new;
untie @old;

