#!/usr/local/bin/perl -Tw
use vars qw( $DBHOST $DBTYPE $DATABASE $DBUSER $DBPASS $DEBUG $CLASS $DB 
             $OPTIONS @ACTIONS $VERSION );
$VERSION = "2.0";

=head1 NAME

tbtel - a command-line interface to the TCB::AddressBook database

=head1 SYNOPSIS

tbtel [pattern]

=head1 DESCRIPTION

tbtel searches the TCB addressbook ('UserDB' on db.ks.uiuc.edu) for
C<pattern>.  It's meant to take the place of the old 'tel' script.

If you want to search for Tim Skirvin:

  tbtel tim skirvin

If you want all people with the name Joanna:
 
  tbtel joanna

And if you want a full dump of the addressbook:
  
  tbtel '*'

For the specifics: C<pattern> consists of of the command line options
offered to the program.  Whitespace and *'s are replaced with wildcards;
other SQL operations may work.  The searches are not case-sensitive.

=cut

###############################################################################
### CONFIGURATION + PRIVATE DATA ##############################################
###############################################################################

## Load shared configurations and/or private data using 'do' commands, as
## seen below.  Note that several 'do's can be run if necessary.

# do '/FULL/PATH/TO/CODE/TO/RUN';

## This is the perl class that you will be using in this script.

$CLASS   = "TCB::AddressBook";                   # Database class

## Modify and uncomment this to use user code instead of just system-wide
## modules.  Note that this path must be set up as a standard Perl tree;
## I'd personally recommend just installing things system-wide unless you're
## a developer.

# use lib '/home/tskirvin/DBI';
use lib '/home/tskirvin/dev/mdtools/tcb-addressbook';

## Database Information
## You may want to set these with a common config file, using 'do FILE'.
## Also, defaults may already be set within the class; only set these if
## you want to override the defaults.

# $DBHOST   = "";               # System that hosts the database
# $DBTYPE   = "";               # The type of database that we're working on
# $DATABASE = "";               # Name of the database we're connecting to
# $DBUSER   = "";               # Username to connect to the database
# $DBPASS   = "";               # Password to connect to the database

do '/home/webserver/dbaccess/user.sbook-guest';  # Populate DBUSER, DBPASS, etc

## Debug Information
## Should we print debugging information?  1 for yes, 0 for no.

$DEBUG = 0;

###############################################################################
### main() ####################################################################
###############################################################################

use strict;

# Load the appropriate class module
{ local $@; eval "use $CLASS";  die "$@\n" if $@; }

$0 =~ s%.*/%%g;		# Lose the annoying path information

my $arg = join(' ', @ARGV) || Error("Usage: $0 [pattern]");
   $arg =~ s/^|$/%/g;		# Make it match non-exact patterns
   $arg =~ s/(\*|\s+)/%/g;	# Whitespace and *'s become wildcards

$DB = $CLASS->connect( $DATABASE, $DBUSER, $DBPASS, $DBHOST, $DBTYPE )
        or Error("Couldn't connect to $DBHOST: " . DBI->errstr . "\n");

my $count = 0;
foreach my $item ($DB->select('AddressBook', { 'Name' => $arg } )) {
  next unless ($$item{'Name'} && $$item{'Info'});
  print "\n" if $count;		# Newline between entries
  my $name = $$item{'Name'};  $name =~ s/(^\s+|\s+$)//g;
  my $info = $$item{'Info'};  $info =~ s/(^\s+|\s+$)//g;
  print "***** $name *****\n$info\n";
  $count++;
}

$DB->disconnect;

Error($count ? "" : "No Information for '$arg'");
exit(0);

###############################################################################
### Subroutines ###############################################################
###############################################################################

sub Error {
  foreach (@_) { next unless $_; print "$_\n"; }

  if ($DEBUG && $DB) {
    print "SQL Queries:\n";
    foreach ($DB->queries) { print "$_\n" }
  }

  exit(0);
}

=head1 REQUIREMENTS

Requires Perl 5 or better, and the DBI module.  It so far works on the
Suns.

=head1 AUTHOR

Tim Skirvin <tskirvin@ks.uiuc.edu>

=head1 COPYRIGHT

Copyright 2000-2001 by University of Illinois Board of Trustees and 
Tim Skirvin <tskirvin@ks.uiuc.edu>

=cut
