#!/bin/sh

# /bin/sh interprets this part, but wish ignores it...
#\
exec wish "$0" "$@"
#geodas requires tcl 8.0 or better

#GEODAS Search Front End Shell Script
#Written by: 
#     Eric Peers
#Modified by:
#     Josh Howie 1998,1999
#Modified by: 
#     Dan Metzger 1999,2000,2001

#tk_setPalette background lightgrey activeBackground slategrey
#tk_setPalette #4783b5
#tk_setPalette lightsteelblue
#tk_setPalette BlanchedAlmond

#tk_setPalette CornflowerBlue ;#nice but dark
#tk_setPalette SkyBlue ;#nice
#tk_setPalette DeepSkyBlue
#tk_setPalette PowderBlue
#tk_setPalette PaleTurquoise

#tk_setPalette LightSkyBlue ;#nice

#set directory variables
if {[file exists ./geodas.cfg]} {
  set _pathconfig "./"
} elseif {[file exists ~/geodas/geodas.cfg]} {
  set _pathconfig [glob ~]/geodas
} elseif {[file exists ~/gdas/geodas.cfg]} {
  set _pathconfig [glob ~]/gdas
} else {
   set temp $env(PATH)
   regsub  -all ":" $temp " " temp
   set found 0
   foreach i $temp {
      if {[file exists $i/geodas.cfg]} {
         break
      }
   }
   set _pathconfig $i
}
   
if {[file exists $_pathconfig/geodas.cfg]} {
  source $_pathconfig/geodas.cfg
} else {
   puts "Could not find a geodas.cfg in home path, current path, or in PATH"
   exit
}

load $_paths(lib)/libgeodastcl.so Geodastcl   ;#load the geodas commands

catch {
  tk_setPalette $_settings(colorscheme)
}

#expand ~/ if found in work path
if [string match ~ [string range $_paths(work) 0 0]] {
  set _paths(work) "[glob ~][string range $_paths(work) 1 end]"
}

set _paths(searchdata) "" ;#dir for storing Search Data Files (will be _paths(work)/[SearchKey])


set screenw [winfo screenwidth .]

if {$screenw >= 1280} {
  set _mapwidth 1024
  set _mapheight 768
} elseif {$screenw >= 1024} {
  set _mapwidth 800
  set _mapheight 600
} elseif {$screenw >= 800} {
  set _mapwidth 640
  set _mapheight 480
} else {
  set _mapwidth 600
  set _mapheight 400
}


##########################
#   GEODAS Help System based on html_library (see file html_library for licence terms)
##########################
source $_paths(lib)/html_library 
#global help_initialized
#global _contents
#global _index
set help_initialized 0
set _contents cont_sr.htm
set _index index_sr.htm

 
###### keep grddas inventories from being added to _dblist....
foreach dbase [array names _databases] {
   if ![string match grd* $dbase] {
   lappend _dblist $dbase
   }
}

#check here for prog path, check where needed for other paths...
if {![info exists _paths(prog)] || $_paths(prog)==""} {
   puts "geodas.cfg error -- no PROG= path specified!"
   exit
}
set auto_path "$_paths(prog) $auto_path"

if { $_paths(work)==""} {
   puts "geodas.cfg error -- work path not available/specified!"
   exit
}
 #puts "this is path to work: $_paths(work)"
if {![file isdirectory $_paths(work)]} {
   file mkdir $_paths(work)
}

    

foreach i $_dblist {
   if {![info exists _paths($i)]} {
      set _paths($i) ""
      set _database $i
      find_inv
   }
}


set _quit 0

#initialize some of the global search criteria values
set _key ""
set _title "Untitled"
set _database $_settings(database)

#these are all the sub search criteria variables
set _type 2 
set _subfile ""
for {set i 0} {$i<50} {incr i} {
   set _ngdc($i) ""
   set _survey($i) ""
}
for {set i 0} {$i<10} {incr i} {
   set _geoparam($i) 0
}
set _geounion 0
set _srcunion 0
set _source {}
set _ngdcunion 0
set _surveyunion 0
set _area {}
   set _a_lastx -1
   set _a_lasty -1
   set _a_firstx -1
   set _a_firsty -1
   set _a_poly 0
   set _a_height 0
   set _a_width 0
   set _limits(top) 90.0
   set _limits(bot) -90.0
   set _limits(left) -180.0
   set _limits(right) 180.0
   set _limits(z360) 0
   set _searchlimits(top) 90.0
   set _searchlimits(bot) -90.0
   set _searchlimits(left) -180.0
   set _searchlimits(right) 180.0
   set _limits(z360) 0
   set _areahint "Use menu to select Map View, Select Area, etc."
   set _uspoly {{-66.62 47.07} {-66.62 44.46} {-69.96 42.97} {-69.53 41.61} {-73.74 39.93} {-75.54 36.95} {-75.29 35.03} {-81.11 30.68} {-80.0 26.7} {-80.2 24.6} {-82.4 24.4} {-83.78 29.2} {-89.72 28.7} {-94.0 29.2} {-96.97 24.97} {-115.05 32.42} {-118.52 32.42} {-121.0 33.91} {-125.27 40.24} {-124.96 48.68} {-121.31 49.12} {-85.45 49.43}}
 


