#     python/ui/MapPlane.py: CCP4MG Molecular Graphics Program
#     Copyright (C) 2001-2008 University of York, CCLRC
#
#     This library is free software: you can redistribute it and/or
#     modify it under the terms of the GNU Lesser General Public License
#     version 3, modified in accordance with the provisions of the 
#     license to address the requirements of UK law.
# 
#     You should have received a copy of the modified GNU Lesser General 
#     Public License along with this library.  If not, copies may be 
#     downloaded from http://www.ccp4.ac.uk/ccp4license.php
# 
#     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 Lesser General Public License for more details.


#---------------------------------------------------------------
# Hide the Tk window
#---------------------------------------------------------------
wm withdraw .
#
#----------------------------------------------------------------
proc GetEnvPath { var } {
#----------------------------------------------------------------
  global system
  global env

# Get environment variable - cope with whether input var has
# leading '$' or not


  regsub {^\$} $var  {} var1

# On NT change the separators to unix style /

  if { [regexp WINDOWS $system(OPSYS)] } {
    set status [catch { global env;
      regsub -all {\\} $env($var1) \/ path } nc ]
  } else {
    set status [catch {global env; set p1 $env($var1)} path ]
  }
  if { $status == 1 } {
    set path ""
    WarningMessage "Can not get environment variable for $var"
  }
  return $path
}

proc Diagnostic { text } {
  global mg_diagnostic
  if { $mg_diagnostic != "" } {
    puts $mg_diagnostic $text
  }
}

global mg_diagnostic
set stat [catch {open $env(HOME)/.CCP4MG/ccp4mg_gui_stdout.txt w} mg_diagnostic]
if $stat {
  set stat [catch {open $env(HOME)/CCP4MG/ccp4mg_gui_stdout.txt w} mg_diagnostic]
  if $stat { set mg_diagnostic "" }
}

#---------------------------------------------------------------------
# Startup Itcl & BLT
#---------------------------------------------------------------------
if { [ catch { package require Itcl } ] } { 
    set libs [ lsort -decreasing [ glob -nocomplain [file join $env(CCP4_ITCLLIB) libitcl*so ] ] ]
    set libs [ concat $libs [ lsort -decreasing [ glob -nocomplain [file join $env(CCP4_ITCLLIB) libitcl*dylib ] ] ] ]
    set libs [ concat $libs [ lsort -decreasing [ glob -nocomplain [file join $env(CCP4_ITCLLIB) itcl*dll ] ] ] ]
    set length [ llength $libs ]
    set idx 0
    set loaded 0
    set itcl_err ""
    while { $idx < $length && $loaded == 0 } {
	set lib [ lindex $libs $idx ]
	if { ![ catch { load $lib } ] } {
	    if { ![ catch { package require Itcl } ] } {
		set loaded 1
	    } else {
	    }
	} else {
	}
	incr idx
    }
    if { $loaded == 0 } {
	set itcl_err "Error: couldn't load Itcl from any of $libs"
    }
}

if { [ catch { namespace import -force itcl::* } ] } {
  Diagnostic "Couldn't import itcl namespace"
  Diagnostic "$itcl_err"
}

#Diagnostic "CCP4_BLTLIB $env(CCP4_BLTLIB)"
Diagnostic "MGROOT $env(MGROOT)"
Diagnostic "Loading BLT"
# Phil: uncomment the following three lines
#load [file join $env(MGROOT) tcltk lib libBLT24.so]
#package require BLT
#Diagnostic "Loaded explicit BLT"

if { [ catch { package require BLT } ] } { 
    set libs [ lsort -decreasing [ glob -nocomplain [file join $env(CCP4_BLTLIB) libBLT*so ] ] ]
    set libs [ concat $libs [ lsort -decreasing [ glob -nocomplain [file join $env(CCP4_BLTLIB) libBLT*dylib ] ] ] ]
    set libs [ concat $libs [ lsort -decreasing [ glob -nocomplain [file join $env(CCP4_BLTLIB) BLT*dll ] ] ] ]
    Diagnostic "BLT libs $libs"
    set length [ llength $libs ]
    set idx 0
    set loaded 0
    while { $idx < $length && $loaded == 0 } {
        set lib [ lindex $libs $idx ]
        Diagnostic "Trying $lib"
        if { ![ catch { load $lib } ] } {
            if { ![ catch { package require BLT } ] } {
                set loaded 1
            }
        } else {
        }
        incr idx
    }
    if { $loaded == 0 } {
      Diagnostic "Error: couldn't load BLT from any of $libs"
    }
}
namespace import blt::*


#--------------------------------------------------------------------

# Source startup.tcl appropriate for operating system

  set system(OPSYS) [string toupper $tcl_platform(platform)]
  source [file join $env(CCP4I_TOP) bin [string tolower $system(OPSYS)] startup.tcl]


global system
set system(RUN_MODE)		mggui
set system(TK_VERSION)		$tk_version
set system(SERVER_PORT)         ""
source [file join $env(CCP4I_TOP) src system.tcl]
source [file join $env(CCP4I_TOP) src utils.tcl]

# If there is an autoconfigure procedure for the system parameters and this opsys
# then run it
if { [llength [info proc autoconf_[subst $system(OPSYS)]_system]] > 0 } {
      autoconf_[subst $system(OPSYS)]_system system system }

InitialiseDotCCP4

source [SearchPath TOP etc types.def]
set mg_types_file  [file join $env(CCP4_MG) gui etc mgtypes.def]     
if { [file exists $mg_types_file ] } { source $mg_types_file }
global configure
InitialisePreferences configure configure


#----------------------------------------------------------------------------
# Get the socket ids from argument list
#----------------------------------------------------------------------------
global coms
global mg_winsize

set mg_winsize [list 800 800 ]
set label_columns 1
set font_size "medium"
set compact_disp_table 0

set nargs [llength $argv]; set n 0
while { $n < $nargs } {
  set cmd [string tolower [lindex $argv $n]]
  switch  -- $cmd \
  -sockets {
    incr n; set coms(SEND_SOCKET) [lindex $argv $n]
    incr n; set coms(REPLY_SOCKET) [lindex $argv $n]
  } -size {
    incr n; set mg_winsize [lindex $argv $n]
    incr n; lappend mg_winsize [lindex $argv $n]
  } -label_columns {
    incr n; set label_columns [lindex $argv $n]
  } -compact_disp_table {
    incr n; set compact_disp_table [lindex $argv $n]
  } -font_size {
    incr n; set font_size [lindex $argv $n]
  } -version {
    incr n; set ccp4mg_version [lindex $argv $n]
  }
  incr n
}


#----------------------------------------------------------------------------
# Start the display table
#----------------------------------------------------------------------------

source [file join $env(CCP4_MGGUI) src ccp4i_startup.tcl]
source [file join $env(CCP4_MGGUI) src display_table.itcl]
source [file join $env(CCP4_MGGUI) src preferences.itcl]


# Load hv3 and animated GIF - Linux only
if { [string first NIX $system(OPSYS)] >= 0 } {
  if { $tcl_platform(os) == "Linux" } {
    lappend auto_path [file join $env(MGROOT) gui  anigif]
    package require anigif 
    lappend auto_path [file join $env(MGROOT) tcltk lib Tkhtml3.0]
    source [file join $env(CCP4_MGGUI)  hv3 hv3_main.tcl]
  }
}

# Draw the 'top' windows
source [file join $env(CCP4_MGGUI) src mgtop.tcl]
