#!/usr/bin/perl
# If this is the second line of the program, the installationprocedure
# went wrong. Then adjust please the following three lines and the path
# to perl in the first line
$HOME=$ENV{HOME};
$CONFIGFILE="/usr/local/lib/addressbook/addressbook.config";
$MYCONFIGFILE="$HOME/.addressbook.config";
#
#########################################################################
#                                                                       #
# tel - get a telephone number                                          #
#                                                                       #
# This is part of my adressbuch / addressbook program			#
# Version 0.5, 28.05.1995						#
# Copyright (C) 1995 Clemens Durka					#
#									#
# Clemens Durka (durka@informatik.tu-muenchen.de)			#
# Lehrstuhl fuer Effiziente Algorithmen, Prof. Dr. E. Mayr		#
# Technische Universitaet Muenchen					#
# Arcisstr. 21								#
# D-80290 Muenchen							#
# Germany								#
#									#
#########################################################################
#									#
# This program is free software; you can redistribute it and/or modify	#
# it under the terms of the GNU General Public License Version 2 as	#
# published by the Free Software Foundation.				#	
#									#
# This program is distributed in the hope that it will be useful,	#
# but WITHOUT ANY WARRANTY; without even the implied warranty of	#
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		#
# GNU General Public License for more details.				#
#									#
# You should have received a copy of the GNU General Public License	#
# along with this program; if not, write to the Free Software		#
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		#
#									#
#########################################################################

if (!@ARGV) {
    print "$0: Suchen einer Telefonnummer\n";
    print "Ausruf: $0 Substring_eines_Namen\n";
    exit 1;
}

$progname="$0";
$searchterm=shift @ARGV;


if (! open (CFG, "<$CONFIGFILE")) {
    ; # warn ("Datei $CONFIGFILE konne nicht geoeffnet werden: $!.\n");  
} else {
    while (<CFG>) {
	if (/^([^# \n][\S]*)\s*(.*)/) {
	    $options{$1}="$2";
	}
    }
}
close CFG;

if (! open (CFG, "<$MYCONFIGFILE")) {
    ; # warn ("Datei $MYCONFIGFILE konne nicht geoeffnet werden: $!.\n");  
} else {
    while (<CFG>) {
	if (/^([^# \n][\S]*)\s*(.*)/) {
	    $options{$1}="$2";
	}
    }
}
close CFG;

$fields{phone} = -1;
$fields{phonepriv} = -1;
$fields{phonework} = -1;
$fields{fax} = -1;

open (FMT, "<$options{adrfile}.fmt") ||
    die ("Datei $options{adrfile}.fmt konne nicht geoeffnet werden: $!.\n");  
while (<FMT>) {
    if (/^([^# \n][\S]*)\s*(.*)/) {
	$fields{$2}="$1";
	$fields{$1}="$2";
    }
}
close FMT;


open (ADR, "<$options{adrfile}") ||
    die ("Datei $options{adrfile} konne nicht geoeffnet werden: $!.\n");  


while (<ADR>) {
    if (/$searchterm/i) {
	@line = split(/$fields{separatorchar}/);
	if (($line[$fields{firstname}] !~ /$searchterm/i) && ($line[$fields{lastname}] !~ /$searchterm/i)) {next;}

	print "$line[$fields{firstname}] $line[$fields{lastname}], $line[$fields{city}]:\t";

	if ($progname =~ /tel/) {
	    if (($fields{phone}>=0) && ($line[$fields{phone}] ne "")) {
		print "$line[$fields{phone}] ";
	    }
	    if (($fields{phonepriv}>=0) && ($line[$fields{phonepriv}] ne "")) {
		print "$line[$fields{phonepriv}] (private) ";
	    }
	    if (($fields{phonework}>=0) && ($line[$fields{phonework}] ne "")) {
		print "$line[$fields{phonework}] (work) ";
	    }
	    if (($fields{fax}>=0) && ($line[$fields{fax}] ne "")) {
		print "Fax: $line[$fields{fax}] ";
	    }
	} elsif ($progname =~ /fax/) {
	    if (($fields{fax}>=0) && ($line[$fields{fax}] ne "")) {
		print "Fax: $line[$fields{fax}] ";
	    } else {
		print "No Fax available";
	    }
	} elsif ($progname =~ /email/) {
	    if (($fields{email}>=0) && ($line[$fields{email}] ne "")) {
		print "$line[$fields{email}] ";
	    } else {
		print "No Emailaddress available";
	    }
	}
	print "\n";
    }
}

close(ADR);
