#!/bin/sh
#                         A R C H E R
# BRL-CAD
#
# Copyright (c) 2002-2007 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# version 2.1 as published by the Free Software Foundation.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this file; see the file named COPYING for more
# information.
#
###
#
# Archer
#
# Author(s):
#    Bob Parker
#    Doug Howard
#
# Description:
#    Main for Archer.
#
#\
ARCHER_HOME=`dirname $0`/..
#\
export ARCHER_HOME
# restart using bwish \
WISH="wish"
#\
for ish in bwish bwish_d wish wish_d ; do
# see if we're installed \
    if test -f ${ARCHER_HOME}/bin/$ish ; then
#\
	WISH="${ARCHER_HOME}/bin/$ish"
#\
	break;
#\
    fi
# see if we're not installed yet \
    if test -f ${ARCHER_HOME}/bwish/$ish ; then
#\
	WISH="${ARCHER_HOME}/bwish/$ish"
#\
	break;
#\
    fi
#\
done
#\
echo $WISH $0 $@
#\
exec $WISH $0 $@

#auto_mkindex_parser::slavehook { _%@namespace import -force ::itcl::* }

# Set the auto_path in order to constrain
# where Archer finds its files.
if {[info exists env(ARCHER_HOME)]} {
    set dir [file normalize $env(ARCHER_HOME)]
} else {
    set dir [file normalize [file join [file dir $argv0] ..]]
}

set env(ARCHER_HOME) $dir

if {$tcl_platform(os) == "Windows NT"} {
    set brlcadDataPath $env(ARCHER_HOME)
} else {
    set brlcadDataPath [bu_brlcad_data ""]
}

lappend auto_path $dir
lappend auto_path [file join $env(ARCHER_HOME) lib]
lappend auto_path [file join $env(ARCHER_HOME) lib tcl8.5]
lappend auto_path [file join $env(ARCHER_HOME) lib tk8.5]
lappend auto_path [file join $env(ARCHER_HOME) lib tcl8.4]
lappend auto_path [file join $env(ARCHER_HOME) lib tk8.4]
lappend auto_path [file join $env(ARCHER_HOME) lib itcl3.3]
lappend auto_path [file join $env(ARCHER_HOME) lib itk3.3]
lappend auto_path [file join $env(ARCHER_HOME) lib iwidgets4.0.2]
lappend auto_path [file join $env(ARCHER_HOME) lib iwidgets4.0.2 scripts]
lappend auto_path [file join $env(ARCHER_HOME) lib blt2.4]
lappend auto_path [file join $env(ARCHER_HOME) lib blt2.4 dd_protocols]
lappend auto_path [file join $env(ARCHER_HOME) lib sdb]
lappend auto_path [file join $brlcadDataPath tclscripts]
lappend auto_path [file join $brlcadDataPath tclscripts sdialogs]
lappend auto_path [file join $brlcadDataPath tclscripts sdialogs scripts]
lappend auto_path [file join $brlcadDataPath tclscripts swidgets]
lappend auto_path [file join $brlcadDataPath tclscripts swidgets scripts]
lappend auto_path [file join $brlcadDataPath tclscripts lib]
lappend auto_path [file join $brlcadDataPath tclscripts archer]
lappend auto_path [file join $dir tclscripts]
lappend auto_path [file join $dir tclscripts sdialogs]
lappend auto_path [file join $dir tclscripts sdialogs scripts]
lappend auto_path [file join $dir tclscripts swidgets]
lappend auto_path [file join $dir tclscripts swidgets scripts]
lappend auto_path [file join $dir tclscripts lib]
lappend auto_path [file join $dir tclscripts archer]

# Initialize bgerror
initBgerror

# Decide on a parent class for Archer
namespace eval Archer {
    if {1} {
	set parentClass itk::Toplevel
	set inheritFromToplevel 1
    } else {
	set parentClass TabWindow
	set inheritFromToplevel 0
    }
}

set Archer::debug 0
package require Archer 1.0
package require Iwidgets
package require Swidgets

# This must be done after loading BLT (that potentially gets it wrong)
set blt_library [file join $env(ARCHER_HOME) lib blt2.4]
lappend auto_path $blt_library

