#!/bin/sh

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


proc install_alert {notice} {
   catch {destroy .alert}
   toplevel .alert
   wm title .alert "GEODAS"
    set x [expr [winfo screenwidth .alert]/2 - [winfo reqwidth .alert]/2]
#	    - [winfo vrootx [winfo parent .alert]]]
    set y [expr [winfo screenheight .alert]/2 - [winfo reqheight .alert]/2]
#	    - [winfo vrooty [winfo parent .alert]]]
    wm geom .alert +$x+$y

   frame .alert.t
   frame .alert.b -relief sunken -borderwidth 2
   pack .alert.t .alert.b -padx 3 -pady 3

   #use a text box so that it doesn't do weird wrapping that msg boxes use
   text .alert.t.msg -relief sunken \
      -yscroll ".alert.t.scroll set" -width 60 -wrap word -height 21 \
      -highlightthickness 0
   scrollbar .alert.t.scroll -command ".alert.t.msg yview" -highlightthickness 0
   .alert.t.msg delete 1.0 end
   .alert.t.msg insert end $notice
   .alert.t.msg config -state disabled

   pack .alert.t.msg .alert.t.scroll -side left -fill y 

   button .alert.b.ok -text "OK" -highlightthickness 2 -command {
     grab release .alert;destroy .alert
   }
   pack .alert.b.ok -side bottom   
   bind .alert <Return> { .alert.b.ok invoke }

   tkwait visibility .alert
   grab set .alert
   update
   #raise .alert
   focus .alert
   tkwait window .alert
}



#############################
### run the setup program ###
#############################

wm title . "GEODAS for Xwindows Setup"

 #puts "tcl_platform(os):$tcl_platform(os)"
  #tk_messageBox -message "argc:$argc| argv:$argv|"
  #tk_messageBox -message "argv0:$argv0|"

set softversion "4.1.16"

#set default GridTranslator settings (can be overwritten with geodas.cfg values)
set _headGRD(version) "1000000001"
set _headGRD(length) "128"
set _headGRD(dataType) "1"
set _headGRD(latDeg) "44"
set _headGRD(latMin) "0"
set _headGRD(latSec) "0"
set _headGRD(latCellSize) "6"
set _headGRD(latNumCells) "1201"
set _headGRD(lonDeg) "-70"
set _headGRD(lonMin) "-30"
set _headGRD(lonSec) "0"
set _headGRD(lonCellSize) "6"
set _headGRD(lonNumCells) "1201"
set _headGRD(minValue) "0"
set _headGRD(maxValue) "0"
set _headGRD(gridRadius) "-1"
set _headGRD(precision) "1"
set _headGRD(NaNvalue) "999999"
set _headGRD(numType) "4"
set _headGRD(waterDatum) "0"
set _headGRD(dataLimit) "0"
set _headGRD(unused1) "0"
set _headGRD(unused2) "0"
set _headGRD(unused3) "0"
set _headGRD(unused4) "0"
set _headGRD(unused5) "0"
set _headGRD(unused6) "0"
set _headGRD(unused7) "0"
set _headGRD(unused8) "0"
set _headGRD(unused9) "0"
set _headGRD(unused10) "0"
set _headGRD(unused11) "0"

set _options(dataFormat) "dfGRD"
if [string match littleEndian $tcl_platform(byteOrder)] {
  set _options(byteOrder) "boLITTLEENDIAN"
} else {
  set _options(byteOrder) "boBIGENDIAN"
}
set _options(headerStyle) "hsGRDNGDC"
set _options(emptyCells) "ecNONE"
set _options(recordDelimiter) "rdNONE"
set _options(dataExtent) "deALL"

set _settings(browser) "netscape"
set _settings(print) "lpr"
set _settings(colorscheme) "None"


set doneSoft 0
set doneCoast 0
set doneInv 0

set doSoftEnabled 1
set doCoastEnabled 1
set doInvEnabled 1

 #puts "env(HOME):$env(HOME)"


#if this program is geodas-setup, then software is already installed and
#this is being run to re-configure GEODAS
if {[string first geodas-setup $argv0] != -1} {
  set thisrun geodas-setup
} else {
  set thisrun geodas-install
}

set installmess "GEODAS items installed:\n"

set cdpaths(cd) "/mnt/cdrom"
set pathconfig "/usr/local/bin"

if [file writable /usr/local/bin] {
  set pathconfig /usr/local/bin
  set pathinstall /usr/local/bin
} else {
  set pathconfig [glob ~]/geodas
  set pathinstall [glob ~]/geodas
}


#now check for file geodas.cfg from previous Installation
set oldinitfile "nada"
if [file readable $env(HOME)/geodas/geodas.cfg] {
  set pathconfig $env(HOME)/geodas
  set oldinitfile geodas.cfg
} 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
      set oldinitfile geodas.cfg
      break
    }
  }
}
if [string match "nada" $oldinitfile] {
  if [file readable ./geodas.cfg] {
    set pathconfig "."
    set oldinitfile geodas.cfg
  }
}

#PROBABLY NEED TO SWITCH pathinstall to whatever is in the _paths(home) in geodas.cfg file ,if found
#i.e. _paths(home) is where executable is, and where new geodas.cfg would go during "install"
#if "setup" then the geodas.cfg could be in the user's directory

 #puts "pathconfig:$pathconfig"

#expand . if found
if [string match . [string range $pathconfig 0 0]] {
  if {[string length $pathconfig] > 1} {
    set pathconfig "[pwd][string range $pathconfig 1 end]"
  } else {
    set pathconfig [pwd]
  }
}

 #puts "oldinitfile:$oldinitfile  pathconfig:$pathconfig"

set msg "Welcome to GEODAS Install/Setup.\n\nThis will install GEODAS Version $softversion onto\
your system. If you already have GEODAS installed, this will update the installation and/or add\
additonal GEODAS components. Version control will insure that files will only be replaced by\
newer versions, so you can safely run this program.\n\n\
Note: If you have an older version of GEODAS Installed, and you wish to change file locations,\
you should exit Install and remove all the old GEODAS files first."

install_alert $msg

if ![string match nada $oldinitfile] {
  set msg "GEODAS Software has been detected on your system."
  if [string match geodas-install $thisrun] {
    set msg "$msg GEODAS software will be Version $softversion"
  } else {
    set msg "$msg Setup will update/configure GEODAS configuration file: $pathconfig/geodas.cfg"
  }
} else {
  if [string match geodas-setup $thisrun] {
    set msg "ERROR, Unable to find GEODAS configuration file geodas.cfg in your PATH. Re-install GEODAS"
    tk_dialog .adialog "GEODAS SETUP" $msg error 0 "OK"
    exit
  }
  set msg "No previous GEODAS installation found in your path\nSetup will install GEODAS System Version $softversion"
}

set choice [tk_dialog .adialog "GEODAS SETUP" $msg info 0 "OK" "Exit Setup"]
if {$choice == 1} {exit}

if {[string match geodas.cfg $oldinitfile]} {
  source $pathconfig/$oldinitfile
  if [string match geodas-install $thisrun] {
    if [info exists _paths(home)] {
      set pathinstall $_paths(home)
      set pathconfig $_paths(home)
    } else {
      set pathinstall $pathconfig
    }
  } elseif [string match geodas-setup $thisrun] {
    set pathinstall $_paths(home) ;#could be different from pathconfig
  }
  if [info exists _paths(cd)] {
    set cdpaths(cd) $_paths(cd)
  }
  #for gridded databases, add ngdcprefixes items if missing
  foreach dbase [lsort [array names _blocksizes]] {
    if ![info exists _ngdcprefixes($dbase)] {
      switch $dbase {
        grdcrm {set aprefix gn}
        grdglb {set aprefix gn}
        grdet2 {set aprefix ge}
        grdgeb {set aprefix gb}
        default {
          tk_messageBox -message "bad database:$dbase Contact NGDC"
          exit
        }
      } ;#switch
      set _ngdcprefixes($dbase) $aprefix
    } ;#if
  } ;#foreach
} ;#if

#old code, may be useful to save
#} elseif [string match geodas.ini $oldinitfile] {
#  set fileid [open $pathconfig/geodas.ini r]
#  gets $fileid str
#  set str [string trim $str]
#  while {![eof $fileid]} {
#    if { [string range $str 0 0]!="#" && [string length $str] > 1 } {
#      if {[string range $str 0 0]=="\["} {
#        set currsection [string trim $str "\[\]"]
#        #tk_messageBox -message "currsection:$currsection"
#      } elseif {[string match $currsection databases]} { ;#add any previous databases
#          #puts "$currsection got:$str"
#          regexp -nocase {([a-z]+)\=(.+)} $str namevalue name value
#          #puts "$name -> $value"
#          if [string match default $name] {
#            set _settings(database) $value
#          } else {
#            set _databases($name) $value ;#we must save other database
#            #puts "now _databases($name)=$_databases($name)"
#          }
#      } elseif {[string match $currsection paths]} {
#          #puts "$currsection got:$str"
#          regexp -nocase {([a-z]+)\=(.+)} $str namevalue name value
#          #puts "$name -> $value"
#          if ![string match carter $name] { ;#ignore carter
#            set _paths($name) [string trimright $value /]
#          }
#      }
#    } ;#if "["
#  gets $fileid str
#  set str [string trim $str]
#  } ;#while
#  close $fileid
#  #remove /gdas/bin if on end of pathconfig
#  set gdasadded [string first /gdas $pathconfig]
#  if ![string match -1 $gdasadded] {
#    set pathconfig [string range $pathconfig 0 [expr $gdasadded - 1]]
#  }
#  set pathinstall $pathconfig
#} ;#if
#END old  code

 #puts "pathconfig:$pathconfig"
 #puts "pathinstall:$pathinstall"
#i think the idea is that users with their own config can change options, but
#they cannot install any new inventories or coastlines
if ![string match $pathconfig $pathinstall] {
  set doCoastEnabled 0
  set doInvEnabled 0
}

#if ![info exists _paths(support)] {
#  if [info exists _paths(prog)] {
#    #remove /bin from end
#    if [string match */bin $_paths(prog)] {
#      set ndx [expr [string last /bin $_paths(prog)] - 1]
#      set _paths(support) [string range $_paths(prog) 0 $ndx]
#    }
#  }
#}

