#!/bin/sh

# /bin/sh interprets this part, but wish ignores it...
#\
exec wish "$0" "$@"


#GEODAS Launch Pad
#Written by: 
#    Dan Metzger


#global havegrd


#set directory variables
set _pathconfig ""
if [file readable ./geodas.cfg] {
  set _pathconfig "."
} elseif [file readable $env(HOME)/gdas/geodas.cfg] {
  set _pathconfig $env(HOME)/gdas
} elseif [file readable $env(HOME)/geodas/geodas.cfg] {
  set _pathconfig $env(HOME)/geodas
} else {
  set temp $env(PATH)
  regsub  -all ":" $temp " " temp ;#make path into list
  foreach i $temp {
    if [file readable $i/geodas.cfg] {
      set _pathconfig $i
      break
    }
  }
}

 #puts "_pathconfig:$_pathconfig"
#sourceing geodas.cfg gives us:
#  _settings
#  _paths
#  _databases
#  _headGRD
#  _options

if [file readable $_pathconfig/geodas.cfg] {
  source $_pathconfig/geodas.cfg   
} else {
   tk_messageBox -message "Could not find a geodas.cfg in current or in PATH"
   exit
}

set env(LD_LIBRARY_PATH) $_paths(lib)

catch {
  tk_setPalette $_settings(colorscheme)
}

if [string match ~ [string range $_paths(work) 0 0]] {
  set _paths(work) "[glob ~][string range $_paths(work) 1 end]"
}
if {![file isdirectory $_paths(work)]} {
   file mkdir $_paths(work)
}

set auto_path "$_paths(prog) $auto_path" ;#extend auto_path to include program (tcl) files

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

