#!/usr/bin/tclsh
# -*- tcl -*-
# $Id: gsm-tools.tcl,v 1.7 2003/07/30 07:46:55 grigory Exp $

set auto_path [linsert $auto_path 0 [file dirname [info script]]]

package require smslib
package require phonebooklib

proc SetDefaults {} {
    set ::PORT  /dev/ttyS0
    set ::SPEED 115200
    set ::language 0
    set ::MODEL t39
    set ::use_gui TK
    
    set ::CFGDIR          ~/.gsm-tools
    set ::ConfigFile      $::CFGDIR/config
    set ::RecivedFile     $::CFGDIR/recived.log
    set ::IncomingCallLog $::CFGDIR/incoming_call.log
    set ::PhoneBookFile   $::CFGDIR/phone_book.dat

    set ::PWD_DIR [file dirname [info script]]

    switch $::tcl_platform(platform) {
        unix { 
	    set ::LIBS [list $::PWD_DIR /usr/share/tcl/gsm-tools]
	}
        windows {
	    if {[info exists ::env(USERPROFILE)]} {
		set ::BASE [file join $::env(USERPROFILE) gsm-tools]
	    } else { set ::BASE $mydir }
	    set ::LIBS [list $::PWD_DIR $::BASE]
	}
    }
}

proc load_lib {libname} {
    foreach path $::LIBS {if {[file exists $path/$libname]} {source $path/$libname; break}}
}

SetDefaults
if {[file exists $::ConfigFile]} {source $::ConfigFile}
load_lib lib_phone_common.tcl
load_lib lib_$::MODEL.tcl

proc if_gui {} {
    if {[string match $::use_gui TK]} {return true}
    return false
}

#------------------------------------------------------------
fconfigure stdout -buffering none
proc stdin_read {} {
    set in_data [gets stdin]
    if {[regexp {^quit} $in_data]} {set ::forever 1} else {puts [phonelib::send_data $in_data]}
}
fileevent stdin readable stdin_read
#------------------------------------------------------------


proc log {file message} {
    set f [open $file a+];
    puts $f "\[[clock format [clock seconds] -format {%Y/%m/%d %T}]\]: $message"
    close $f
}


# Initialize module
phonebook::init_phonebook
if {[if_gui]} {load_lib gui.tcl} else {vwait ::forever}