#puts "glob ~:[glob ~]"

if ![info exists _paths(support)] {  ;#i.e. not found in geodas.cfg file
  switch $tcl_platform(os) {
    Linux {
      if [file writable /usr/local/geodas] {
        set _paths(support) /usr/local/geodas
      } else {
        set _paths(support) [glob ~]/geodas
      }
    }
    SunOS {
      if [file writable /usr/local/geodas] {
        set _paths(support) /opt/geodas
      } else {
        set _paths(support) [glob ~]/geodas
      }
    }
    Darwin {
      if [file writable /usr/local/geodas] {
        set _paths(support) /opt/geodas
      } else {
        set _paths(support) [glob ~]/geodas
      }
    }
    default {
      if [file writable /usr/local/geodas] {
        set _paths(support) /opt/geodas
      } else {
        set _paths(support) [glob ~]/geodas
      }
    }
  } ;#switch
}


 #puts "_paths(support):$_paths(support)"

if ![info exists _paths(work)] {
  set _paths(work) ~/geodas/work
}


frame .options -relief groove -borderwidth 1

label .options.labsys -text "Your Operating System:"
radiobutton .options.linux -text "Linux-x86" -variable opsys -value linux
radiobutton .options.solaris -text "Solaris" -variable opsys -value solaris
radiobutton .options.darwin -text "Mac OS-X" -variable opsys -value darwin
radiobutton .options.unix -text "Source Installation" -variable opsys -value unix
pack .options.labsys -anchor w
pack .options.linux -padx 12 -anchor w
pack .options.solaris -padx 12 -anchor w 
pack .options.darwin -padx 12 -anchor w
pack .options.unix -padx 12 -anchor w


frame .controls
button .controls.ok -text "Next >>" -command {
  query_computer
}
button .controls.cancel -text "Exit Setup" -command {
  if {$doneSoft || $doneCoast || $doneInv} {
    if {$doneSoft && [string match unix $opsys]} {
      set msg "GEODAS Install Program Complete!\n\nTo Finish Installation you must compile binaries.\
See readme.txt in subdirectory $_paths(source).\n\nBe sure to delete directory geodas_install."
    } else {
      set msg "GEODAS Install/Setup Program Complete!\n\n\
To run GEODAS, users can simply type geodas at the command prompt.\
(Be sure program geodas is in user's PATH environment variable)\n\n > geodas\n\n\
You can add additional GEODAS cd sets by running program geodas-setup at any time.\n\n\
To run Grid Translator (Design-a-Grid) directly from your browser you must set your Browser MIME type.\n\n\
For information and instructions see file $_paths(support)/readme.txt or the GEODAS Help System"
      if [string match geodas-install $thisrun] {
      set msg "$msg\n\nBe sure to delete subdirectory geodas_install."
      }
    }
  } else {
    set msg "GEODAS Install/Setup Program Complete!\n\n No new GEODAS components added."
  }
  install_alert $msg
  exit
}
button .controls.help -text "Help" -command {
set msg "GEODAS Install/Setup.\n\nChoose the appropriate installation for your\
operating system. To use the Source Installation you need to compile the Fortran and\
C++ source code after this installation.  See readme.txt for explicit instructions."
  install_alert $msg
}

pack .controls.ok -side left -expand yes
pack .controls.cancel -side left -expand yes
pack .controls.help -side left -expand yes

frame .feed -relief groove -borderwidth 1
scrollbar .feed.vsb -command ".feed.txt yview"
text .feed.txt -width 68 -height 24 -yscrollcommand ".feed.vsb set"
pack .feed.vsb -side right -fill y
pack .feed.txt -side left -fill both  -expand yes

pack .options -side top -fill x
pack .controls -pady 4 -fill x
pack .feed -pady 4 -fill x

update

set x [expr [winfo screenwidth .]/2 - [winfo reqwidth .]/2]
set y [expr [winfo screenheight .]/2 - [winfo reqheight .]/2]
wm geom . +$x+$y

switch $tcl_platform(os) {
  Linux {
    .options.linux invoke
    .options.solaris configure -state disabled
    .options.darwin configure -state disabled
  }
  SunOS {
    .options.solaris invoke
    .options.linux configure -state disabled
    .options.darwin configure -state disabled
  }
  Darwin {
    .options.darwin invoke
    .options.linux configure -state disabled
    .options.solaris configure -state disabled
  }
  default {
    .options.solaris configure -state disabled
    .options.linux configure -state disabled
    .options.darwin configure -state disabled
    .options.unix invoke
  }
} ;#switch 

#tk_messageBox -message "up"



###########################
### proc query_computer ###
###########################
proc query_computer {} {

global env
global tcl_platform
global env
global thisrun
global _settings
global _paths


if [string match geodas-install $thisrun] {
  set imagespath "./images"
} else {
  set imagespath "$_paths(images)"
}


  # parray tcl_platform
 #tk_messageBox -message "proc query_computer"
pack forget .options.labsys
pack forget .options.linux
pack forget .options.solaris
pack forget .options.darwin
pack forget .options.unix

frame .options.f1
label .options.f1.labintro -text "" -anchor e
label .options.f1.mess -text "Choose Paths for your Installation" -anchor e -pady 2 -padx 30
pack .options.f1.labintro .options.f1.mess -side left -fill x

frame .options.f2
label .options.f2.labpcd -text "Path to GEODAS SETUP CD:" -anchor e
entry .options.f2.entpcd -width 40 -textvariable cdpaths(cd)
pack .options.f2.entpcd .options.f2.labpcd -side right -fill x

frame .options.f3
label .options.f3.labpins -text "Path to GEODAS Executables:" -anchor e
entry .options.f3.entpins -width 40 -textvariable pathinstall ;#Executables
pack .options.f3.entpins .options.f3.labpins -side right -fill x

frame .options.f4
label .options.f4.labpsup -text "Path to GEODAS Support Files:" -anchor e
entry .options.f4.entpsup -width 40 -textvariable _paths(support)
pack .options.f4.entpsup .options.f4.labpsup -side right -anchor w -fill x

frame .options.f5
label .options.f5.labpwrk -text "Path to User Work Files:" -anchor e
entry .options.f5.entpwrk -width 40 -textvariable _paths(work)
pack .options.f5.entpwrk .options.f5.labpwrk -side right -anchor w -fill x

frame .options.f6
label .options.f6.labbrow -text "Browser Command:" -anchor e
entry .options.f6.entbrow -width 40 -textvariable _settings(browser)
pack .options.f6.entbrow .options.f6.labbrow -side right -anchor w -fill x

frame .options.f7
label .options.f7.labprint -text "Print Command:" -anchor e
entry .options.f7.entprint -width 40 -textvariable _settings(print)
pack .options.f7.entprint .options.f7.labprint -side right -anchor w -fill x

frame .options.f8
label .options.f8.labscheme -text "Color Scheme:" -anchor e
tk_optionMenu .options.f8.scheme _settings(colorscheme) \
     None ivory bisque lavender LightCyan LightGrey LightSkyBlue LightSlateGray\
                LightSteelBlue MistyRose PaleTurquoise pink RosyBrown seashell thistle wheat white
pack .options.f8.scheme .options.f8.labscheme -side right -anchor w -fill x -pady 4

pack .options.f1 .options.f2 .options.f3 .options.f4 .options.f5 .options.f6 .options.f7 .options.f8 -side top -anchor e -padx 6


.controls.ok configure -command {
  init_paths
}
 
.controls.help configure -command {
set msg "GEODAS Install/Setup.\n\nTo run GEODAS you will need to install GEODAS Executables and\
Support files in subdirectories for which users have read/execute permissions.\
GEODAS Support files include functions, help, source, images, coastlines, and data inventories,\
and will be placed in subdirectory /geodas-support-path containing subdirectories\
/bin /help /images /source /coast and /inv.  Depending on your GEODAS product, these support files\
may include GEODAS Coastlines and Database Inventories,\
which you will have the option of running directly from the GEODAS CDs.\n\n\
When executed, GEODAS programs create various \"work\" files, including the user's output data files.\
The default work path  is ~/geodas/work, allowing multiple users to have their own work space. This can be\
changed as desired.\n\nIf necessary, change the commands to launch Browser and print files.\n\n\
Note: If you have an older version of GEODAS Installed, and you wish to change GEODAS file locations,\
you should exit this Install and remove all the old GEODAS files first."
install_alert $msg
}
 
if [string match geodas-setup $thisrun] {
  .options.f3.entpins config -state disabled
  .options.f4.entpsup config -state disabled
}


} ;#proc query_computer