#initialize the main window
wm title . "GEODAS Launch Pad" 
wm geometry . "+0+0"
wm iconbitmap . "@$_paths(images)/geodas.ico"
wm protocol . WM_DELETE_WINDOW {
  exit
}


  image create photo gdicon_sr -file $_paths(images)/gdicon-sr.gif
  image create photo gdicon_cr -file $_paths(images)/gdicon-cr.gif
  image create photo gdicon_gl -file $_paths(images)/gdicon-gl.gif
  image create photo gdicon_gt -file $_paths(images)/gdicon-gt.gif
  image create photo gdicon_gb -file $_paths(images)/gdicon-gb.gif
  image create photo gdicon_et -file $_paths(images)/gdicon-et.gif
  image create photo gdicon_ce -file $_paths(images)/gdicon-ce.gif
  image create photo gdicon_hp -file $_paths(images)/gdicon-hp.gif
  image create photo gdicon_help -file $_paths(images)/gdicon-help.gif

  set have_sr 0
  set have_cr 0
  set have_gl 0
  set have_gb 0
  set have_et 0
  set have_gt 0
  set have_hp 0

  if [info exists _settings(database)] {
    set have_sr 1
  }

  if [info exists _databases(grdcrm)] {
    set have_cr 1
    set have_gt 1
  }

  if [info exists _databases(grdglb)] {
    set have_gl 1
    set have_gt 1
  }

  if [info exists _databases(grdgeb)] {
    set have_gb 1
    set have_gt 1
  }

  if [info exists _databases(grdet2)] {
    set have_et 1
    set have_gt 1
  }

  if [file exists "$_paths(home)/geodas-hydroplot"] {
    set have_hp 1
  }


  frame .launchbar -bd 1 -relief raised

  if [string match 1 $have_sr] {
    button .launchbar.search -image gdicon_sr -command {
      set command {$_paths(home)/geodas-search &}
      eval exec $command
    }
  pack .launchbar.search -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }

  if [string match 1 $have_cr] {
    button .launchbar.coastal -image gdicon_cr -command {
      #first check to see if the cd is loaded
      set response $_paths(cd)
      set msg "Please Load NGDC Coastal Relief Model GEODAS SETUP cd"
      while {![file readable $response/startcrm.htm]} {
        set result [my_inputdialog GEODAS $msg response]
        if {$result} {  ;#if OK pressed
          set response [string trimright $response /]
          set _paths(cd) $response
        } else {        ;#if Cancel pressed
          return 0
        }
      } ;#while
      #tk_messageBox -message "Found CD!"
      if [file exists "$_paths(cd)/grddas01/"] {
        set thepath $_paths(work)
        set volid grddas01
      } elseif [file exists "$_paths(cd)/grddas02/"] {
        set thepath $_paths(work)
        set volid grddas02
      } elseif [file exists "$_paths(cd)/grddas03/"] {
        set thepath $_paths(cd)
        set volid grddas03
      } elseif [file exists "$_paths(cd)/grddas04/"] {
        set thepath $_paths(cd)
        set volid grddas04
      } elseif [file exists "$_paths(cd)/grddas05/"] {
        set thepath $_paths(cd)
        set volid grddas05
      } elseif [file exists "$_paths(cd)/grddas06/"] {
        set thepath $_paths(cd)
        set volid grddas06
      } elseif [file exists "$_paths(cd)/grddas07/"] {
        set thepath $_paths(cd)
        set volid grddas07
      } elseif [file exists "$_paths(cd)/grddas08/"] {
        set thepath $_paths(cd)
        set volid grddas08
      } else {
        tk_dialog .adialog "UNABLE TO START COASTAL RELIEF MODEL!" \
        "No-find:$_paths(cd)/grddasnn/\nIs your CD Installed?" question 0 "OK"
        return 0
      }
      if {[string match grddas01 $volid] || [string match grddas02 $volid]} {  ;#older
        if [catch {open $_paths(support)/$volid.htm r} filein] {
          tk_dialog .adialog "UNABLE TO OPEN GEODAS FILE!" \
          "unable to read file: $_paths(support)/$volid.htm\nCheck file permissions" question 0 "OK"
          return 0
        }
        if [catch {open $_paths(work)/startcrm.htm w 0755} fileout] {
          tk_dialog .adialog "UNABLE TO CREATE GEODAS FILE!" \
          "unable to create file: $_paths(work)/startcrm.htm\nCheck permissions" question 0 "OK"
          return 0
        }
        while {![eof $filein]} {
          gets $filein str
          if [string match "var pathcd" [string range $str 0 9]] {
            set str "var pathcd = \"$_paths(cd)/\""
          } elseif [string match "<img src=" [string range $str 0 8]] {
            set str "<img src=\"$_paths(cd)/$volid/$volid.jpg\""
          }
          puts $fileout $str
        }
        close $filein
        close $fileout
      }
      set command "$_settings(browser) $thepath/startcrm.htm &"
      eval exec $command
      timed_alert "$_settings(browser) will now load\nCoastal Relief Model Web Page" 4000
    }
  pack .launchbar.coastal -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }

  if [string match 1 $have_gl] {
    button .launchbar.lakes -image gdicon_gl -command {
      #first check to see if the cd is loaded
      set response $_paths(cd)
        set msg "Please Load Great Lakes Bathymetry GEODAS SETUP cd"
      while {![file readable $response/startglb.htm]} {
        set result [my_inputdialog GEODAS $msg response]
          if {$result} {  ;#if OK pressed
          set $_paths(cd) response
        } else {        ;#if Cancel pressed
          return 0
        }
      } ;#while
      #tk_messageBox -message "Found CD!"
      set command {$_settings(browser) $_paths(cd)/startglb.htm &}
      eval exec $command
      timed_alert "$_settings(browser) will now load\nGreat Lakes Bathymetry Web Page" 4000
    }
  pack .launchbar.lakes -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }


  if [string match 1 $have_gb] {
      button .launchbar.gebco -image gdicon_gb -command {
      #first check to see if the cd is loaded
        set response $_paths(cd)
      set msg "Please Load GEBCO Worldwide Bathymetry GEODAS SETUP cd"
        while {![file readable $response/startgeb.htm]} {
        set result [my_inputdialog GEODAS $msg response]
        if {$result} {  ;#if OK pressed
            set $_paths(cd) response
        } else {        ;#if Cancel pressed
          return 0
        }
      } ;#while
      #tk_messageBox -message "Found CD!"
      set command {$_settings(browser) $_paths(cd)/startgeb.htm &}
      eval exec $command
      timed_alert "$_settings(browser) will now load\nGEBCO Worldwide Bathymetry Web Page" 4000
    }
  pack .launchbar.gebco -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }

  if [string match 1 $have_et] {
    button .launchbar.sands -image gdicon_et -command {
      #first check to see if the cd is loaded
      set response $_paths(cd)
      set msg "Please Load ETOPO2 Global Elevation Data GEODAS SETUP cd"
      while {![file readable $response/startet2.htm]} {
        set result [my_inputdialog GEODAS $msg response]
        if {$result} {  ;#if OK pressed
          set $_paths(cd) response
        } else {        ;#if Cancel pressed
          return 0
        }
      } ;#while
      #tk_messageBox -message "Found CD!"
      set command {$_settings(browser) $_paths(cd)/startet2.htm &}
      eval exec $command
      timed_alert "$_settings(browser) will now load\nETOPO2 Global Elevation Data Web Page" 4000
    }
  pack .launchbar.sands -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }

  if [string match 1 $have_gt] {
    button .launchbar.gridtran -image gdicon_gt -command {
      set command {$_paths(home)/geodas-gridtran &}
      eval exec $command
    }
  pack .launchbar.gridtran -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }

  button .launchbar.coastex -image gdicon_ce -command {
    set command {$_paths(home)/geodas-coastex &}
    eval exec $command
    }
  pack .launchbar.coastex -side left -ipadx 2 -ipady 2 -padx 2 -pady 2

  if [string match 1 $have_hp] {
    button .launchbar.hydroplot -image gdicon_hp -command {
      set command {$_paths(home)/geodas-hydroplot &}
      eval exec $command
    }
  pack .launchbar.hydroplot -side left -ipadx 2 -ipady 2 -padx 2 -pady 2
  }

  button .launchbar.help -image gdicon_help -command {
    help $_contents
  }

  pack .launchbar.help -side left -ipadx 2 -ipady 2 -padx 2 -pady 2

  pack .launchbar

  if [string match 1 $have_sr] {
    bind .launchbar.search <Enter> {
      tooltip .launchbar.search {GEODAS Data Search and Retrieval}
    }
  }

  if [string match 1 $have_cr] {
    bind .launchbar.coastal <Enter> {
      tooltip .launchbar.coastal {NGDC Coastal Relief Model}
    }
  }

  if [string match 1 $have_gl] {
    bind .launchbar.lakes <Enter> {
      tooltip .launchbar.lakes {Great Lakes Bathymetry}
    }
  }

  if [string match 1 $have_gb] {
    bind .launchbar.gebco <Enter> {
      tooltip .launchbar.gebco {GEBCO Worldwide Bathymetry Grids}
    }
  }

  if [string match 1 $have_et] {
    bind .launchbar.sands <Enter> {
      tooltip .launchbar.sands {ETOPO2 Global Elevation Grids}
    }
  }

  if [string match 1 $have_gt] {
    bind .launchbar.gridtran <Enter> {
      tooltip .launchbar.gridtran {GEODAS Grid Translator}
    }
  }

  bind .launchbar.coastex <Enter> {
    tooltip .launchbar.coastex {GEODAS Coastline Extractor}
  }

  if [string match 1 $have_hp] {
    bind .launchbar.hydroplot <Enter> {
      tooltip .launchbar.hydroplot {GEODAS Hydro-Plot}
    }
  }

  bind .launchbar.help <Enter> {
    tooltip .launchbar.help {GEODAS Help System}
  }




