#!/usr/bin/wish
#
# OpenVerse Main Module 
# 
# this file initalizes the program and does any
# platform specific things/setup. It will then source 
# supporting modules.
#
# Module Name		- Main Module
# Sourced By		- Command Line
#
# Copyright (C) 1999-2001 David Gale <cruise@openverse.com>
# For more information visit http://www.openverse.com/
#
# 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.
# 
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
# USA.

global MV MVS

# Determine the name of the OpenVerse application
if {$argv0 != ""} {set app $argv0} else {set app [pwd]}
catch {set app [file readlink $app]}

# Set up platform-specific stuff
switch $tcl_platform(platform) {
"macintosh" {
	# TODO: Make Mac OS stuff really work

	# This seems to work if you put the wish
	# binary in the dir you are starting from.
	set MV(platform) "Macintosh"
	set MV(homedir) "."
	set MV(libdir) "$MV(homedir)/lib"
	set MV(libbase) "$MV(homedir)"
	set MV(helpdir) "$MV(homedir)/help"
	set MV(tearoff_menus) 0
	set MV(browser_cmd) "netscape"
} "unix" {
	global env

	# Check environment variable OVRUNDIR
	if [info exists env(OVRUNDIR)] {
		set MV(libbase) $env(OVRUNDIR)
	} else {
		set MV(libbase) "/usr/lib/OpenVerse"
	}
	set MV(platform) "UNIX"
	set MV(homedir) "~/.OpenVerse"
	set MV(libdir) "$MV(libbase)/lib"
	set MV(helpdir) "$MV(libbase)/help"

	# Install to home directory
	# TODO: remove -directory, use global installation
	if ![file exists $MV(homedir)] {file mkdir $MV(homedir)}
	foreach dir {"images" "rimages" "simages" "anims" "rooms" "panels"
			"plugins" "plugins/Query" "objects" "icons"
			"download"} {
		if ![file exists "$MV(homedir)/$dir"] {
			file mkdir "$MV(homedir)/$dir"
		}
		if ![file exists "$MV(libbase)/$dir"] continue
		foreach file [glob -nocomplain -directory\
				"$MV(libbase)/$dir" "*"] {
			set tail [file tail $file]
			set uname "$MV(homedir)/$dir/$tail"
			set gname "$MV(libbase)/$dir/$tail"
			# Copy file if:
			# - It's not a directory
			# - Either the user doesn't have it, or
			#   the user has an older version
			if {[file type $gname] != "directory" &&
					(![file exists $uname] ||
					[file mtime $uname] <
					[file mtime $gname])} {
				file copy -force $gname $uname
			}
		}
	}
	wm iconbitmap . "@$MV(homedir)/icons/ov.xbm"
	wm iconmask . "@$MV(homedir)/icons/ov.mask"

	set MV(tearoff_menus) 1
	if [info exists env(BROWSER)] {
		set MV(browser_cmd) $env(BROWSER)
	} else {
		set MV(browser_cmd) "netscape"
	}
} default {
	# Windows! This section needs some more work since windows does not
	# really support a safe central location for OpenVerse, there is really
	# no way to protect against users who delete things.
	if {[file exists "c:\\windows\\system32\\cmd.exe"] ||
			[file exists "c:\\winnt\\system32\\cmd.exe"]} {
		# For now, cmd.exe denotes Windows NT
		set MV(platform) "Windows NT"
		set MV(browser_cmd) "cmd.exe /c start"
	} else {
		# No cmd.exe; must be Windows 9x
		set MV(platform) "Windows 9x"
		set MV(browser_cmd) "start.exe"
	}
	set MV(homedir) [file dirname $app]
	set MV(libdir) "$MV(homedir)/lib"
	set MV(libbase) "$MV(homedir)"
	set MV(helpdir) "$MV(homedir)/help"
	set MV(tearoff_menus) 0
}} 

# Create directories in the user's personal OpenVerse directory
foreach dir {"rimages" "download" "objects" "simages"} {
	set dir "$MV(homedir)/$dir"
	if ![file exists $dir] {file mkdir $dir}
}

# Load modules
foreach module {"misc.tcl" "Globals.tcl" "IOFuncs.tcl" "BaseFunc.tcl"
		"Passage.tcl" "Objects.tcl" "SetFuncs.tcl" "Avatars.tcl"
		"ChtFuncs.tcl" "AvEd.tcl" "URL.tcl" "Bubble.tcl" "balloon.tcl"
		"nameplate.tcl" "tkohlp.tcl" "ServGUI.tcl" "Debug.tcl"
		"InitMain.tcl"} {
	source [file nativename "$MV(libdir)/$module"]
}

