#!/bin/sh
#  Alicq ICQ client
#  Copyright (C) Ihar Viarheichyk 2001

#  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 License for more details.

# Restart with wish \
exec wish "$0" "$@"

set base [file dirname [info script]]
lappend auto_path ~/tcl $base/icq $base/lib $base/modules

package require glue
package require icq
package require proxy
package require AlicqWidgets

set version 0.8.1

proc SetDefaults {} {
	set mydir [file dirname [info script]]
	switch $::tcl_platform(platform) {
	unix	{ 
		  set ::BASE 	{~/.alicq}
		  set ::RCLIST  [list $::BASE/alicqrc ~/.alicqrc]
		  set ::MODULES [list $::BASE/modules $mydir/modules\
		   		 /usr/share/alicq/modules]
		  set ::DATA	[list $::BASE/data $mydir/data\
		   		 /usr/share/alicq ]
		}
	windows	{ if {[info exists ::env(USERPROFILE)]} {
			set ::BASE [file join $::env(USERPROFILE) alicq]
		  } else { set ::BASE $mydir }
		  set ::RCLIST 	[list [file join $::BASE alicq.cfg]\
		  	       	[file join $mydir alicq.cfg]]
		  set ::MODULES [list [file join $::BASE modules]\
		  		      [file join $mydir modules]]
		  set ::DATA	[list [file join $::BASE data]\
		  		      [file join $mydir data]]
	  	}
	}
	if [info exists ::option(-config)] {set ::RCLIST $::option(-config)}
	
	# Base directory
	if {![file exist $::BASE]} {
		file mkdir $::BASE
		if {$::tcl_platform(platform)=={unix}} {
			file attributes $::BASE -permissions 0700
		}	
	}	
	
	# Default loggind
	set ::LOGLEVEL 0
	set ::LOGNAME [file join $::BASE alicq.log]
	if {[file exists $::LOGNAME]} {file delete $::LOGNAME}
}

proc GetOpts {} {
	foreach {key val} $::argv {
		if ![string match -* $key] {
			return -code error "$key: not an option"
		}
		set ::option($key) $val
	}
}

proc SourceConfig {{more 1}} {
	foreach fname $::RCLIST {
		if [file exists $fname] {
			set ::RCFILE $fname
			Log 5 "Startup file $fname found"
			#uplevel #0 source $fname; return 1
			if {[catch {uplevel #0 source "{$fname}"} v]} {
			   Log 5 "Repairing $::RCFILE"	
			   Log 5 $v
			   package require repair
			   if {$more} {return [SourceConfig 0]}
			} else { 
				Event ConfigLoaded
				return 1 
			}
		}
	}
	if {[info exists v]} {
		return -code error $v
	} else { return 0 }
}

# Logging
proc Log {level str} {
	if {$level>$::LOGLEVEL} return
	set fd [open $::LOGNAME a 0600]
	set ln "\[[clock format [clock seconds] -format {%x %X}]\]: $str"
	puts $fd $ln
	close $fd
	Event Log $ln
}

proc LogError {id {description "Unknown error"}} { Log 0 "ERROR $id \($description\)" }

proc DumpProperties {} {
	puts [format {%-30s %-8s %s} Property Type Description]
	puts [string repeat - 80]
	foreach y [lsort [grep x [lsmeta] {
			[string match $::option(-properties) $x]
		}]] {
		if {[property::exists $y -descr]} {
			set descr [property::meta $y -descr]
		} else { set descr "(no description)" }
		puts [format {%-30s %-8s %s} $y [property::meta $y -type]\
			$descr]
	}
	Finis
}
proc Finis {args} { exit 0 }

puts [time {

GetOpts
if {[info exists ::option(-version)]} {puts $version; Finis}
if {[info exists ::option(-help)]} {puts "alicq options:\n\t-version\n\t-properties <mask>\tlist properties by mask\n\t-config <file>\tspecify startup file to use\n\t-help\tthis help"; Finis}

SetDefaults
Log 0 "Alicq started"
# If config file was not found, using default module set
if {![SourceConfig] || [info exists ::option(-master)]} {
	package require AlicqMaster
	SourceConfig
}
if {[info exists ::option(-properties)]} DumpProperties

Hook Error LogError
Hook Global|Exit Finis
} 1]
# vim:syn=tcl
