#############################################################################
# CDMS: Interface to CDMS-simulator
#   (c) 1997/2000: Rolf Schrödter, DLR, Institut für Planetenerkundung
#############################################################################
# $Id: Browse.tcl,v 1.3 2006-11-17 16:38:06+01 rolf Exp rolf $
#############################################################################
# Browse.tcl :    Browser for existing HK/Data files
#############################################################################



#############################################################################
# Command line options:
# 1. Interactive mode:
#      wish.exe Browse.tcl
# 2. Batch mode (automatic):
#      wish.exe Browse.tcl command args
#    Example:
#      wish.exe Browse.tcl list *rolis.dat
#      wish.exe Browse.tcl extract -file Europa-5-rolis.dat -out Europa-5-img.dat -pattern *ROLIS*DATA*type=Compressed*
#      wish.exe Browse.tcl uncompress -file Europa-5-rolis.dat -out Europa-5-img.afl -format astro
#
#############################################################################

#############################################################################
# Features:
#   - Callbacks for internal data representation
#   - Separate interfaces for ASCII / Windowed visualisation
#############################################################################

namespace eval browse {    ;# create namespace
    variable BROWSE
    set BROWSE(VersionNo)   1.40
    set BROWSE(VersionDate) 17-Nov-2006
}

proc browse::errorMsg { msg } {
    puts stderr  $msg
    catch {
        console show
        tkwait window [toplevel .]
    }
    exit 1
}
proc browse::helpMsg {} {
    variable BROWSE

    set exe [file tail [info nameofexe]]
    set kit [file tail $::starkit::topdir]

    puts stderr  "
    Rosetta Rolis Quick-Look Viewer/Decoder/Analyzer
    Version $BROWSE(VersionNo) $BROWSE(VersionDate)

    Interactive browser mode:
        $exe $kit

    Commandline (batch) mode:
        $exe $kit command args

    Valid commands are:
        browse     - Interactive browser
        list       - List file contents, 1 line per frame
        extract    - Extract selected data frames
        hk         - Extract housekeeping frames
        uncompress - Uncompress (wavelet) image data
        unpack     - Unpack raw image data

    For additional help on command options:
        $exe $kit command
    "
}

package require starkit

set WRAP       [starkit::startup]
set LIBDIR     [file dirname [info script]]
set EXEDIR     [file dirname $::starkit::topdir]

package ifneeded Decoder 0.2 {
    switch -glob $tcl_platform(os) {
        Windows* {
                load [file join $EXEDIR Decoder.dll]
#               load [file join $LIBDIR Source Decoder Debug Decoder.dll]   ;# debugging
        }
        Solaris* -
        SunOS* -
        Linux* {
                load [file join $EXEDIR decoder.so]
        }
        default {
            error "Platform $tcl_platform(os) not supported - need recompilation of decoder-lib"
        }
    }
}

# CATCH the whole script
#
set err [catch {

source [file join $LIBDIR BrowseInit.tcl]
source [file join $LIBDIR BrowseError.tcl]
source [file join $LIBDIR BrowseCDMS.tcl]
source [file join $LIBDIR BrowseList.tcl]
source [file join $LIBDIR BrowseRolisData.tcl]
source [file join $LIBDIR BrowseRolisHK.tcl]
source [file join $LIBDIR BrowseExtract.tcl]
source [file join $LIBDIR BrowseSort.tcl]
source [file join $LIBDIR BrowseISB.tcl]
source [file join $LIBDIR BrowseUncompress.tcl]
source [file join $LIBDIR BrowseUnpack.tcl]

set WISH    [info exists tk_version]
set fname   ""

if { $argc && [llength $argv] } {
    # if command line args specified
    set gui  0
    puts "Version $browse::BROWSE(VersionNo) $browse::BROWSE(VersionDate) "
    set cmd  [string tolower [lindex $argv 0]]
    set args [lrange $argv 1 end]
    switch -glob -- $cmd {
        -*          { browse::helpMsg }
        browse      { set gui 1; set fname [lindex $argv 1]}
        list        { browse::files $args }
        extract     { eval browse::extract $args }
        hk          { eval browse::hk::doit $args }
        sort        { eval browse::sort $args }
        uncompress  { eval uc::uncompress $args }
        unpack      { eval up::unpack $args }
        version     { }
        default     { set gui 1 }
    }
} else {
    set gui 1
}

if { ! $gui && $WISH } {
    package require Tk
    wm iconify .
    console show
    console eval { .console configure -font {Courier 10} }
    tkwait window .
} elseif { $gui } {
    if { ! $WISH } {
        browse::helpMsg
        exit
    }
    # if no command line args --> need WISH for interactive
    # proceed in interactive browser mode
    source [file join $LIBDIR EffNotebook.tcl]
    source [file join $LIBDIR BrowseWindow.tcl]

    browse::browse $fname
}

} errorMsg ]  ;# CATCH the whole script
if {$err} {
    puts stdout [format "\n*** error in script ***\n%s\n\n%s" $errorMsg $::errorInfo]
}