# PROCEDURE: showMainWindow
#
# Show the main window.
#
# Arguments:
#       None
#
# Results:
#       No return.
#
proc showMainWindow {} {
    if {$::Archer::inheritFromToplevel} {
	wm deiconify $::Archer::archer
	raise $::Archer::archer
	focus -force $::Archer::archer
    } else {
	wm deiconify .
	raise .
	focus -force .
    }
}

# PROCEDURE: createSplashScreen
#
# Create the proper splash screen based on gui mode.
#
# Arguments:
#       None
#
# Results:
#       No return.
#
proc createSplashScreen {} {
    global env
    global brlcadDataPath

    set useImage 1

    if {$useImage} {
	set image [image create photo -file [file join $brlcadDataPath tclscripts archer images aboutArcher.png]]
	set ::Archer::splash [Splash .splash -image $image]
    } else {
	set ::Archer::splash [Splash .splash -message "Loading Archer..."]
    }

    update idletasks
    $::Archer::splash configure -background $::Archer::SystemWindow
    $::Archer::splash center
    update idletasks
    $::Archer::splash activate
    after 3000 destroySplashScreen; # 3 more sec
    update
}

# PROCEDURE: destroySplashScreen
#
# Makes sure the gui is ready to go and then
# destroys the splash screen.
#
# Arguments:
#       None
#
# Results:
#       No return.
#
proc destroySplashScreen {} {
    # check to see if main gui is ready
    if {$::Archer::showWindow == 0} {
        after 1000 destroySplashScreen; # 1 more sec
        return
    }

    if {$::Archer::splash == ""} {
	return
    }
    $::Archer::splash deactivate
    itcl::delete object $::Archer::splash
    set ::Archer::splash ""
    showMainWindow
}

proc exitArcher {archer} {
    $archer askToSave

    if {$::Archer::inheritFromToplevel} {
	wm withdraw $archer
    } else {
	wm withdraw .
    }

    if {$archer != ""} {
	::itcl::delete object $archer
    }

    exit
}

# *************** Main Function ***************

# PROCEDURE: main
#
# Main procedure for AJEM/MUVES GUI.  Sets up and handles initialization.
#
# Arguments:
#       None
#
# Results:
#       No return.
#
proc main {} {
    global env
    global brlcadDataPath
    global tcl_platform
    global argv

    if {$::Archer::inheritFromToplevel} {
	set ::Archer::archer [Archer .\#auto]
	wm title $::Archer::archer "Archer"
	if {$tcl_platform(os) == "Windows NT"} {
	    wm iconbitmap $::Archer::archer -default \
		[file join $brlcadDataPath doc html manuals archer archer.ico]
	}
	set size [wm maxsize $::Archer::archer]
        set width [expr {[lindex $size 0] - 8}]
        set height [expr {[lindex $size 1] - 40}]
	wm geometry $::Archer::archer "$width\x$height+0+0"
	wm protocol $::Archer::archer WM_DELETE_WINDOW {exitArcher $::Archer::archer}
    } else {
	wm title . "Archer"
	if {$tcl_platform(os) == "Windows NT"} {
	    wm iconbitmap . -default \
		[file join $brlcadDataPath doc html manuals archer archer.ico]
	}
	set ::Archer::archer [Archer .\#auto]
	set size [wm maxsize .]
        set width [expr {[lindex $size 0] - 8}]
        set height [expr {[lindex $size 1] - 40}]
	pack $::Archer::archer -expand yes -fill both
	wm geometry . "$width\x$height+0+0"
	wm protocol . WM_DELETE_WINDOW [list exitArcher $::Archer::archer]
    }

    if {$argv != ""} {
	set target [lindex $argv 0]
	set ext [file extension $target]
	if {$ext == ".g"} {
	    $::Archer::archer Load "BRL-CAD" $target
	} elseif {$ext == ".xg" || $ext == ".sdb"} {
	    $::Archer::archer Load "IVAVIEW" $target
	}
    }

    $::Archer::archer configure -quitcmd [list exitArcher $::Archer::archer]

    update
}

# TCL script execution start HERE!
wm withdraw .

# start splash screen
createSplashScreen
 
# do main procedure
main
set ::Archer::showWindow 1

# Local Variables:
# mode: sh
# tab-width: 8
# sh-indentation: 4
# sh-basic-offset: 4
# indent-tabs-mode: t
# End:
# ex: shiftwidth=4 tabstop=8