set _year(0) 1850
set _year(1) 2020
set _date(0) 19780101
set _date(1) 20201231


#set the search selected variables for criteria selections
for {set i 0} {$i<10} {incr i} {
   set search($i) 0
}

#set the header window variables
set _hdrfile ""
set _hdr_data(0) {}
set _hdr_datas(0) {}
set _hdr_datal(0) {}

#just define an array called entry so I can use it as a temp storage variable...
set _entry(0) {}

#initialize other global variables 
set _currhint "Use File Menu to start a New search or Open a previous search"
set _lasthint $_currhint
set _active_search ""
set _active_header ""
set _active_plot ""
set _active_dl ""
set _active_dialog ""
set _active_name ""


set _pparam "NAVIGATION"
set _pframe "SING"
set _pcolor "mono"
#set the plot variables for once the search is complete...
for {set i 0} {$i<4} {incr i} {
   set _plot($i) 0
}
set _ptop 90.0
set _pbot -90.0
set _pleft -180.0
set _pright 180.0

set _legend {}
set _flash_quit 1
set _oldtag {}
set _oldcolor {}

#this is a colorpalette for use in legends, etc...
set _colorpalette "black blue turquoise {dark green} orange red plum slategrey {navy blue} {dark sea green} {hot pink} gold {medium purple} {dark orange} burlywood thistle {dark slate gray} {cadet blue} goldenrod {dark olive green} {dark turquoise} {medium orchid} {sea green} {orange red} {navajo white} magenta RoyalBlue {medium aquamarine} {saddle brown} {lime green} snow4 {dark orchid} LightSlateGrey {rosy brown} {green yellow} {dark khaki} {light steel blue} khaki {indian red} bisque3 lavender {blue violet} {cornflower blue} tomato {medium spring green} {medium violet red} cyan firebrick LawnGreen {medium blue} {forest green} {dodger blue} {pale violet red} chocolate MediumTurquoise green salmon {midnight blue} {light sea green} {pale green} {spring green}"

set _dlorg "Single"
set _dlcorrect 0
set _dlformat(0) "dfA77"
set _dlformat(1) {}
set _dlparam(0) "All"
set _dlparam(1) {1 1 1 1 1 1 1 1}


set _bathybot 100
set _bathytop 0
set _bathyinc 1
set _bathyfiles {}
set _bathypath ""
set _bathyswap 0pwd

#the temps are used in finding files and anywhere else I need them
set _temp 0
set _temp2 0
set _temp3 0


find_images
#initialize the main window
wm title . "GEODAS Data Search and Retrieval" 
wm geometry . +20+100
wm iconbitmap . "@$_paths(images)/geodas.ico"
wm protocol . WM_DELETE_WINDOW {
   if {[closesearch]!=2} exit   ;#if closesearch dialog not canceled, then exit
}




#set up top menu
#create the menubar
frame .menu
pack .menu -side top -anchor nw -fill x

menubutton .menu.file -text "File" -menu .menu.file.m -pady 2 \
           -underline 0
  menu .menu.file.m -tearoff 0
    .menu.file.m add command -label "New Search" -underline 0 -command {
          set _active_search ""
          set _currstatus "New Search"
          set _currhint "Use Task menu to define search criteria"
          set _active_dialog .crit
          check_menus
          loadvars
          set exitstatus [selectcrit]
          if {$exitstatus == 0} {
            loadvars
          }
          set _active_dialog ""
          check_menus
          if {$_active_search!=""} {
             set _currhint "Use the program menu to redefine criteria, display results, download data"
             update
             casc_wins
          } else {
             set _currhint "No search criteria selected! Recreate a new search"
          }
       }
    .menu.file.m add command -label "Open Search..." -underline 0 \
       -command {
          set _currhint "Double Click on the Search you wish to open, then click OK"
          set _active_dialog .open
          check_menus
          opensearch
          set _active_dialog ""
          check_menus
          if {$_active_search!=""} {
             set _currhint "Use the Task menu to redefine criteria, display results, download data"
             update
             casc_wins
          } else {
             set _currhint "Use the file menu to open/create a new Search"
          }
       }
    .menu.file.m add command -label "Close Search" -underline 0 -state disabled -command {
          closesearch
          check_menus
       }
    .menu.file.m add separator
    .menu.file.m add command -label "Delete Search(s)..." -underline 0 -command {
      delsearch 
    }       
    .menu.file.m add separator
    .menu.file.m add command -label "Print..." -underline 0 -state disabled -command {
      #set command "$_settings(print) $_paths(work)/$_key.lss &"
      #eval exec $command
      printsearch
    } 
    .menu.file.m add separator
    .menu.file.m add command -label "Exit" -underline 1 -command {
       if {[closesearch]!=2} exit
    }