#######################
### proc init_paths ###
#######################
proc init_paths {} {

global pathinstall
global pathconfig
global cdpaths
global tcl_platform
global _settings
global _paths
global _databases
global cdvolume
global cdvolid
global cdvolumes
global cdvolnums
global cdvolids
global cddbase
global cdversion
global doSoft
global doCoast
global doInv
global doSoftEnabled
global doCoastEnabled
global doInvEnabled
global thisrun
global _blocksizes
global _latcellsizes
global _loncellsizes
global _ngdcprefixes
 #tk_messageBox -message "proc init_paths"
 #puts "init_paths start pathinstall:$pathinstall pathconfig:$pathconfig"
 
set cdpaths(cd) [string trimright $cdpaths(cd) /]

if {![file exists $cdpaths(cd)/setup] && ![file exists $cdpaths(cd)/software/geodas]} {
  tk_dialog .adialog "GEODAS SETUP CD not Found!" \
  "Please be sure GEODAS SETUP CD is mounted\nand it's Path is set correctly" error 0 "OK"
  return 0
}

if [catch {open $cdpaths(cd)/cdrdas.vol r} fileid] {
  tk_dialog .adialog "GEODAS SETUP CD Not Found!" \
  "Unable to open:$cdpaths(cd)/cdrdas.vol \nPlease be sure GEODAS SETUP CD is mounted\n\
and it's Path is set correctly" error 0 "OK"
  return 0
} else {
  set checklcase [glob -nocomplain $cdpaths(cd)/gdas/*.b00]
  #puts "checklcase b00:{$checklcase}|"
  set checkucase [glob -nocomplain $cdpaths(cd)/GDAS/*.B00]
  #puts "checkucase B00:{$checkucase}|"
  if {[llength $checklcase] != 0} {
    #tk_messageBox -message "CD is Lowercase"
    set _settings(casecd) lowercase
  } elseif {[llength $checkucase] != 0} {
    #tk_messageBox -message "CD is Uppercase"
    set _settings(casecd) uppercase
  } else {
    tk_messageBox -message "Unable to see Coastlines files!"
    set _settings(casecd) lowercase
  }

  gets $fileid cdvolume ;#read cdrdas.vol file for HYDDAS01.V41, HYDNOS00.V41, GRDDAS03.V41, GRDDAS02.V10, GRDCRM00.V41, etc.
  set cdversion [string tolower [string range $cdvolume 9 11]]  ;# v41 v10
  set cdvolid [string tolower [string range $cdvolume 0 7]]     ;# hyddas01 grddas03 grdcrm00 trkdas00 cdcdasxx
  set cddbase [string tolower [string range $cdvolume 0 5]]     ;# trkdas hyddas hydnos cdcdas
  #in some cases, fix database from cdvolid
  if [string match hyddas01 $cdvolid] {
    set cddbase hydnos
  }
  if [string match grddasg? $cdvolid] {
    set cddbase grdglb
  } elseif [string match grddasw1 $cdvolid] {
    set cddbase grdgeb
  } elseif [string match grddase1 $cdvolid] {
    set cddbase grdet2
  } elseif [string match grddas?? $cdvolid] {
    set cddbase grdcrm
  }
  if [string match mbbdas01 $cdvolid] {
    set cddbase trkmec
  }
  if [string match arodasm1 $cdvolid] {
    set cddbase aromin
  }
  if [string match arodasn1 $cdvolid] {
    set cddbase aronur
  }
  #puts "cddbase:$cddbase"
  if {[string match trk* $cddbase] || [string match hyd* $cddbase]} {
    if ![string match v4* $cdversion] {
      tk_dialog .adialog "GEODAS SETUP CD is wrong version!" \
      "Wrong version of GEODAS CD at: $cdpaths(cd)\nYou will need GEODAS CD Version 4.x" error 0 "OK"
      close $fileid
      return 0
    }
  }
  if [string match 00 [string range $cdvolid 6 7]] {   ;#new DVD multi-volume standard
    while {![eof $fileid]} {
      gets $fileid cdnextvol ;#read cdrdas.vol file for actual volumes
      if {![eof $fileid]} {
        lappend cdvolumes $cdnextvol ;#GRDDAS05.V41
        lappend cdvolnums [string range $cdnextvol 6 7] ;#05
        lappend cdvolids [string tolower [string range $cdnextvol 0 7]] ;#grddas05!!!
      }
    }
  } else {
    lappend cdvolumes $cdvolume ;#GRDDASE1.V41
    lappend cdvolnums [string range $cdvolume 6 7]  ;#E1
    lappend cdvolids $cdvolid  ;#grddase1
  }
 #puts "cddbase:$cddbase"
 #puts "cdvolume:$cdvolume  cdvolumes:$cdvolumes"
 #puts "cdvolnums:$cdvolnums"
 #puts "cdvolid:$cdvolid  cdvolids:$cdvolids"
  close $fileid
} ;#if/else

if [string match littleEndian $tcl_platform(byteOrder)] {
  if [string match uppercase $_settings(casecd)] {
    set cdpaths(coast) "$cdpaths(cd)/GDAS"
    set cdpaths(inv) "$cdpaths(cd)/GDAS"
  } else {
    set cdpaths(coast) "$cdpaths(cd)/gdas"
    set cdpaths(inv) "$cdpaths(cd)/gdas"
  }
} elseif [string match bigEndian $tcl_platform(byteOrder)] {
  if [string match uppercase $_settings(casecd)] {
    set cdpaths(coast) "$cdpaths(cd)/GDAS/BIGENDIA"
    set cdpaths(inv) "$cdpaths(cd)/GDAS/BIGENDIA"
  } else {
    set cdpaths(coast) "$cdpaths(cd)/gdas/bigendia"
    set cdpaths(inv) "$cdpaths(cd)/gdas/bigendia"
  }
  #if [file exists $cdpaths(cd)/gdas/bigendia] {
  #  set cdpaths(coast) "$cdpaths(cd)/gdas/bigendia"
  #  set cdpaths(inv) "$cdpaths(cd)/gdas/bigendia"
  #} elseif [file exists $cdpaths(cd)/setup/xwindows/inv] {
  #  set cdpaths(coast) "$cdpaths(cd)/setup/xwindows/coast"
  #  set cdpaths(inv) "$cdpaths(cd)/setup/xwindows/inv"
  #}
} else {
  tk_messageBox -message "ERROR, cannot determine Byte Order"
  return 0
}

set pathinstall [string trimright $pathinstall /]
#delete /gdas or /gdas/bin if appended
set gdasadded [string first /gdas $pathinstall]
if ![string match -1 $gdasadded] {
  set pathinstall [string range pathinstall 0 [expr $gdasadded - 1]]
}

#expand ~/ if found
set ndx [string first / $pathinstall]
if [string match ~ [string range $pathinstall 0 0]] {
  if {$ndx == 1} {
    set pathinstall "[glob ~][string range $pathinstall 1 end]"
  } else {
    set str [string range $pathinstall 0 [expr $ndx - 1]] 
    set pathinstall "[glob $str][string range $pathinstall $ndx end]"
  }
}

if ![file exists $pathinstall] {
  if [catch [file mkdir $pathinstall] result] {
    tk_dialog .adialog "GEODAS Executable Path ERROR" \
    "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper permissions" \
    info 0 "OK"
    . config -cursor {}
    return 0
  }
}

if [string match geodas-install $thisrun] {
  if ![file writable $pathinstall] then {
      tk_dialog .adialog "GEODAS Executable Path ERROR" \
      "$pathinstall Does Not have Write Permissions\n\n\
      Please be sure GEODAS Install Paths are correct\nand have proper permissions" \
      info 0 "OK"
      . config -cursor {}
      return 0
  }
  set pathconfig $pathinstall
}

set _paths(support) [string trimright $_paths(support) /]

#expand ~/ if found
set ndx [string first / $_paths(support)]
if [string match ~ [string range $_paths(support) 0 0]] {
  if {$ndx == 1} {
    set _paths(support) "[glob ~][string range $_paths(support) 1 end]"
  } else {
    set str [string range $_paths(support) 0 [expr $ndx - 1]] 
    set _paths(support) "[glob $str][string range $_paths(support) $ndx end]"
  }
}

 #puts "init_paths now pathinstall:$pathinstall pathconfig:$pathconfig"

pack forget .options.f1 .options.f2 .options.f3 .options.f4 .options.f5 .options.f6 .options.f7 .options.f8

if [string match geodas-install $thisrun] {
  set softmess "GEODAS Software (17.0 MB)"
} elseif [string match geodas-setup $thisrun] {
  set softmess "GEODAS Configuration Changes"
}  

label .options.labwhat -text "Choose items to Install:"
checkbutton .options.cbsoft -text $softmess -variable doSoft
checkbutton .options.cbcoast -text "GEODAS Coastline Files (41.9 MB)" -variable doCoast
checkbutton .options.cbinv -text "GEODAS Inventoriesxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -variable doInv
pack .options.labwhat -anchor w
pack .options.cbsoft -padx 12 -anchor w
pack .options.cbcoast -padx 12 -anchor w 
pack .options.cbinv -padx 12 -anchor w

if [info exists cdpaths(coast)] {
  if ![file exists $cdpaths(coast)/hersey.b00] {
    set doCoastEnabled 0
  }
} else {
  set doCoastEnabled 0
}

if [string match $cddbase trkdas] {
  .options.cbinv configure -text "Marine Trackline Geophysics Inventory (30.2 MB)"
} elseif [string match $cddbase trkmec] {
  .options.cbinv configure -text "US East Coast Multibeam Bathymetry Inventories (3.8 MB)"
} elseif [string match $cddbase aromin] {
  .options.cbinv configure -text "Minnesota Aeromagnetics Inventories (18.8 MB)"
} elseif [string match $cddbase aronur] {
  .options.cbinv configure -text "N.U.R.E. Aeromagnetics Inventories (18.9 MB)"
} elseif [string match $cddbase hydnos] {
  .options.cbinv configure -text "NOS Hydrographic Surveys Inventory (13.2 MB)"
} elseif [string match $cddbase grdcrm] {
  .options.cbinv configure -text "NGDC Coastal Relief Model Volume(s) $cdvolnums (2.1 kb)"
} elseif [string match $cddbase grdglb] {
  .options.cbinv configure \
  -text "Great Lakes Bathymetry Volume(s) $cdvolnums (2.1 kb)"
} elseif [string match $cddbase grdgeb] {
  .options.cbinv configure \
  -text "GEBCO Worldwide Bathymetry Grids (1.2 kb)"
} elseif [string match $cddbase grdet2] {
  .options.cbinv configure \
  -text "ETOPO2 Global Gridded Elevation Data  (1.2 kb)"
} elseif [string match $cddbase cdcdas] {
   set doInvEnabled 0
}

if {$doSoftEnabled == 0} {
  .options.cbsoft configure -state disabled
}

if {$doCoastEnabled == 0} {
  .options.cbcoast configure -state disabled
}

if {$doInvEnabled == 0} {
  .options.cbinv configure -state disabled
}

.controls.ok configure -text Install! -command {
    if [expr ($doSoft && $doSoftEnabled) || ($doCoast && $doCoastEnabled) || ($doInv && $doInvEnabled)] {
      do_copys
    } else {
        tk_messageBox -message "You have Not Selected any Options"
    }
} ;# -command

.controls.help configure -command {
set msg "GEODAS Install/Setup.\n\nTo run GEODAS the GEODAS Software must be Installed.\
GEODAS Coastlines Files can be run directly off the GEODAS SETUP cd, but this will\
slow down screen plots and you will need to have a GEODAS SETUP cd mounted.\
The Marine Trackline Geophysics Inventory, NOS Hydrographic Surveys Inventory,\
Aeromagnetics Inventories and\
Multibeam Bathymetry Inventories can also be run directly off the cd, but this will\
slow GEODAS operation and you will need to have the appropriate GEODAS SETUP cd mounted.\n\n\
GEODAS Gridded Databases such as Coastal Relief Model, Great Lakes Bathymetry and ETOPO2 \
must be installed for them to work.\n\n
You may install additional GEODAS cd sets simply by following the prompts and mounting the\
various GEODAS SETUP cds. You may also install additional cd sets later by running geodas-setup\
from the command prompt, after this installation. "
install_alert $msg
}

} ;# end proc init_paths

#####################
### proc do_copys ###
#####################
proc do_copys {} {

global tcl_platform
global pathinstall
global pathconfig
global cdpaths
global _settings
global _paths
global _databases
global _blocksizes
global _latcellsizes
global _loncellsizes
global _ngdcprefixes
global _headGRD
global _options
global cddbase
global cdvolume
global cdvolid
global cdvolumes
global cdvolnums
global cdvolids
global cdversion
global doSoft
global doCoast
global doInv
global doSoftEnabled
global doCoastEnabled
global doInvEnabled
global doneSoft
global doneCoast
global doneInv
global installmess
global thisrun
global softversion
global oldsoftversion

#set the final paths (if extracted from init file some paths may now be overridden)
 #puts "do_copys pathinstall:$pathinstall pathconfig:$pathconfig"

set _paths(cd) $cdpaths(cd)

if [string match geodas-install $thisrun] {
  #if support path doesn't exist create it
  if ![file exists $_paths(support)] then {
    #puts "try:file mkdir $_paths(support)"
    if [catch [file mkdir $_paths(support)] result] {
      tk_dialog .adialog "GEODAS Support Path ERROR" \
      "$result\n\nPlease Be sure GEODAS Paths are correct and have proper permissions" error 0 "OK"
      return 0
    }
  }
}

if {$doSoft} {
  set _paths(home) $pathinstall
  set _paths(prog) "$_paths(support)/bin" ;#regardless
  set _paths(images) "$_paths(support)/images"
  set _paths(help) "$_paths(support)/help"
  set _paths(source) "$_paths(support)/source"
  set _paths(lib) "$_paths(support)/lib"
  if [info exists _settings(softwareversion)] { ;#from a previous installation
    set oldsoftversion $_settings(softwareversion)
  } else {
    set oldsoftversion "0.0.00"
    set _settings(softwareversion) $softversion ;#new version number
  }
  if [string match 1 [string compare $softversion $oldsoftversion]] { ;#if softversion > oldsoftversion
    set _settings(softwareversion) $softversion
  }
}


set pathlist [array names _paths]

if {$doInv} {
  if [string match grd??? $cddbase] {
    set _paths($cddbase) "$_paths(support)/inv"
  } else {
    set _paths($cddbase) "$_paths(support)/inv"
    if [string match trkm* $cddbase] {  ;#if this is a multbeam cd
      set _paths(trkmbb) "$_paths(support)/inv"
    }
    if [string match aro* $cddbase] {  ;#if this is a arodas cd
      set _paths(arodas) "$_paths(support)/inv"
    }
  }
} elseif ![string match grd??? $cddbase] { ;#accessing from cd, and not grddas
  if {[lsearch -exact $pathlist $cddbase] == -1} { ;#if database was not in geodas config file 
    set _paths($cddbase) $cdpaths(inv)
  }
  if [string match trkm* $cddbase] {  ;#if this is a multbeam cd
    if {[lsearch -exact $pathlist trkmbb] == -1} {
      set _paths(trkmbb) $cdpaths(inv)
    }
  }
  if [string match aro* $cddbase] {  ;#if this is a arodas cd
    if {[lsearch -exact $pathlist arodas] == -1} {
      set _paths(arodas) $cdpaths(inv)
    }
  }
}

if {$doCoast} {
    set _paths(coast) "$_paths(support)/coast"
} else {
  if {[lsearch -exact $pathlist coast] == -1} { ;#if coast was not in GEODAS config file
    set _paths(coast) $cdpaths(coast)
  } elseif {![file exists $_paths(coast)/hersey.b00]} {
    set _paths(coast) $cdpaths(coast)
  }
}
 #parray _paths

# write to geodas.cfg
if [string match trkdas $cddbase] {
  set _settings(database) trkdas
  set _databases(trkdas) "Marine Trackline Geophysics"
} elseif [string match trkmec $cddbase] {
  set _settings(database) trkmec
  set _databases(trkmec) "US East Coast Multibeam Bathymetry"
  set _databases(trkmbb) "NGDC Multibeam Bathymetry"
} elseif [string match aromin $cddbase] {
  set _settings(database) aromin
  set _databases(aromin) "Minnesota Aeromagnetics"
  set _databases(arodas) "NGDC Aeromagnetic Surveys"
} elseif [string match aronur $cddbase] {
  set _settings(database) aronur
  set _databases(aronur) "N.U.R.E. Aeromagnetics"
  set _databases(arodas) "NGDC Aeromagnetic Surveys"
} elseif [string match hydnos $cddbase] {
  set _settings(database) hydnos
  set _databases(hydnos) "NOS Hydrographic Surveys"
} elseif [string match grdcrm $cddbase] {
  set _databases(grdcrm) "NGDC Coastal Relief Model 3 Second Grids"
  foreach volid $cdvolids {   ;#cdvolids is a list: grddas01 grddas02 grddas03
    set _databases($volid) "NGDC Coastal Relief Model Volume [string range $volid 6 7]"
  }
  set _blocksizes($cddbase) 1
  set _latcellsizes($cddbase) 3
  set _loncellsizes($cddbase) 3
  set _ngdcprefixes($cddbase) gn
} elseif [string match grdglb $cddbase] {
  set _databases(grdglb) "Great Lakes Bathymetry 3 Second Grids"
  foreach volid $cdvolids {   ;#cdvolids is a list: grddasg1 grddasg2 grddasg3
    set _databases($volid) "Great Lakes Bathymetry Volume [string toupper [string range $volid 6 7]]"
  }
  set _blocksizes($cddbase) 1
  set _latcellsizes($cddbase) 3
  set _loncellsizes($cddbase) 3
  set _ngdcprefixes($cddbase) gn
} elseif [string match grdgeb $cddbase] {
  set _databases(grdgeb) "GEBCO Worldwide Bathymetry 1 Minute Grids"
  set _databases($cdvolid) "GEBCO Worldwide Bathymetry Volume [string toupper [string range $cdvolid 6 7]]"
  set _blocksizes($cddbase) 30
  set _latcellsizes($cddbase) 60
  set _loncellsizes($cddbase) 60
  set _ngdcprefixes($cddbase) gb
} elseif [string match grdet2 $cddbase] {
  set _databases(grdet2) "ETOPO2 Global Elevation Data 2 Minute Grids"
  set _databases($cdvolid) "ETOPO2 Global Gridded Elevation Data Volume [string toupper [string range $cdvolid 6 7]]"
  set _blocksizes($cddbase) 45
  set _latcellsizes($cddbase) 120
  set _loncellsizes($cddbase) 120
  set _ngdcprefixes($cddbase) ge
}

 #tk_messageBox -message "Path Install:$pathinstall\nPath Bin:$_paths(prog)"


if {![file exists $_paths(work)]} then {
  #tk_messageBox -message "try: _paths(work) $_paths(work)"
  if [catch [file mkdir $_paths(work)] result] {
    tk_dialog .adialog "GEODAS Work Path Error" \
    "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper permissions" error 0 "OK"
    return 0
  }
}

if {$doSoft && $doSoftEnabled} {
  set daresult [copy_software]
  if [string match 0 $daresult] {
    return 0
  }
} ;#if {$doSoft}

update

if {$doCoast && $doCoastEnabled} {
  copy_coastlines
} ;#if {$doCoast}

update

if {$doInv && $doInvEnabled} {
  copy_inventories
} ;#if {$doInv}


#make a backup of the geodas.cfg file
if [file exists $pathconfig/geodas.cfg] {
  file copy -force $pathconfig/geodas.cfg $pathconfig/geodas.cfg.bak
  file delete $pathconfig/geodas.cfg
}

if [string match Linux $tcl_platform(os)] {
  #make a backup of the geodas.ini file
 #puts "copy:$pathconfig/geodas.ini.bak"
  if [file exists $pathconfig/geodas.ini] {
    file copy -force $pathconfig/geodas.ini $pathconfig/geodas.ini.bak
    file delete $pathconfig/geodas.ini
  }
}

 #tk_messageBox -message "try:$pathconfig/geodas.cfg"
if [catch {open $pathconfig/geodas.cfg w 0755} fileout] {
  . config -cursor {}
  tk_dialog .adialog "GEODAS Install/Setup ERROR" \
  "unable to write file: $pathconfig/geodas.cfg\nCheck permissions" error 0 "OK"
  return 0
}

if [string match Linux $tcl_platform(os)] {
 #puts "open:$pathconfig/geodas.ini"
  if [catch {open $pathconfig/geodas.ini w 0755} fileini] {
    . config -cursor {}
    tk_dialog .adialog "GEODAS Install/Setup ERROR" \
   "unable to write file: $pathconfig/geodas.ini\nCheck permissions" error 0 "OK"
    return 0
  }
}

puts $fileout "#Geodas Configuration File"
puts $fileout "#Be very carefull if you decide to modify by hand!"
puts $fileout " "
if [string match Linux $tcl_platform(os)] {
  puts $fileini "#Geodas Initialization File"
  puts $fileini "#Be very carefull if you decide to modify by hand!"
  puts $fileini " "
  puts $fileini "\[General\]"
}

foreach type [lsort [array names _settings]] {
  set aline [format "set %s(%s)" _settings $type]
  set aline "$aline \"$_settings($type)\""
  puts $fileout $aline
  if [string match Linux $tcl_platform(os)] {
    set aname [string toupper $type]
    set avalue $_settings($type)
    if [string match SOFTWAREVERSION $aname] {
      set aname VERSION
    }
    set aline "$aname=$avalue"
    puts $fileini $aline
  }
}
puts $fileout " "
if [string match Linux $tcl_platform(os)] {
  puts $fileini " "
  puts $fileini "\[DataBase\]"
}

foreach dbase [lsort [array names _databases]] {
  set aline [format "set %s(%s)" _databases $dbase]
  set aline "$aline \"$_databases($dbase)\""
  puts $fileout $aline
  if [string match Linux $tcl_platform(os)] {
    set aname [string toupper $dbase]
    set avalue $_databases($dbase)
    set aline "$aname=$avalue"
    puts $fileini $aline
  }
}
puts $fileout " "
if [string match Linux $tcl_platform(os)] {
  puts $fileini " "
  puts $fileini "\[Path\]"
}

foreach pathitem [lsort [array names _paths]] {
  set aline [format "set %s(%s)" _paths $pathitem]
  set aline "$aline \"$_paths($pathitem)\""
  puts $fileout $aline
  if [string match Linux $tcl_platform(os)] {
    set aname [string toupper $pathitem]
    set avalue "$_paths($pathitem)/"
    switch -glob $aname {
      HOME {
        set aname SOFTWARE
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      CD {
        set aname SETUPCD
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      TRK??? {
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      HYD??? {
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      ARO??? {
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      COAST {
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      HELP {
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      WORK {
        set aline "$aname=$avalue"
        puts $fileini $aline
      }
      default {
      }
    } ;#switch
  } ;#if linux
} ;#foreach pathitem

puts $fileout " "

#blocksize,latcellsize and loncellsize are for grid data
foreach dbase [lsort [array names _blocksizes]] {
  set aline [format "set %s(%s)" _blocksizes $dbase]
  set aline "$aline $_blocksizes($dbase)"
  puts $fileout $aline
}

foreach dbase [lsort [array names _latcellsizes]] {
  set aline [format "set %s(%s)" _latcellsizes $dbase]
  set aline "$aline $_latcellsizes($dbase)"
  puts $fileout $aline
}

foreach dbase [lsort [array names _loncellsizes]] {
  set aline [format "set %s(%s)" _loncellsizes $dbase]
  set aline "$aline $_loncellsizes($dbase)"
  puts $fileout $aline
}

foreach dbase [lsort [array names _ngdcprefixes]] {
  set aline [format "set %s(%s)" _ngdcprefixes $dbase]
  set aline "$aline $_ngdcprefixes($dbase)"
  puts $fileout $aline
}


puts $fileout " "

if [string match Linux $tcl_platform(os)] {
  #first set up links from minor to major database
  foreach dbase [lsort [array names _databases]] {
    if [string match grd????? $dbase] {
      switch $dbase {
        grddas01 -
        grddas02 -
        grddas03 -
        grddas04 -
        grddas05 -
        grddas06 -
        grddas07 -
        grddas08 {set _dbmajor($dbase) grdcrm}
        grddasg1 -
        grddasg2 {set _dbmajor($dbase) grdglb}
        grddase1 {set _dbmajor($dbase) grdet2}
        grddasw1 {set _dbmajor($dbase) grdgeb}
        default {
          tk_messageBox -message "setup doesn't know:$dbase Contact NGDC"
          exit
        }
      } ;#switch
    }
  }
 #parray _dbmajor
  foreach dbase [lsort [array names _databases]] {
    set adb [string toupper $dbase]
    if [string match GRD??? $adb] {
      puts $fileini " "
      puts $fileini "\[$adb\]"
      set aline "BLOCKSIZE=$_blocksizes($dbase)"
      puts $fileini $aline
      set aline "LATCELLSIZE=$_latcellsizes($dbase)"
      puts $fileini $aline
      set aline "LONCELLSIZE=$_loncellsizes($dbase)"
      puts $fileini $aline
      set aline "NGDCPREFIX=$_ngdcprefixes($dbase)"
      puts $fileini $aline
    }
  } ;#foreach
  foreach dbase [lsort [array names _databases]] {
    set adb [string toupper $dbase]
    if [string match grd??? $dbase] {
      if [file readable $_paths($dbase)/$dbase.inv] {
        if [info exists _grids] {
          unset _grids
        }
        source $_paths($dbase)/$dbase.inv
        #puts "sourced:$_paths($dbase)/$dbase.inv"
        foreach dbminor [lsort [array names _dbmajor]] {
          if [string match $dbase $_dbmajor($dbminor)] {
            puts $fileini " "
            puts $fileini "\[[string toupper $dbminor]\]"
            puts $fileini "DATABASE=[string toupper $dbase]"
            set adb [string toupper $dbminor]
            foreach grid [lsort [array names _grids]] {
              #puts "string match $adb [string range $_grids($grid) 0 7]"
              if [string match $adb [string range $_grids($grid) 0 7]] {
                puts $fileini "$grid=[string range $_grids($grid) 9 10].[string index $_grids($grid) 11]"
              }
            }
          }
        }
      }
    }
  } ;#foreach
}

foreach field [lsort [array names _headGRD]] {
  set aline [format "set %s(%s)" _headGRD $field]
  set aline "$aline $_headGRD($field)"
  puts $fileout $aline
}
puts $fileout " "

if [string match "" $_options(recordDelimiter)] {
  set _options(recordDelimiter) rdNONE
}
foreach option [array names _options] {
  set aline [format "set %s(%s)" _options $option]
  set aline "$aline $_options($option)"
#puts "options line:$aline"
  puts $fileout $aline
}

close $fileout
.feed.txt insert end "\n\ngeodas.cfg built\n" 
. config -cursor {}

update
 #tk_messageBox -message "GEODAS Successfully Installed!"
tk_dialog .adialog "GEODAS INSTALLED!" $installmess {} 0 "OK"
set msg "To install another GEODAS Database:\nMount the GEODAS SETUP cd and then press Continue"
set result 0
if [string match geodas-setup $thisrun] {
  if ![string match $pathconfig $pathinstall] {
    set result 1
  }
}
while {$result != 1} {
  set result [tk_dialog .dialog GEODAS $msg {} 0 Continue Finished]
  if {$result == 0} { ;#Continue pressed
    if [catch {open $cdpaths(cd)/cdrdas.vol r} fileid] {
      tk_dialog .adialog "GEODAS SETUP CD Not Found!" \
      "Unable to open:$cdpaths(cd)/cdrdas.vol \nPlease be sure GEODAS SETUP CD is mounted." error 0 "OK"
    } else {
      gets $fileid cdvolume
      set cdversion [string tolower [string range $cdvolume 9 11]]  ;# v41 v10
      set cdvolid [string tolower [string range $cdvolume 0 7]]
      set cddbase [string tolower [string range $cdvolume 0 5]]
      #in some cases, fix database from cdvolid
      if [string match hyddas01 $cdvolid] {
        set cddbase hydnos
      }
      if [string match grddasg? $cdvolid] {
        set cddbase grdglb
      } elseif [string match grddasw1 $cdvolid] {
        set cddbase grdgeb
      } elseif [string match grddase1 $cdvolid] {
        set cddbase grdet2
      } elseif [string match grddas?? $cdvolid] {
        set cddbase grdcrm
      }
      if [string match mbbdas01 $cdvolid] {
        set cddbase trkmec
      }
      if [string match arodasm1 $cdvolid] {
        set cddbase aromin
      }
      if [string match arodasn1 $cdvolid] {
        set cddbase aronur
      }
      #puts "cddbase:$cddbase"
      unset cdvolumes
      unset cdvolnums
      unset cdvolids
      if [string match 00 [string range $cdvolid 6 7]] {   ;#new DVD multi-volume standard
        while {![eof $fileid]} {
          gets $fileid cdnextvol ;#read cdrdas.vol file for actual volumes
          if {![eof $fileid]} {
            lappend cdvolumes $cdnextvol ;#GRDDAS05.V41
            lappend cdvolnums [string range $cdnextvol 6 7] ;#05
            lappend cdvolids [string tolower [string range $cdnextvol 0 7]] ;#grddas05!!!
          }
        }
      } else {
        lappend cdvolumes $cdvolume ;#GRDDASE1.V41
        lappend cdvolnums [string range $cdvolume 6 7]  ;#E1
        lappend cdvolids $cdvolid  ;#grddase1
      }
      #puts "cdvolumes:$cdvolumes"
      #puts "cdvolnums:$cdvolnums"
      #puts "cdvolids:$cdvolids"
      close $fileid
      set cdpaths(coast) ""
      set cdpaths(inv) ""
      if [string match littleEndian $tcl_platform(byteOrder)] {
        if [string match uppercase $_settings(casecd)] {
          set cdpaths(coast) "$cdpaths(cd)/GDAS"
          set cdpaths(inv) "$cdpaths(cd)/GDAS"
        } else {
         set cdpaths(coast) "$cdpaths(cd)/gdas"
          set cdpaths(inv) "$cdpaths(cd)/gdas"
        }
      } elseif [string match bigEndian $tcl_platform(byteOrder)] {
        if [string match uppercase $_settings(casecd)] {
          set cdpaths(coast) "$cdpaths(cd)/GDAS/BIGENDIA"
          set cdpaths(inv) "$cdpaths(cd)/GDAS/BIGENDIA"
        } else {
          set cdpaths(coast) "$cdpaths(cd)/gdas/bigendia"
          set cdpaths(inv) "$cdpaths(cd)/gdas/bigendia"
        }
      } else {
        tk_messageBox -message "ERROR, cannot determine byte order"
        return 0
      }
      
      .options.cbinv configure -state normal
      if [string match trkdas $cddbase] {
        .options.cbinv configure -text "Marine Trackline Geophysics Inventory (30.2 MB)"
      } elseif [string match hydnos $cddbase] {
        .options.cbinv configure -text "NOS Hydrographic Surveys Inventory (13.2 MB)"
      } elseif [string match trkmec $cddbase] {
        .options.cbinv configure -text "US East Coast Multibeam Bathymetry Inventories (3.8 MB)"
      } elseif [string match aromin $cddbase] {
        .options.cbinv configure -text "Minnesota Aeromagnetics Inventories (18.8 MB)"
      } elseif [string match aronur $cddbase] {
         .options.cbinv configure -text "N.U.R.E. Aeromagnetics Inventories (18.9 MB)"
      } elseif [string match grdglb $cddbase] {
        .options.cbinv configure -text "Great Lakes Bathymetry Volume(s) $cdvolnums"
      } elseif [string match grdgeb $cddbase] {
        .options.cbinv configure -text "GEBCO Worldwide Bathymetry Grids"
      } elseif [string match grdet2 $cddbase] {
        .options.cbinv configure -text "ETOPO2 Global Gridded Elevation Data"
      } elseif [string match grdcrm $cddbase] {
        .options.cbinv configure -text "NGDC Coastal Relief Model Volume(s) $cdvolnums"
      }
      if {$doneCoast == 0} {
        if [file exists $cdpaths(coast)/hersey.b00] {
          .options.cbcoast configure -state normal
          set doCoastEnabled 1
        } else {
          .options.cbcoast configure -state disabled
          set doCoastEnabled 0
        }
      }
      set doInvEnabled 1
      set doInv 0
      set result 1
    } ;#if
  } else { ;#user clicked finished
    if {[expr $doSoftEnabled +  $doCoastEnabled +  $doInvEnabled] == 0} {
      .controls.ok configure -state disabled 
    }
  } ;#if
} ;#while
update

} ;#end proc do_copys



                                           

##########################
### proc copy_software ###
##########################
proc copy_software {} {

global tcl_platform
global pathinstall
global cdpaths
global _paths
global doSoftEnabled
global doneSoft
global installmess
global opsys
global thisrun
global dlgresponse
global softversion
global oldsoftversion

 #puts "copy_software pathinstall:$pathinstall"


if [string match geodas-setup $thisrun] {
  .options.cbsoft configure -state disabled
  set doSoftEnabled 0
  set installmess "$installmess\nConfiguration"
  return 1
}

set wishpath /usr/bin/wish
if ![file exists $wishpath] {
  set wishpath /usr/local/bin/wish
}

while {![file exists $wishpath]} {
  set dlgresponse $wishpath ;#default dialog response
  set result [install_inputdialog "Please Enter Tcl/Tk wish interpreter path" dlgresponse]
  if {$result} {  ;#if OK pressed
    set wishpath $dlgresponse
  } else {        ;#if Cancel pressed
    return 0
  }
}

 #puts "wishpath:$wishpath"


  . config -cursor watch
  update

# note that _paths(prog) is _paths(home)/bin  
  .feed.txt delete 1.0 end
  if [file exists $_paths(prog)] then { ;#get rid of files no longer used
    if [file exists $_paths(prog)/geodas] {
       file delete $_paths(prog)/geodas
    }
    if [file exists $_paths(prog)/geowish] {
       file delete $_paths(prog)/geowish
    }
    if [file exists $_paths(prog)/geowish4.0] {
       file delete $_paths(prog)/geowish4.0
    }
    if [file exists $_paths(prog)/geopath] {
       file delete $_paths(prog)/geopath
    }
    if [file exists $_paths(prog)/geodas.ini] {
       file delete $_paths(prog)/geodas.ini
    }
    if [file exists $_paths(prog)/gdinit] {
       file delete $_paths(prog)/gdinit
    }
    if [file exists $_paths(prog)/tkerror] {
       file delete $_paths(prog)/tkerror
    }
    if [file exists $_paths(prog)/readme.txt] {
       file delete $_paths(prog)/readme.txt
    }
    if [file exists $_paths(prog)/text.tcl] {
       file delete $_paths(prog)/text.tcl
    }
     if [file exists $_paths(prog)/tearoff.tcl] {
       file delete $_paths(prog)/tearoff.tcl
    }
    if [file exists $_paths(prog)/parray.tcl] {
       file delete $_paths(prog)/parray.tcl
    }
    if [file exists $_paths(prog)/palette.tcl] {
       file delete $_paths(prog)/palette.tcl
    }
    if [file exists $_paths(prog)/optMenu.tcl] {
       file delete $_paths(prog)/optMenu.tcl
    }
    if [file exists $_paths(prog)/obsolete.tcl] {
       file delete $_paths(prog)/obsolete.tcl
    }
    if [file exists $_paths(prog)/menu.tcl] {
       file delete $_paths(prog)/menu.tcl
    }
    if [file exists $_paths(prog)/focus.tcl] {
       file delete $_paths(prog)/focus.tcl
    }
    if [file exists $_paths(prog)/entry.tcl] {
       file delete $_paths(prog)/entry.tcl
    }
    if [file exists $_paths(prog)/button.tcl] {
       file delete $_paths(prog)/button.tcl
    }
    if [file exists $_paths(prog)/bgerror.tcl] {
       file delete $_paths(prog)/bgerror.tcl
    }
    if [file exists $_paths(prog)/entry.tcl] {
       file delete $_paths(prog)/entry.tcl
    }
    if [file exists $_paths(prog)/dialog.tcl] {
       file delete $_paths(prog)/dialog.tcl
    }
    if [file exists $_paths(prog)/tk.tcl] {
       file delete $_paths(prog)/tk.tcl
    }
    if [file exists $_paths(prog)/scrlbar.tcl] {
       file delete $_paths(prog)/scrlbar.tcl
    }
    if [file exists $_paths(prog)/init.tcl] {
       file delete $_paths(prog)/init.tcl
    }
    if [file exists $_paths(prog)/win_mnt.tcl] {
       file delete $_paths(prog)/win_mnt.tcl
    }
    if [file exists $_paths(prog)/bathy.tcl] {
       file delete $_paths(prog)/bathy.tcl
    }
  } else { ;#_paths(prog) does not exist
    if [catch [file mkdir $_paths(prog)] result] {
      tk_dialog .adialog "GEODAS Path Error" \
      "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper
      permissions" \
      info 0 "OK"
      . config -cursor {}
      return 0
    }
  }

  if [file writable $_paths(help)] then {
    if [file exists $_paths(help)/geodas.hlp] {
       file delete $_paths(help)/geodas.hlp
    }
    if [file exists $_paths(help)/trkdas_c.txt] {
       file delete $_paths(help)/trkdas_c.txt]
    }
    if [file exists $_paths(help)/hydnos_c.txt] {
       file delete $_paths(help)/hydnos_c.txt]
    }
    if [file exists $_paths(help)/arodas_c.txt] {
       file delete $_paths(help)/arodas_c.txt]
    }
  } else {
    if [catch [file mkdir $_paths(help)] result] {
      tk_dialog .adialog "GEODAS Path Error" \
      "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper
      permissions" \
      info 0 "OK"
      . config -cursor {}
      return 0
    }
  }

  if [file writable $_paths(home)] then {
    if [file exists $_paths(home)/geodas-wish] {
       file delete $_paths(home)/geodas-wish
    }
  }

# Delete the old coastline files if found
  if [file writable $_paths(support)/coast] then {
    if [file exists $_paths(support)/coast/her3.cs8] {
       file delete $_paths(support)/coast/her3.cs8
    }
    if [file exists $_paths(support)/coast/hers.cs8] {
       file delete $_paths(support)/coast/hers.cs8
    }
    if [file exists $_paths(support)/coast/wdb1.cs8] {
       file delete $_paths(support)/coast/wdb1.cs8
    }
    if [file exists $_paths(support)/coast/wv12.cs4] {
       file delete $_paths(support)/coast/wv12.cs4
    }
    if [file exists $_paths(support)/coast/wv01.cs4] {
       file delete $_paths(support)/coast/wv01.cs4
    }
    if [file exists $_paths(support)/coast/nu05.cs4] {
       file delete $_paths(support)/coast/nu05.cs4
    }
    if [file exists $_paths(support)/coast/nu80.cs4] {
       file delete $_paths(support)/coast/nu80.cs4
    }
  }

  if ![file exists $_paths(images)] then {
    if [catch [file mkdir $_paths(images)] result] {
      tk_dialog .adialog "GEODAS Path Error" \
      "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper permissions" \
      info 0 "OK"
      . config -cursor {}
      return 0
    }
  }

  if ![file exists $_paths(lib)] then {
    if [catch [file mkdir $_paths(lib)] result] {
      tk_dialog .adialog "GEODAS Path Error" \
      "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper permissions" \
      info 0 "OK"
      . config -cursor {}
      return 0
    }
  }

  if ![file exists $_paths(source)] then {
    if [catch [file mkdir $_paths(source)] result] {
      tk_dialog .adialog "GEODAS Path Error" \
      "$result\n\nPlease be sure GEODAS Install Paths are correct\nand have proper permissions" \
      info 0 "OK"
      . config -cursor {}
      return 0
    }
  }


  #.feed.txt insert 1.0 "[exec find . -type f -maxdepth 1 -name $filesin]"
  #  .feed.txt insert 1.0 "[eval exec ls [glob $filesin]]"
  
  .feed.txt insert end ">> Copy Software Files:\n" 

# note: in older installations, some scripts (geodas,geodas-coastex,geodas-gridtran,geodas-setup)
#       have mod-date = installation-date, so version number must be used as well as date
  #puts "oldsoftversion=$oldsoftversion"
  #puts "   softversion=$softversion"

 ################ copying some geodas startup files
  if ![string match unix $opsys] { ;#if not Source Installation
    set present [file exists $_paths(home)/geodas-gridlist]
    if {!$present || ($present && ([file mtime ./$opsys/geodas-gridlist] > [file mtime $_paths(home)/geodas-gridlist])) } {
      .feed.txt insert end "copying: geodas-gridlist\n"
      file copy -force ./$opsys/geodas-gridlist $_paths(home)
    }
  }
  .feed.txt insert end :\n

  if ![string match unix $opsys] { ;#if not Source Installation
    set present [file exists $_paths(home)/geodas-hdrecs77]
    if {!$present || ($present && ([file mtime ./$opsys/geodas-hdrecs77] > [file mtime $_paths(home)/geodas-hdrecs77])) } {
      .feed.txt insert end "copying: geodas-hdrecs77\n"
      file copy -force ./$opsys/geodas-hdrecs77 $_paths(home)
    }
  }
  .feed.txt insert end :\n

  ################ copying tcl scripts, bin's, tclIndex
  set source_files [concat [glob ./*.tcl] [glob ./*.bin] {tclIndex}]
  
  foreach file $source_files {
     set present [file exists $_paths(prog)/$file]
     # present = 1 if file is there, 0 if not

     if {!$present || ($present && ([file mtime $file] > [file mtime $_paths(prog)/$file])) } {
        .feed.txt insert end "copying: $file"
        .feed.txt insert end \n
        file copy -force $file $_paths(prog)
     }
  } ;#foreach
  .feed.txt insert end :\n



  ################ copying linux-only executables, shared objects
if [string match Linux $tcl_platform(os)] {
  set present [file exists $_paths(home)/geodas-gridtran]
  #puts "$_paths(home)/geodas-gridtran present=$present"
  # present = 1 if file is there, 0 if not
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./linux/geodas-gridtran] > [file mtime $_paths(home)/geodas-gridtran])) } {
     .feed.txt insert end "copying: gridtran"
     .feed.txt insert end \n
     file copy -force ./linux/geodas-gridtran $_paths(home)
  }
  set present [file exists $_paths(home)/geodas-coastex]
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./linux/geodas-coastex] > [file mtime $_paths(home)/geodas-coastex])) } {
     .feed.txt insert end "copying: coastex"
     .feed.txt insert end \n
     file copy -force ./linux/geodas-coastex $_paths(home)
  }
  set present [file exists $_paths(home)/geodas-hydroplot]
  if {!$present || ($present && ([file mtime ./linux/geodas-hydroplot] > [file mtime $_paths(home)/geodas-hydroplot])) } {
     .feed.txt insert end "copying: hydroplot"
     .feed.txt insert end \n
     file copy -force ./linux/geodas-hydroplot $_paths(home)
  }
  set present [file exists $_paths(lib)/libqtintf-6.5-qt2.3.so]
  if {!$present || ($present && ([file mtime ./linux/libqtintf-6.5-qt2.3.so] > [file mtime $_paths(lib)/libqtintf-6.5-qt2.3.so])) } {
     .feed.txt insert end "copying: libqtintf-6.5-qt2.3.so"
     .feed.txt insert end \n
     file copy -force ./linux/libqtintf-6.5-qt2.3.so $_paths(lib)
  }
  set present [file exists $_paths(lib)/libqt.so.2]
  if {!$present || ($present && ([file mtime ./linux/libqt.so.2] > [file mtime $_paths(lib)/libqt.so.2])) } {
     .feed.txt insert end "copying: libqt.so.2"
     .feed.txt insert end \n
     file copy -force ./linux/libqt.so.2 $_paths(lib)
  }
}


  ################ copying horizontal datum grid files
  if [string match littleEndian $tcl_platform(byteOrder)] {
    set filesdir "./files_le"
  } else {
    set filesdir "./files_be"
  }

  set source_files [concat [glob $filesdir/*.las] [glob $filesdir/*.los] ]
  
  foreach file $source_files {
     set present [file exists $_paths(prog)/$file]
     # present = 1 if file is there, 0 if not

     if {!$present || ($present && ([file mtime $file] > [file mtime $_paths(prog)/$file])) } {
        .feed.txt insert end "copying: $file"
        .feed.txt insert end \n
        file copy -force $file $_paths(prog)
     }
  } ;#foreach
  .feed.txt insert end :\n

  ################ copying .htm files for Coastal Relief Model
  set source_files [concat [glob ./*.htm]]
  
  foreach file $source_files {
     set present [file exists $_paths(support)/$file]
     if {!$present || ($present && ([file mtime $file] > [file mtime $_paths(support)/$file])) } {
        .feed.txt insert end "copying: $file"
        .feed.txt insert end \n
        file copy -force $file $_paths(support)
     }
  } ;#foreach
  .feed.txt insert end :\n

  ########### gx_'s
  if ![string match unix $opsys] { ;#if not Source Installation
    foreach filedir [glob $opsys/gx_*] {
       set file [file tail $filedir]
       #puts "this is file being evaluated: $file"
       set present [file exists $_paths(prog)/$file]
       # present = 1 if file is there, 0 if not

       if {!$present || ($present && ([eval file mtime $filedir] > [file mtime $_paths(prog)/$file])) } {
          .feed.txt insert end "copying: $file\n"
          file copy -force $filedir $_paths(prog)
       }
    } ;#foreach
  }

  ######### source code
  .feed.txt insert end ">>Copy source files:\n"
  set present [file exists $_paths(source)/readme.txt]
  if {!$present || ($present && ([file mtime ./source/readme.txt] > [file mtime $_paths(source)/readme.txt])) } {
    file copy -force ./source/readme.txt $_paths(source)
  }
  if ![file exists $_paths(source)/geodastcl] {
    file mkdir $_paths(source)/geodastcl
  }
  foreach filedir [glob ./source/geodastcl/*] {
    set file [file tail $filedir]
    set present [file exists $_paths(source)/geodastcl/$file]
    if {!$present || ($present && ([eval file mtime $filedir] > [file mtime $_paths(source)/geodastcl/$file])) } {
      file copy -force $filedir $_paths(source)/geodastcl
    }
  } ;#foreach
  if ![file exists $_paths(source)/fortran] {
    file mkdir $_paths(source)/fortran
  }
  foreach filedir [glob ./source/fortran/*] {
    set file [file tail $filedir]
    set present [file exists $_paths(source)/fortran/$file]
    if {!$present || ($present && ([eval file mtime $filedir] > [file mtime $_paths(source)/fortran/$file])) } {
      file copy -force $filedir $_paths(source)/fortran
    }
  } ;#foreach
  .feed.txt insert end :\n

  # copying the main readme file ...
  set present [eval file exists $_paths(support)/readme.txt]
     # present = 1 if file is there, 0 if not
  
  if {!$present || ($present && ([file mtime readme.txt] > [file mtime $_paths(support)/readme.txt])) } {
     .feed.txt insert end "copying: readme.txt\n"
     file copy -force readme.txt $_paths(support)
  }

  .feed.txt insert end :\n

  ############### copy main scripts ################

  set present [file exists $_paths(home)/geodas-search]
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./geodas-search] > [file mtime $_paths(home)/geodas-search])) } {
    .feed.txt insert end "copying: geodas-search\n"
    file copy -force ./geodas-search $_paths(home)
  }
  .feed.txt insert end :\n


if ![string match Linux $tcl_platform(os)] { ;#if Linux, we have Kylix Versions instead
  set present [file exists $_paths(home)/geodas-gridtran]
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./geodas-gridtran] > [file mtime $_paths(home)/geodas-gridtran])) } {
    .feed.txt insert end "copying: geodas-gridtran\n"
    file copy -force ./geodas-gridtran $_paths(home)
  }
  .feed.txt insert end :\n

  set present [file exists $_paths(home)/geodas-coastex]
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./geodas-coastex] > [file mtime $_paths(home)/geodas-coastex])) } {
    .feed.txt insert end "copying: geodas-coastex\n"
    file copy -force ./geodas-coastex $_paths(home)
  }
  .feed.txt insert end :\n
}  ;#![string match Linux $tcl_platform(os)]

  #now copy geodas-install script, renaming to geodas-setup
  set present [file exists $_paths(home)/geodas-setup]
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./geodas-install] > [file mtime $_paths(home)/geodas-setup])) } {
    .feed.txt insert end "copying: geodas-setup\n"
    file copy -force ./geodas-install $_paths(home)/geodas-setup
  }
  .feed.txt insert end :\n

  #now copy over geodas script
  set present [file exists $_paths(home)/geodas]
  if [string match 1 [string compare $softversion $oldsoftversion]] {set present 0} ;#forces copy
  if {!$present || ($present && ([file mtime ./geodas] > [file mtime $_paths(home)/geodas])) } {
    .feed.txt insert end "copying: geodas\n"
    file copy -force ./geodas $_paths(home)
  }
  .feed.txt insert end :\n


  ############# copying help files from ./help ############ 
  .feed.txt insert end ">>Copy help files....\n"

  foreach filedir [glob help/*] {
     set file [file tail $filedir]
     set present [file exists $_paths(help)/$file]
     if {!$present || ($present && ([file mtime $filedir] > [file mtime $_paths(help)/$file])) } {
        .feed.txt insert end "copying: $file\n"
        file copy -force $filedir $_paths(help)
     }
  } ;#foreach
  .feed.txt insert end :\n


  ############# image files ... #################
    .feed.txt insert end ">>Copy image files:\n"
    foreach filedir [glob images/*] {
      set file [file tail $filedir]
      set present [eval file exists $_paths(images)/$file]
      if {!$present || ($present && ([file mtime $filedir] > [file mtime $_paths(images)/$file])) } {
        .feed.txt insert end "copying: $file"
        .feed.txt insert end \n
        file copy -force $filedir $_paths(images)
      }
    } ;# foreach
  .feed.txt insert end :\n

  ############# lib files ... #################
  .feed.txt insert end ">>Copy lib files:\n"

  if ![string match unix $opsys] { ;#if not Source Installation
    set present [file exists $_paths(lib)/libgeodastcl.so]
    if {!$present || ($present && ([file mtime ./$opsys/libgeodastcl.so] > [file mtime $_paths(lib)/libgeodastcl.so])) } {
      .feed.txt insert end "copying: libgeodastcl.so\n"
      file copy -force ./$opsys/libgeodastcl.so $_paths(lib)
    }
  }
  .feed.txt insert end :\n

  set present [file exists $_paths(lib)/html_library]
  if {!$present || ($present && ([file mtime ./html_library] > [file mtime $_paths(lib)/html_library])) } {
    file copy -force ./html_library $_paths(lib)
  }
  .feed.txt insert end :\n


  .options.cbsoft configure -state disabled
  set doSoftEnabled 0
  set installmess "$installmess\nSoftware"
  set doneSoft 1
  . config -cursor {}
  update
} ;#end proc copy_software


############################
### proc copy_coastlines ###
############################
proc copy_coastlines {} {

global pathinstall
global cdpaths
global _paths
global doCoastEnabled
global doneCoast
global installmess
global opsys
global _settings

  . config -cursor watch
  update
  if {![file exists $_paths(coast)]} then {
    # tk_messageBox -message "try: _paths(coast) $_paths(coast)"
    if [catch [file mkdir $_paths(coast)] result] {
      . config -cursor {}
      tk_dialog .adialog "Unable to Create GEODAS Path: $_paths(coast)" \
      "$result\n\nPlease be sure GEODAS Install Path is correct\nand has proper permissions" info 0 "OK"
      return 0
    }
  }
  .feed.txt insert end "\n>> Copy Coastlines Files:\n"   !!!starthere
  if [string match $_settings(casecd) uppercase] {
    set cstext .B00
  } else {
    set cstext .b00
  }
  foreach filedir [glob $cdpaths(coast)/*$cstext] {
     # puts "this is filedir: $filedir"
     set file [file tail $filedir]
     set present [eval file exists $_paths(coast)/$file]
     # present = 1 if file is there, 0 if not

     if {!$present || ($present && ([eval file mtime $filedir] > [eval \
                file mtime $_paths(coast)/$file])) } {
        .feed.txt insert end "copying: $file"
        .feed.txt insert end \n
        file copy -force $filedir $_paths(coast)
     }
  }
 
  .options.cbcoast configure -state disabled
  set doCoastEnabled 0
  set installmess "$installmess\nCoastlines"
  set doneCoast 1
  . config -cursor {}
  update
} ;#end proc copy_coastlines


############################
### proc copy_inventories ###
############################
proc copy_inventories {} {

global pathinstall
global cdpaths
global _paths
global _databases
global cddbase
global cdvolume
global cdvolid
global cdvolumes
global cdvolnums
global cdvolids
global cdversion
global doInvEnabled
global doneInv
global installmess
global opsys

  . config -cursor watch
  update
  #tk_messageBox -message "Do Inventories"
#  if [string match grd??? $cddbase] {
#    set basedbase grddas
#  } else {
#    set basedbase $cddbase
#  }
 
  set invpath $_paths($cddbase)

  if {![file exists $invpath]} then {
    if [catch [file mkdir $invpath] result] {
      . config -cursor {}
      tk_dialog .adialog "Unable to Create GEODAS Path: $invpath" \
      "$result\n\nPlease be sure GEODAS Install Path is correct\nand has proper permissions" info 0 "OK"
      return 0
    }
  }
  .feed.txt insert end "\n\n>> Copy Inventory Files\n"
  # parray _paths
  if [string match trk* $cddbase] {
     foreach filedir [glob $cdpaths(inv)/trk*] {
        set file [file tail $filedir]
        set present [eval file exists $_paths($cddbase)/$file]
        if {!$present || ($present && ([eval file mtime $filedir] > [eval \
                   file mtime $_paths($cddbase)/$file])) } {
           .feed.txt insert end "copying: $file"
           .feed.txt insert end \n
           file copy -force $filedir $_paths($cddbase)
        }
     }
   } elseif {$cddbase == "hydnos"} {
     foreach filedir [glob $cdpaths(inv)/hyd*] {
        #puts "this is filedir: $filedir"
        set file [file tail $filedir]
        set present [file exists $_paths($cddbase)/$file]
        # present = 1 if file is there, 0 if not

        if {!$present || ($present && ([eval file mtime $filedir] > [eval \
                   file mtime $_paths($cddbase)/$file])) } {
           .feed.txt insert end "copying: $file"
           .feed.txt insert end \n
           file copy -force $filedir $_paths($cddbase)
        }
     }
   } elseif [string match aro* $cddbase] {
     foreach filedir [glob $cdpaths(inv)/aro*] {
        #puts "this is filedir: $filedir"
        set file [file tail $filedir]
        set present [eval file exists $_paths($cddbase)/$file]
        # present = 1 if file is there, 0 if not

        if {!$present || ($present && ([eval file mtime $filedir] > [eval \
                   file mtime $_paths($cddbase)/$file])) } {
           .feed.txt insert end "copying: $file"
           .feed.txt insert end \n
           file copy -force $filedir $_paths($cddbase)
        }
     }
   } elseif [string match grd??? $cddbase] { ;#cddbase is grdcrm, grdglb, grdgeb, etc.
     set pathlist [array names _paths]
     #if this grid inventory path exists in pathlist
     if {[lsearch -exact $pathlist $cddbase] != -1} {
       if [file readable $_paths($cddbase)/$cddbase.inv] {
         #get any files already installed
         source $_paths($cddbase)/$cddbase.inv
       }
     }
     switch $cddbase {
       grdcrm {
         set _dbname "NGDC Coastal Relief Model"
         set _blocksize 1
         set _latcellsize 3
         set _loncellsize 3
         set _ngdcprefix gn
         }
       grdglb { 
         set _dbname "Great Lakes Bathymetry"
         set _blocksize 1
         set _latcellsize 3
         set _loncellsize 3
         set _ngdcprefix gn
         }
       grdgeb {
         set _dbname "GEBCO Worldwide Bathymetry"
         set _blocksize 30
         set _latcellsize 60
         set _loncellsize 60
         set _ngdcprefix gw
         }
       grdet2 {
         set _dbname "ETOPO2 Global Gridded Elevation Data"
         set _blocksize 45
         set _latcellsize 120
         set _loncellsize 120
         set _ngdcprefix ge
         }
       default {
         tk_dialog .adialog "CONTACT NGDC!" \
         "Setup Unable to resolve Grid CD: $cddbase" error 0 "OK"
         return 0
         }
     }
     if [string match v10 $cdversion] {
       set xten ".g03" ;#older version CD
     } else {
       set xten ".g98"
     }
     
     if [catch {open $_paths($cddbase)/$cddbase.inv w 0755} fileout] {
       . config -cursor {}
       tk_dialog .adialog "UNABLE TO CREATE/UPDATE GRID INVENTORY FILE!" \
       "Unable to create/update file: $_paths($cddbase)/$cddbase.inv\nCheck permissions" error 0 "OK"
       return 0
     }
     puts $fileout "set _databasename \"$_dbname\""
     puts $fileout "set _blocksize $_blocksize"
     puts $fileout "set _latcellsize $_latcellsize"
     puts $fileout "set _loncellsize $_loncellsize"
     puts $fileout "set _ngdcprefix $_ngdcprefix"

     foreach volume $cdvolumes {   ;#GRDDASE1.V41 GRDDAS02.V10
       set volid [string tolower [string range $volume 0 7]]
       set gridfiles [glob "$cdpaths(cd)/$volid/griddata/*$xten"]
       foreach gridfile $gridfiles {
         set grid [string range [file tail $gridfile] 0 7] ;#e.g. gna48070
         if ![string match template $grid] {
           set _grids($grid) $volume  ;#e.g. set _grids(gna48070) GRDDAS01.V10
         }
       } ;#foreach
       #parray _grids
     }  ;#foreach volume

     foreach grid [lsort -decreasing [array names _grids]] {
       set aline [format "set %s(%s)" _grids $grid]
       set aline "$aline $_grids($grid)" ;#e.g. set _grids(gna48070) GRDDASW1.V41
       puts $fileout $aline
     }

     close $fileout
     .feed.txt insert end "\n\n$cddbase.inv built\n"
   } else {
	tk_messageBox -message "invalid database"
   } ;#if

  .options.cbinv configure -state disabled
  set doInvEnabled 0

  if [string match trkdas $cddbase] {
    set installmess "${installmess}\nMarine Trackline Geophysics Inventory"
  } elseif [string match hydnos $cddbase] {
    set installmess "${installmess}\nNOS Hydrographic Surveys Inventory"
  } elseif [string match trkmec $cddbase] {
    set installmess "${installmess}\nUS East Coast Multibeam Bathymetry Inventories"
  } elseif [string match aromin $cddbase] {
    set installmess "${installmess}\nMinnesota Aeromagnetic Inventories"
  } elseif [string match aronur $cddbase] {
    set installmess "${installmess}\nN.U.R.E.Aeromagnetic Inventories"
  } elseif [string match grdglb $cddbase] {
    set installmess "${installmess}\nGreat Lakes Bathymetry Vol $cdvolnums"
  } elseif [string match grdgeb $cddbase] {
    set installmess "${installmess}\nGEBCO Worldwide Bathymetry Grids"
  } elseif [string match grdet2 $cddbase] {
    set installmess "${installmess}\nETOPO2 Global Gridded Elevations"
  } elseif [string match grdcrm $cddbase] {
    set installmess "${installmess}\nCoastal Relief Model Vol $cdvolnums"
  }

  set doneInv 1
  . config -cursor {}
  update
} ;#end proc copy_inventories


proc install_inputdialog {msg _dlgresponse} {
#creates simple dialog for user to input a line of data
#default is set to incoming "_response"
global confirmStatus
  upvar $_dlgresponse dlgresponse
  toplevel .inputdlg
  set atitle "GEODAS Install"
  wm title .inputdlg $atitle
   set x [expr [winfo screenwidth .inputdlg]/2 - [winfo reqwidth .inputdlg]/2 \
	   - [winfo vrootx [winfo parent .inputdlg]]]
   set y [expr [winfo screenheight .inputdlg]/2 - [winfo reqheight .inputdlg]/2 \
	   - [winfo vrooty [winfo parent .inputdlg]]]
   wm geom .inputdlg +$x+$y

  label .inputdlg.lbl -text $msg
  entry .inputdlg.entry -width 50 -textvariable dlgresponse
  frame .inputdlg.buts
  pack .inputdlg.lbl .inputdlg.entry .inputdlg.buts -side top
  button .inputdlg.buts.ok -text "OK" -borderwidth 5 -command {
    grab release .inputdlg
    destroy .inputdlg
    set confirmStatus 1
  }
  bind .inputdlg <Return> {.inputdlg.buts.ok invoke}
  button .inputdlg.buts.cancel -text "Cancel" -command {
    grab release .inputdlg
    destroy .inputdlg
    set confirmStatus 0
  }

  pack .inputdlg.buts.ok .inputdlg.buts.cancel -side left -padx 10

  tkwait visibility .inputdlg
  grab set .inputdlg
  update
  focus .inputdlg.entry
  tkwait window .inputdlg


  #tk_messageBox -message "confirmStatus: $confirmStatus"

  return $confirmStatus

} ;#proc install_inputdialog
