#!_SH_ 
# the next line restarts using wish \
exec _WISH_ "$0" "$@" 

# Carnet : a simple addressbook written in tcl/tk
# Copyright (C) 1996 Nocera Luciano
# Revision: $Id: carnet,v 1.2 1997/03/11 21:45:40 lnocera Exp lnocera $
# State: Exp
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 Licensake for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# Author : Nocera Luciano
# 
# Office  -       3401 Walnut street
#                 GRASP Lab, Rm # 316C
#                 University of Pennsylvania
#                 Philadelphia, PA 19104
#
# Home    -       2300 Pine St.
#                 Philadelphia, PA
#                 19103
#                 USA
#
# Phone           (215) 898-0336
# Fax             (215) 573-2048
# Email           lnocera@grip.cis.upenn.edu, luciano@mygale.org
# URL             http://www.cis.upenn.edu/~lnocera

# the current version
# the name if the main window
set INTERNAL(NAME) carnet

regsub -all {\$} {$Revision: 1.3beta $} "" RCS(REVISION)
regsub -all {\$} {$Date: 1997/03/11 21:45:40 $} "" RCS(DATE)

#----------------------------------------------------------------------#
# configure at the installation time what follows                      #
#----------------------------------------------------------------------#
# library path (where procs.tcl ext.tcl ... are located)
set INTERNAL(LIB_PATH) _LIBDIR_
# mail command for SimpleMail
set INTERNAL(MAILCOMMAND) _MAIL_COMMAND_
# finger command
set INTERNAL(FINGERCOMMAND) _FINGER_COMMAND_
# this variable control if a mail is sent to the maintainer (of carnet)
# the first time carnet is used (set it to 0 if you REALLY DON'T LIKE this)
set INTERNAL(SPYNEWUSER) _SPY_NEW_USER_

#----------------------------------------------------------------------#
# Do not edit below                                                    #
#----------------------------------------------------------------------#
# the maintainer email
set INTERNAL(MAINTAINER_MAIL) "lnocera@grip.cis.upenn.edu"
# database full path. This is for individual use.
set home $env(HOME)
# the WWW Home Page
set INTERNAL(WWW_HOME_PAGE) "http://www.cis.upenn.edu/~lnocera/carnet/index.html"
# ./.carnet dir
set INTERNAL(LOCAL_DIR_NAME) "$home/.$INTERNAL(NAME)"
# the private database
set INTERNAL(DBPRIVATE_NAME) "$INTERNAL(LOCAL_DIR_NAME)/db.private"
# the public database
set INTERNAL(DBPUBLIC_NAME)  "$INTERNAL(LOCAL_DIR_NAME)/db.public"
# the file used to import emails ... from vm or whatever
set INTERNAL(IMPORT_FILE_NAME) "$INTERNAL(LOCAL_DIR_NAME)/import"
# mark used to define a company in the listbox should not be
# set to null because companies and people have different key definitions
# see GetKey and companion functions GetLabelFromKey and GetKeyFromLabel
set INTERNAL(COMPANY_MARK)   {<comp.>}
# the config file name
set INTERNAL(CONFIG_FILE)    "$INTERNAL(LOCAL_DIR_NAME)/config"
# the custom file name
set INTERNAL(CUSTOM_FILE)    "$INTERNAL(LOCAL_DIR_NAME)/custom"
# icon bitmap full path.
set INTERNAL(BITMAP) $INTERNAL(LIB_PATH)/log.xbm

# unmap .
wm withdraw .

#----------------------------------------------------------------------#
# define wait procs
#----------------------------------------------------------------------#
proc WaitPlease { after message {p {}}} {
    global INTERNAL

    if [winfo exists .wait] {
	focus .wait
    } else {
	if { $p != {} } { wm withdraw $p }
    	set t [toplevel .wait]
	wm geometry .wait +400+350
	set f [frame $t.f -relief raised -bd 2 -height 5 ]
	set c [canvas $f.c -width 50 -height 40]
	$c create bitmap 20 20  -bitmap hourglass -anchor c
	set m [message $f.m -justify left -width 500 -padx 10 -pady 10 \
		   -text $message]
	pack $c $m -side left 
	pack $f
	focus $m
	update
	after $after
    }
}

proc WaitFinished {} {
    global INTERNAL
    update
    destroy .wait
}

if {$argc == 0} {
    WaitPlease 0 "

$INTERNAL(NAME) $RCS(REVISION), Copyright (C) 1996 Nocera Luciano
$INTERNAL(NAME) comes with ABSOLUTELY NO WARRANTY; for details invoke
`warranty' under the Help menu. This is free software, and you are 
welcome to redistribute it under certain conditions; invoke `license' 
under the Help menu for details.

$INTERNAL(NAME): Please wait, initalising...

"
}
set INTERNAL(DATABASE)    "all"
set BROWSE(DIR)           ""
set INTERNAL(CURRENT_KEY) ""
set INTERNAL(SAVE)        0
set INTERNAL(MODIFIED)    0
set INTERNAL(KSEP) 	  {}
set INTERNAL(KEND)        {}
# The following is the database header, it is check everytime a database is 
# loaded (DO NOT MODIFY AFTER INSTALLATION
set INTERNAL(HEADER)     \
"------------------------------------------------------------------

$INTERNAL(NAME) database


YOU SHOULD NOT EDIT THIS FILE DIRECTELY. IF YOU ARE CONSTRAINED TO
EDIT PLEASE REFER TO THE $INTERNAL(NAME) SOURCE FILES BEFORE. IN ANY
CASE YOU MUST NOT REMOVE OR CHANGE THIS HEADER IF YOU WANT
$INTERNAL(NAME) TO BE ABLE TO RECOGNIZE THIS FILE AS A VALID
$INTERNAL(NAME) DATABASE.