# Create the menu button
menubutton .menu.prog -text "Task" -menu .menu.prog.m -pady 2\
           -underline 0 -state disabled
#create the actual menu...
  menu .menu.prog.m -tearoff 0
     .menu.prog.m add command -label "Search..." \
        -underline 0 -command {
           set _currhint "Define your search criteria. Red boxes signify parameters other than default chosen"
           set _active_dialog .crit
           check_menus
           set exitstatus [selectcrit]
           if {$exitstatus == 0} {
             loadvars
           }
           set _active_dialog ""
           check_menus
           set _currhint "Use the Task menu to redefine criteria, display results, download data"
        }
     .menu.prog.m add separator
     .menu.prog.m add command -label "Retrieve Headers" -underline 9 \
         -state disabled -command { 
           check_menus
           hdr_load       
           check_menus   
        }
     .menu.prog.m add command -label "Plot Search..." -underline 0 \
        -state disabled -command {
           set _currhint "Choose either a Postcript or interactive screen plot"
           set _active_dialog .plot
           check_menus
           plot
           set _active_dialog ""
           check_menus
           set _currhint "Use the Task menu to redefine criteria, display results, download data"

        }
     .menu.prog.m add command -label "Download Data..." -underline 0 \
        -state disabled -command {
           set _currhint "Download data from the CD to an ASCII format"
           set _active_dialog .dl
           check_menus
           download
           set _active_dialog ""
           check_menus
           set _currhint "Use the Task menu to redefine criteria, display results, download data"

        }

menubutton .menu.summary -text "Summary" -menu .menu.summary.m \
   -pady 2 -underline 0 -state disabled

   menu .menu.summary.m -tearoff 0
     .menu.summary.m add command -label "by Source (concise)" -underline 4 \
        -command {sum_update lss; raise .summary} 
     .menu.summary.m add command -label "by Survey (complete)" -underline 4 \
        -command {sum_update lsl; raise .summary}

menubutton .menu.win -text "Window" -menu .menu.win.m -pady 2 -underline 0
   menu .menu.win.m -tearoff 0
      .menu.win.m add command -label "Cascade" -underline 0 -command {
         casc_wins
      }
 
menubutton .menu.help -text "Help" -menu .menu.help.m -pady 2 \
            -underline 0
   menu .menu.help.m -tearoff 0
      .menu.help.m add command -label "Help Contents" -underline 0 -command {
        help $_contents
      }
      .menu.help.m add command -label "About GEODAS" -underline 0 -command {
        about "GEODAS Data Search and Retrieval" geodas.gif
      }

#put the menus up on the menubar
#pack .menu.file .menu.prog .menu.summary .menu.win .menu.help -side left

#The tk_menubar command make things traverse across the menus
#much smoother than without it. Other than that, I don't know nuttin about it.
tk_menuBar .menu .menu.file .menu.prog .menu.summary .menu.win .menu.help


#gauge_create .gauge 100 PaleGreen
#pack .gauge -expand yes -fill both -padx 4

gauge_create .menu.gauge 150 PaleGreen

#pack .menu.file .menu.prog .menu.summary .menu.win .menu.help .menu.gauge -side left

button .menu.cancel -text "Cancel" -underline 0 -state disabled -command {
  if [string match Search $_active_name] {
    catch {close $_active_search}
    delfiles $_key
    set _quit 1
    set _active_search ""
  } elseif [string match Download $_active_name] {
    catch {close $_active_dl}
    set _active_dl ""
  } elseif [string match Headers $_active_name] {
    catch {close $_active_header}
    set _active_header ""
  } elseif [string match Plot $_active_name] {
    catch {close $_active_plot}
    set _active_plot ""
  }
  .menu.cancel configure -state disabled
  tk_dialog .adialog "GEODAS" "$_active_name Canceled!" error 0 "OK"
  set _active_name ""
  check_menus
}
#pack .cancel
pack .menu.file .menu.prog .menu.summary .menu.win .menu.help .menu.gauge .menu.cancel -side left -padx 2



#set up initial screen widgets
frame .bot
label .bot.status -textvariable _key -width 8 \
              -relief sunken -borderwidth 3 -anchor w -padx 3
 
label .bot.hints -textvariable _currhint -relief sunken \
             -borderwidth 3 -anchor w -padx 3

pack .bot -side bottom -fill x
pack .bot.status -side left -anchor w
pack .bot.hints -side right -anchor se -fill x -expand 1



