#!/bin/sh
# unverse.tcl
# Starting point for the unununiverse client and server

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

# -- Set up logging, right away -----------------------------------------------
namespace eval lib {
namespace export enable_log disable_log log

# Flags to indicate how to handle each log class
variable log_action
set log_action(norm) 0
set log_action(chat) 0
set log_action(warn) 0
set log_action(err) 1

# Enables logging of class 'type'
proc enable_log {type} {
	variable log_action
	if [info exists log_action($type)] {
		set log_action($type) 1
		log "unverse" "norm" "$type logging enabled"
	} else {
		log "unverse" "warn" "Can't enable logging for bad class" \
				"'$type'"
	}
}

# Disables logging of class 'type'
proc disable_log {type} {
	variable log_action
	if [info exists log_action($type)] {
		log "unverse" "norm" "$type logging disabled"
		set log_action($type) 0
	} else {
		log "unverse" "warn" "Can't disable logging for bad class" \
				"'$type'"
	}
}

# Log 'args' of class 'type' from file 'file'
proc log {file type args} {
	variable log_action

	# Build msg from all the args
	set msg [join $args]

	if [info exists log_action($type)] {
		if !$log_action($type) return;
		switch $type {
		norm {
			puts "\033\[0;34m$file\033\[1;30m: \033\[0m$msg"
		} warn {
			puts -nonewline "\033\[0;34m$file\033\[1;30m: "
			puts "\033\[1;33m$msg\033\[0m"
		} err {
			puts -nonewline "\033\[0;34m$file\033\[1;30m: "
			puts "\033\[1;31m$msg\033\[0m"
		} default {
			puts -nonewline "\033\[0m[\033\[1;32m$type\033\[0m\] "
			puts "\033\[30m$file: \033[0m$msg"
		}}
	} else {
		log "unverse" "warn" "Can't log '$msg' from '$file'; bad log" \
				"class '$type'"
	}
}

}

# -- Change these lines to toggle initial debugging ---------------------------
lib::enable_log "norm"
lib::enable_log "chat"
lib::enable_log "warn"
lib::enable_log "err"

# -- Collect information about this system ------------------------------------
namespace eval lib {
namespace export fs

variable fs

switch $tcl_platform(platform) {
"macintosh" {
	log "unverse" "err" "Macintosh platform, commiting suicide"
	exit
} "unix" {
	log "unverse" "norm" "UNIX platform, yay"

	if [info exists env(OVRUNDIR)] {
		set fs(sys) $env(OVRUNDIR)
	} else {
#		set fs(sys) "/usr/lib/unverse"
		set fs(sys) "~/devel/unverse"
	}
	set fs(sys.lib) "$fs(sys)/lib"
#	set fs(sys.rc) "/etc/unverse/global.rc"
	set fs(sys.rc) "$fs(sys)/etc/unverse/global.rc"
#	set fs(sys.client.rc) "/etc/unverse/client.rc"
	set fs(sys.client.rc) "$fs(sys)/etc/unverse/client.rc"
#	set fs(sys.server.rc) "/etc/unverse/server.rc"
	set fs(sys.server.rc) "$fs(sys)/etc/unverse/server.rc"
	set fs(sys.client) "$fs(sys)/client"
	set fs(sys.server) "$fs(sys)/server"
	set fs(sys.common) "$fs(sys)/common"

#	set fs(usr) "~/.unverse"
	set fs(usr) "$fs(sys)/home"
	set fs(usr.rc) "$fs(usr)/global.rc"
	set fs(usr.client.rc) "$fs(usr)/client.rc"
	set fs(usr.server.rc) ""
	set fs(usr.download) "$fs(usr)/download"
	set fs(usr.client) "$fs(usr)/client"
	set fs(usr.server) "$fs(usr)/server"
	set fs(usr.common) "$fs(usr)/common"
} default {
	log "unverse" "err" "Windows platform, murdering self"
	exit
}}

}

# -- Run client or server? ----------------------------------------------------
namespace eval lib {
namespace export mode

if {$argv == ""} {
	log "unverse" "norm" "Initiating client"
	variable mode "client"
} else {
	log "unverse" "norm" "Initiating server"
	variable mode "server"
	set fs(usr.server.rc) $argv
}

}

# -- Load the library ---------------------------------------------------------
lib::log "unverse" "norm" "Loading core library"
source "$lib::fs(sys.lib)/list.tcl"
source "$lib::fs(sys.lib)/config.tcl"
source "$lib::fs(sys.lib)/hook.tcl"
source "$lib::fs(sys.lib)/module.tcl"

# -- Load needed modules ------------------------------------------------------
namespace eval lib {
namespace export module service

# Initalize module lists to empty
variable module
set module(all) ""
set module(loaded) "lib"
set module(requested) ""

}

# Create the mod and serv namespaces
namespace eval mod {}

# Load all applicable configuration files
lib::load_configuration

# Load modules
lib::log "unverse" "norm" "Loading modules"
lib::identify_modules
lib::load_module $lib::module(requested)

# Commandline hack
if [info exists tk_version] {
	entry .e -textvariable command
	bind .e <Key-Return> {eval $command; set command ""}
	pack .e -fill x
}

# -- Event loop ---------------------------------------------------------------
if {!($tcl_interactive || [info exists tk_version])} {
	lib::log "unverse" "norm" "Waiting forever"
	vwait forever
}