------------------------- RECORDS FOLLOW --------------------------
"
#
# regular expression used to parse the records for hypertext
#

# HTTP address
set INTERNAL(HTTP_REGEXP) 	{http://[^ ]+}
# FTP address
set INTERNAL(FTP_REGEXP)  	{ftp://[^ ]+}
# Files address
set INTERNAL(FILE_REGEXP) 	{file:[^ ]+}
# phones +33 16 1 123456...
set INTERNAL(PHONE_REGEXP1)	{([0-9]+ )*[0-9][0-9][0-9][0-9][0-9][0-9]+}
# phones +33 16 1 12 12 12... +33 16 1 12.12.12... +33 16 1 12-12-12... 
set INTERNAL(PHONE_REGEXP2)	{([0-9]+ )*([0-9][0-9][ \.\-][0-9][0-9])+([ \.\-][0-9][0-9]+)+}
# phone  +33 16 1 123 123 123... +33 16 1 123.123.123... +33 16 1 123-123-123...
set INTERNAL(PHONE_REGEXP3)	{([0-9]+ )*[0-9][0-9][0-9]([ \.\-][0-9][0-9][0-9])*([ \.\-][0-9][0-9][0-9]+)+}
# email
set INTERNAL(EMAIL_REGEXP)      {[A-Za-z.0-9_\-]+@[A-Za-z.0-9_\-]+}
set INTERNAL(ANNIVERSARY_REGEXP) {[0-9][0-9]/[0-9][0-9]/[0-9][0-9]([0-9][0-9])*}

#----------------------------------------------------------------------#
# Defaults overloaded by the config file
#----------------------------------------------------------------------#

set CONFIG(BACKUP_LEVEL)                3
set CONFIG(USE_ACCENTS)                 1
set CONFIG(XSCROLLBAR_ON)          	0
set CONFIG(YSCROLLBAR_ON)          	1
set CONFIG(TOOLBAR_ON)         		1
set CONFIG(LOGBAR_ON)          		1
set CONFIG(ALPHABAR_ON)          	1
set CONFIG(AUTOSAVE)           		1
set CONFIG(PRINT_EMAIL)        		0
set CONFIG(PRINT_PHONE)        		1
set CONFIG(PRINT_ADDRESS)      		1
set CONFIG(PRINT_NOTES)        		0
set CONFIG(PRINT_FORMAT)       		"ascii"
set CONFIG(PRINT_FILE)         		"$home/$INTERNAL(NAME).ascii"
set CONFIG(PRINT_WHAT)         		"all"
set CONFIG(REMEMBER_GEOMETRY)        	0
set CONFIG(GEOMETRY)        		""
set CONFIG(ENTRY_WIDTH)        		40
set CONFIG(ENTRY_HEIGHT)       		6
set CONFIG(LIST_WIDTH)         		29
set CONFIG(EDITOR_COMMAND)    		"netscape"
set CONFIG(BROWSER_COMMAND)    		"netscape"
set CONFIG(FTP_COMMAND)    		"netscape"
set CONFIG(MAIL_COMMAND)      		"SimpleMail"
set CONFIG(PHONE_COMMAND)      		"dial"
set CONFIG(ANNIVERSARY_HIGHLIGHT_COLOR) "limegreen"
set CONFIG(BROWSER_HIGHLIGHT_COLOR) 	"limegreen"
set CONFIG(FILE_HIGHLIGHT_COLOR) 	"limegreen"
set CONFIG(FTP_HIGHLIGHT_COLOR) 	"limegreen"
set CONFIG(PHONE_HIGHLIGHT_COLOR) 	"limegreen"
set CONFIG(MAIL_HIGHLIGHT_COLOR)	"limegreen"
set CONFIG(SEARCH)             		"global"
set CONFIG(SEARCH_CASE)        		0
set CONFIG(SEARCH_INTERACTIVE) 		0
set CONFIG(DATE_FORMAT)                 "+%m/%d/%y"
set CONFIG(ANNIVERSARY_WARNINGS_DELAY) 86400000 ;# every 24 hours for
					         # lucky people who never
					         # log out!

source $INTERNAL(LIB_PATH)/proc.tcl
TermDoIt ;# start in a shell if invoked in interactive mode

# window manager config
wm resizable    . 0 0
wm title        . $INTERNAL(NAME)
wm iconname     . $INTERNAL(NAME)
wm iconbitmap   . @$INTERNAL(BITMAP)

# error handling
source $INTERNAL(LIB_PATH)/error.tcl
foreach item {app print browse help simplemail accents ext} {
    source $INTERNAL(LIB_PATH)/$item.tcl
}
set MAIL(mailcommand) $INTERNAL(MAILCOMMAND) ;# this is for SimpleMail compatibility
TestInstall ;# test for the database

# config file
if [file exist $INTERNAL(CONFIG_FILE)] { source $INTERNAL(CONFIG_FILE) }
wm geometry . $CONFIG(GEOMETRY)

Init ;# initalise the interface

# adjust the search mode correctely
if {$CONFIG(SEARCH_INTERACTIVE)} {
    bind isearch <Key>            {SearchRecord}
    $SEARCH(ENTRY_BUTTON) config -state disabled
} else {
    bind isearch <Key>             {}
    $SEARCH(ENTRY_BUTTON) config -state normal
}

# load the custom file
set INTERNAL(CUSTOM_FILE) [glob -nocomplain $INTERNAL(CUSTOM_FILE)]
if [file exist $INTERNAL(CUSTOM_FILE)] { source $INTERNAL(CUSTOM_FILE) }

SearchRecord
if {$argc == 0} {WaitFinished}
wm deiconify .

Message "Welcome to $INTERNAL(NAME)"

# warn for today anniversaries
after 5 WarnTodayAnniversaries