#!/bin/sh
# The next line is executed by /bin/sh, but not tcl \
exec wish "$0" ${1+"$@"}
###
### ObexTool 0.35
###   (c) Gerhard Reithofer, Techn EDV Reithofer, 2003-2006
###   ObexTool is licensed using the GNU General Public Licence,
###   see http://www.gnu.org/copyleft/gpl.html

set OBX_Debug 0
###
### The ObexTool default directory - change if necessary
###
set ObexTool_defdir /usr/share/obextool

###
### ObexTool Version (moved here from lib/obextool.ini)
###
set OBEXTOOLVERSION 0.35

###
### Ensure that the warp_lines call in the
### common dialogues (obextool.ini) use the global variable 
### $glb_dlg_width - ger
###
set glb_dlg_width 60
option add *Dialog.msg.font {Helvetica -12 bold} userDefault
option add *Dialog.msg.wrapLength 0 userDefault
option add *Dialog.msg.width $glb_dlg_width userDefault

package require BWidget
package require tablelist

###
### ObexTool header message
###
proc obex_header { msg } {
  global OBEXTOOLVERSION 
  puts "ObexTool $OBEXTOOLVERSION"
  puts "  (c) Gerhard Reithofer, Techn EDV Reithofer, 2003-2008"
  puts "  ObexTool is licensed using the GNU General Public Licence,"
  puts "  see http://www.gnu.org/copyleft/gpl.html"
  puts "$msg"
}

###
### Usage message
###
proc obex_usage { args } {
  global argv0
  set exit_code 0
  set alen [llength $args]
  if {$alen>1} { puts "Error: [lindex $args 0]" }
  if {$alen>2} { set exit_code [lindex $args 1] }
  obex_header "Usage: [file tail $argv0] \[opt arg\]..."
  puts " --help"
  puts " --version"
  puts " --obexcmd obexftp-wrapper-command"
  puts " --obexdir obextool-main-directory"
  puts " --obexcfg obextool-config-directory"
  puts " --setconf key value"
  puts " --memstat 0|1"
  puts " --debug level"
  puts " Use the environment value:"
  puts " OBEXTOOL to define an ObexTool main default directory (--obexdir),"
  puts " OBEXCMD to define an obexftp default command string (--obexcmd) and"
  puts " OBEXTOOL_CFG to define a default config directory (--obexcfg)."
  exit $exit_code
}

###
### Check for $OBEXDIR definition:
###
### The OBEXDIR path is currently determined this way, ordered by priority:
### 1. check if the command line option "--obexdir" is used
### 2. check the environment variable called OBEXTOOL is set
### 3. check the contents of the TCL-variable "ObexTool_defdir" 
### 4. check if in the subdirectory "lib" of the directory where 
###    "obextool.tk" is located, a file exists with the name
###    "obextool.ini" - if yes, this is the OBEXTOOL directory
###

# existing startup directory has lowest priority - default
set OBEXDIR [file normalize [file dirname $argv0]]

# $ObexTool_defdir directory overrides startup directory
if {[file exists $ObexTool_defdir/lib/obextool.ini]} {
  set OBEXDIR $ObexTool_defdir
}

# environment value overrides defaults
if {[info exists env(OBEXTOOL)]} { set OBEXDIR $env(OBEXTOOL) } 

# commad line overrides all - MUST be defined correctly
if {$argc} {
  foreach {opt arg} $argv {
    switch -- $opt { 
      --help    { obex_usage }
      --version { obex_header "Use --help for detailed information."
                  exit 0 }
      --obexdir { set OBEXDIR $arg }
        default { }
    }
  }
}

if {![file exists $OBEXDIR/lib/obextool.ini]} {
  puts "$msg"
  puts "ObexTool startup error: unable to find the ObexTool main directory '$dir'!"
  puts "Ensure to start the main executable directly or set the environment"
  puts "variable OBEXTOOL the ObexTool main directory, e.g."
  puts "     export OBEXTOOL=/usr/local/obext01a"
  puts "or use the command line parameter '--obexdir' to define the ObexTool main"
  puts "directory. If you want to startup the ObexTool from another directory,"
  puts "create a shell program, which points to the full pathname of the"
  puts "main executable obextool.tk, file links will NOT work. E.g.:"
  puts "     $ObexTool_defdir/obextool.tk"
  puts "You may also change the default directory variable 'ObexTool_defdir'"
  puts "in the main executable obextool.tk to fit your suits."
  exit 1
}

###
### Check for $OBEXCFG definition:
### The OBEXCFG path (this is new) - where to find the configuration files:
### 1. check if the command line option "--obexcfg" is used
### 2. check the subdirectory "etc" of the main directory (OBEXDIR/etc)
### 3. check the environment variable called OBEXTOOL_CFG is set,
###    which points to the configuration directory
###
set OBEXCFG $OBEXDIR/etc
if {[info exists env(OBEXTOOL_CFG)]} {
  set OBEXCFG $env(OBEXTOOL_CFG)
}
if {$argc} {
  foreach {opt arg} $argv {
    switch -- $opt {
      --obexcfg { set OBEXCFG $arg }
      default   { }
    }
  }
}

###
### Basic initialization of ObexTool
### The last and only dedicated source statement
###
source $OBEXDIR/lib/obextool.ini

###
### Read the main configuration file first
###
read_obextool_config cfg

###
### Environmet $OBEXCMD as config value override and
### --obexcfg default (idea from Hendrik Sattler)
###
if [info exists env(OBEXCMD)] {
  set ObexConfig(obexftp,command) $env(OBEXCMD)
}
###
### Command line overrides:
### --help, --version, --obexdir and --obexcfg are redundant, 
### as they are managed in an earlier state.
###
if $argc {
# foreach [list opt arg] $argv 
  for {set i 0} {$i<$argc} {incr i} {
    set opt [lindex $argv $i] 
    set arg [lindex $argv [incr i]] 
    debug_out "opt=$opt arg=$arg"
    switch -- $opt {
      --help    { obex_usage }
      --version { 
        obex_header "Use --help for command line information." ; exit 0 }
      --obexcmd { set ObexConfig(obexftp,command) $arg }
      --obexdir { set OBEXDIR $arg }
      --obexcfg { set OBEXCFG $arg }
      --memstat { set ObexConfig(config,memstatus) $arg }
      --setconf { set val [lindex $argv [incr i]]
                  set ObexConfig($arg) $val }
      --debug   { set OBX_Debug $arg }
      default   { obex_usage "Invalid option '$opt'" 1 }
    }
  }
  debug_var OBEXCFG
}

###
### Load language dependent messages
###
load_Messages obextool [getObexCfg config language] $OBEXTOOLVERSION

###
### Read all other configuration files
### ext-config uses function 'get_text'
###
read_obextool_config typ
read_obextool_config ext

###
### Now let's load the obextool packages
###
lappend auto_path $OBEXDIR/lib
package require obexfile
package require obexlist
package require obextree

#####################################################
#                                                   #
#  Begin of ObexTool namespace part                 #
#                                                   #
#####################################################

namespace eval ObexTool {
  variable version $::OBEXTOOLVERSION
  variable mainframe
  variable mem_used 0
  variable dl_last_val 0
  variable dl_active_v 0
  variable array file_types 
  variable obexftp_version "<=0.10.3"
  variable mobile_manufact "*unknown*"
  variable mobile_model "*unknown*"
  variable status_prefix {Got info}
  array set file_types [getObexCfg cfg file_types]

  proc text_view { title txt } {
    variable version 
  debug_out "text_view $title \$txt" 3
    set font   [getObexCfg textwin font]
    set width  [getObexCfg textwin width]
    set height [getObexCfg textwin height]
    set top .txt_view

    set blist [list [get_text "&Close"] \
                    [get_text "Close text window"]\
                      "after idle {destroy $top}"] 
    set wtitle [format [get_text "ObexTool %s - text viewer"] $version]
    set sw [new_swindow $top $wtitle $title $blist 0 .]
    debug_var sw
    set tw [text $sw.txt -width $width -height $height -font $font \
                         -wrap none -bg white]
    pack $tw
    $sw setwidget $tw

    $tw insert end $txt
    BWidget::place $top 0 0 center .
    wm deiconify $top
  }

  #########################################################
  #
  #  ObexTool file functions - file interface
  #  ... no error checkin now ... somethin for todo!
  #
  #########################################################
  proc file_put { args } {
    variable file_types
  debug_out "file_put $args" 3

    set type ""
    set name [ObexList::do_select get $ObexList::list_mode]
    if ![string_empty $name] { 
      set path [lindex $ObexList::lb_entries($name) 0]
      set type [lindex $ObexList::lb_entries($name) 1]
    }

    if [string_empty $type] {
      set inpn [read_filename $name]
    } else {
      set inpn [read_filename $name $type]
    }
    if [string_empty $inpn] return

    set dir [ObexTree::curr_dir]
    if [string_empty $dir] return

    set size [file size $inpn]
    set name [file tail $inpn]
    set dest [make_path $dir $name]

    set_cursor on
    if [ObexFile::path_exists dir $dest] {
      set msg [get_text "A folder with the name '%s' already exists!"]
      append msg [get_text "\nCannot upload file."]
      warning [format $msg $dest]
      set_cursor 
      return
    }

    set overwrite 0
    if [ObexFile::path_exists file $dest] {
      set query [get_text "File '%s' already exists!"]
      append query [get_text "\nDo you want to overwrite the existing file?"]
      if ![ask_yes_no [get_text "Name collision"] [format $query $dest]] {
        status_msg [get_text "File upload cancelled!"]
        set_cursor
        return
      }
      set overwrite 1
    }

    if $overwrite { ObexFile::obexftp rm $dest }
    ObexFile::obexftp pf $inpn $dir

    if [ObexFile::path_exists file $dest] {
      set msg [get_text "File '%s' uploaded to '%s'"]
      ObexTree::refresh_list [format $msg $inpn $dest]
    } else {
      set msg [get_text "File '%s' could not be uploaded to '%s'!"]
      no_permission [format $msg $inpn $dest]
    }
    set_cursor
  }

  proc file_rename { args } {
    variable old_name

    if [llength $args] {
  debug_out "llength \$args=[llength $args]" 5
      set name [lindex $args 0]
      set path [make_path [ObexTree::curr_dir] $name]
    } else {
      set name [ObexList::do_select get $ObexList::list_mode]
  debug_var name 5
      if [string_empty $name] {
        warning [get_text "No file or folder selected to delete!"]
        return
      }
      set path [lindex $ObexList::lb_entries($name) 0]
    }

    set old_name $name
    set new_name [filename_dlg [get_text "New file name:"] $name]
    if [string_empty $new_name] return
    debug_var new_name

    if [string_equal $new_name $old_name] {
      warning [get_text "New name is identical to old name!"]
      return
    }

    set dir_name [ObexTree::curr_dir]
    if [string_empty $dir_name] return

    set_cursor on
    set opath [make_path $dir_name $old_name]
    set npath [make_path $dir_name $new_name]
    debug_out "opath=$opath, npath=$npath"
    ObexFile::obexftp mv $opath $npath

    if [ObexFile::path_exists any $npath] {
      set msg [get_text "File '%s' renamed to '%s'."]
      ObexTree::refresh_list [format $msg $opath $npath]
    } else {
      set msg [get_text "File or folder '%s' could not be renamed!"]
      no_permission [format $msg $opath]
    }
    set_cursor
  }

  proc file_delete { args } {
    debug_out "file_delete $args" 3

    if [llength $args] {
  debug_out "llength \$args=[llength $args]" 5
      set name [lindex $args 0]
      set path [make_path [ObexTree::curr_dir] $name]
    } else {
      set name [ObexList::do_select get $ObexList::list_mode]
  debug_var name 5
      if [string_empty $name] {
        warning [get_text "No file or folder selected to delete!"]
        return
      }
      set path [lindex $ObexList::lb_entries($name) 0]
    }
    set type [lindex $ObexList::lb_entries($name) 1]
  debug_var type 4
    if [string_equal $type "folder"] {
      set query [get_text "Do you really want to delete the folder '%s'?"]
    } else {
      set query [get_text "Do you really want to delete the file '%s'?"]
    }
    if ![ask_yes_no [get_text "Confirmation"] [format $query $path]] {
      status_msg [get_text "Delete action cancelled!"]
      return
    }
  debug_var path 3
    set_cursor on
    ObexFile::obexftp rm $path
    if [ObexFile::path_exists any $path] {
      set msg [get_text "File or folder '%s' could not be deleted!"]
      if [string_equal $type "folder"] {
        append msg [get_text "\nFolders must be empty before deleting."]
      }
      no_permission [format $msg $path]
    } else {
      if [string_equal $type "folder"] {
        ObexTree::refresh_list [format [get_text "Folder '%s' deleted."] $path]
      } else {
        ObexTree::refresh_list [format [get_text "File '%s' deleted."] $path]
      }
    }
    set_cursor 
  }

  ### Custom transport set to 'Siemens'
  ### Connecting...done
  ### Receiving info... Got info 233472:
  ### done
  ### Receiving info... Got info 96256:
  ### done
  ### Disconnecting...done
  proc file_status { show_text } {
    global ObexConfig
    variable status_prefix
    variable mainframe
    variable mem_used
    variable status
  debug_out "file_status $show_text" 3
    set total 0
    set mfree 0
    if ![getObexCfg config memstatus] return
    set_cursor on
    set lines [ObexFile::obexftp st]

    foreach line $lines {
  debug_var line
  debug_out "\[string first $status_prefix $line\]=[string first $status_prefix $line]" 
      if {[string first $status_prefix $line]>=0} {
        set nword [lindex [split $line] end]
        set value [string range $nword 0 end-1]
  debug_var value 3
        if $total {
          set mfree $value
        } else {
  debug_var total 3
          set total $value
        }
      }
    }
    if !$total {
      set msg [get_text "It seems, that your device does not"]
      append msg [get_text " support the memory status feature."]
      append msg [get_text "\nMemory status will be disabled."]
      set ObexConfig(config,memstatus) 0
      warning $msg
      set_cursor 
      return
    }
    set mem_used [expr $total-$mfree]
    set mem_stat [format [get_text "Total %4.1f\nUsed %4.1f\nFree %4.1f"] \
        [expr $total/1024.0] [expr $mem_used/1024.0] [expr $mfree/1024.0]]
    DynamicHelp::register .mainframe.status.prg.bar balloon $mem_stat
    $mainframe configure -progressmax $total
    
    set_cursor 
    if $show_text {
      set    msg [get_text " Memory status\n"]
      append msg [get_text "===============\n"]
      append msg [format [get_text "Memory total: %s bytes\n"] $total]
      append msg [format [get_text "Memory used:  %s bytes\n"] $mem_used]
      append msg [format [get_text "Memory free:  %s bytes\n"] $mfree]
      text_view [get_text "Device memory status"] $msg
    }
  }

  proc get_obexftp_ver {} {
    variable obexftp_version
    debug_out "ObexTool::get_obexftp_ver" 5

    set line [ObexFile::obexftp ve]

  # Try `obexftp --help' for more information.
  # ObexFTP 0.10.4-rc3
    if {[string range $line 0 6] eq "ObexFTP"} { 
      set obexftp_version [string range $line 7 end]
    }
  }

  proc filetype_icon { ext args } {
    set postfix icon
    if [llength $args] { set postfix [lindex $args 0] }
    foreach dir $Bitmap::path {
      set img [glob -nocomplain "$dir/${ext}_${postfix}.*"]
      if [llength $img] { return [lindex $img 0] }
    }
    set img [glob -nocomplain "$dir/file_${postfix}.*"]
    if [llength $img] { return [lindex $img 0] }
    return file
  }

  proc file_type { ext } {
    variable file_types

    if ![string_empty $ext] {
      if [string_equal [string index $ext 0] "."] {
        set typ [string range $ext 1 end]
      }
      set ext [string tolower $ext]
    }
    if {[lsearch [array names file_types] $ext] == -1} { 
      return [format [get_text "%s file"] $ext]
    } else {
##### return [get_text $file_types($ext)]
      return $file_types($ext)
    }
  }

  proc addtoolbar { tbar idx blist } {
  debug_out "ObexTool::addtoolbar $tbar $idx $blist" 3
    set bbox [ButtonBox $tbar.bbox$idx -spacing 0 -padx 1 -pady 1]
    foreach but $blist {
      set imgc [lindex $but 0]
      set help [lindex $but 1]
      set cmnd [lindex $but 2]
    debug_out "imgc=$imgc help=$help cmnd=$cmnd"
      $bbox add -image [Bitmap::get $imgc] \
        -highlightthickness 0 -takefocus 0 -relief link -borderwidth 1 \
        -padx 1 -pady 1 -helptext $help -command $cmnd
    }
    pack $bbox -side left -anchor w
    set sep [Separator $tbar.sep$idx -orient vertical]
    pack $sep -side left -fill y -padx 4 -anchor w
  }

  ###
  ### $font does not work !?!?!? - ger
  ###
  proc about {} {
    variable version
    variable obexftp_version
    variable mobile_manufact 
    variable mobile_model 
    set    msg [get_text "\nA program to communicate with mobile phones using"]
    append msg [get_text " the OBEX protocol (like Siemens, Ericsson or Nokia, ...)"]
    set    aut [format "\n%c Gerhard Reithofer, Techn. EDV Reithofer 2003-2008" 169]
    append aut "\ngerhard.reithofer@tech-edv.co.at, http://www.tech-edv.co.at"
    append msg [get_text "\nObexTool is licensed using the GNU General Public Licence,"]
    append msg [get_text "\nsee http://www.gnu.org/copyleft/gpl.html"]
   
  # set obxftpver [ObexFile::obexftp ve]
  #########################################
  #  set  tec "\nObexftp version: $obexftp_version"
  #  set_cursor on
  #########################################
    
    set info .about
    toplevel $info -borderwidth 0 -relief raised -background white
    wm withdraw $info
    wm title $info [get_text "Program information"]

    set logo [Bitmap::get ObexTool]
    set tfr $info
    label $tfr.l -image $logo -borderwidth 0
    set lfont "Helvetica 18 bold"
    set sfont "Helvetica 14"
    set txtf [frame $tfr.txt]
    Label $txtf.t1 -bg "#ffffff" -font $lfont \
          -text [format [get_text "Version %s"] $version]
    Label $txtf.t2 -bg "#ffffff" -font $sfont -wraplength 550 -text $msg 
    Label $txtf.t3 -bg "#ffffff" -font $sfont -wraplength 550 -text $aut 
    set b1 [Button $tfr.ok -text [get_text "Close"] -width 20 \
             -underline 0 -command "after idle {destroy $info}"]
    eval pack [winfo children $txtf] -fill x -expand yes
    eval pack [winfo children $tfr] -padx 50 -pady 20
    wm transient $info .
    BWidget::place $info 0 0 center 
    wm deiconify $info
    grab set .about
    return .about
  }

  proc create_mainframe { } {
    variable toolbar1 1
    variable mainframe
    variable mem_used
    variable status

    # Menu description
    set file_menu [list\
          [list command [get_text "A&bout"] {} \
                [get_text "Program information"] {Ctrl I}\
                -command {ObexTool::about} ]\
          {separator}\
          [list command [get_text "&Upload"] {} \
                [get_text "Upload a file"] {Ctrl U} \
                -command {ObexTool::file_put} ]\
          [list command [get_text "D&ownload"] {} \
                [get_text "Download a file"] {Ctrl O} \
                -command {ObexList::file_get} ]\
          {separator}\
          [list command [get_text "&New"] {} \
                [get_text "Create a new folder"] {Ctrl N} \
                -command {ObexTree::new_folder} ]\
          [list command [get_text "&Find"] {} \
                [get_text "Find file or folder"] {Ctrl W} \
                -command {ObexTree::find_file} ]\
          {separator}\
          [list command [get_text "&Quit"] {} \
                [get_text "Exit Obex tool"] {Ctrl Q} \
                -command exit ]\
      ]
    set edit_menu [list\
          [list command [get_text "&Cut"] {} \
                [get_text "Cut selection"] {Ctrl X} \
                 -command {ObexList::menu_edit cut} ]\
          [list command [get_text "&Copy"] {} \
                [get_text "Copy selection"] {Ctrl C} \
                -command {ObexList::menu_edit copy} ]\
          [list command [get_text "&Paste"] {} \
                [get_text "Paste selection"] {Ctrl V} \
                -command {ObexList::menu_edit paste} ]\
          {separator}\
          [list command [get_text "&Delete"] {} \
                [get_text "Delete selection"] {Ctrl D} \
                -command {ObexTool::file_delete} ]\
      ]
    if [getObexCfg config filemove] {
      lappend edit_menu \
         [list command [get_text "&Rename"] {}\
               [get_text "Rename selection"] {} \
              -command {ObexTool::file_rename} ]
    }
    set view_menu [list\
          [list command [get_text "&Folder up"] {} \
            [get_text "Up one folder level"] {} \
            -command {ObexTree::up_level}]\
          [list command [get_text "&Reload"] {} \
            [get_text "Reread folder contents"] {Ctrl R} \
            -command {ObexTree::refresh_list ""}]\
          [list command [get_text "&Properties"] {} \
            [get_text "File or folder information"] {Ctrl P} \
            -command {ObexList::show_props} ]\
      ]
    if [getObexCfg config memstatus] {
      lappend view_menu \
          {separator}\
          [list command [get_text "&Memory"] {} \
            [get_text "Memory status"] {Ctrl M} \
            -command {ObexTool::file_status 1} ]
    }
    set opts_menu [list\
          [list checkbutton [get_text "&Toolbar"] {all option} \
              [get_text "Show/hide toolbar"] {}\
              -variable ObexTool::toolbar1 \
              -command  {$ObexTool::mainframe showtoolbar 0 \
                         $ObexTool::toolbar1}\
          ]\
          {separator}\
          [list radiobutton [get_text "&Large icons"] {} \
                       [get_text "Display large icons"] {}  \
                       -command {ObexList::update_list icons}      \
                       -variable ObexTool::list_mode -value icons  ]\
          [list radiobutton [get_text "&File list"]   {} \
                       [get_text "Display file list"]  {}   \
                       -command {ObexList::update_list listb}      \
                       -variable ObexTool::list_mode -value listb  ]\
          [list radiobutton [get_text "&Details"]     {} \
                       [get_text "Display file details"]    {}   \
                       -command {ObexList::update_list detail}     \
                       -variable ObexTool::list_mode -value detail ]\
      ]
    set mainmenu [list \
      [get_text "&File"   ] all file 0 $file_menu\
      [get_text "&Edit"   ] all edit 0 $edit_menu\
      [get_text "&View"   ] all view 0 $view_menu\
      [get_text "&Options"] all options 0 $opts_menu\
    ]
  ### create mainframe, menues, toolbars, status line and status bar 
    if [getObexCfg config memstatus] {
      set mainframe [MainFrame .mainframe -menu $mainmenu \
                         -progressfg [getObexCfg gui status_bg] \
                         -progressvar ObexTool::mem_used \
                         -textvariable ObexTool::status]

  ### Next line is "hard coded" because the BWidgets have no method
  ### for retrieving the progress bar window path - ger
      .mainframe.status.prg.bar configure -bg [getObexCfg gui status_fg]
      $mainframe showstatusbar progression
    } else {
      set mainframe [MainFrame .mainframe -menu $mainmenu \
                         -textvariable ObexTool::status]
    }
  ### toolbar creation
    set tb1  [$mainframe addtoolbar]
    addtoolbar $tb1 1 [list \
      [list upload   [get_text "Upload a file"]  \
            {ObexTool::file_put}]\
      [list download [get_text "Download a file"]\
            {ObexList::file_get}]\
    ]
    addtoolbar $tb1 2 [list \
      [list edit_cut [get_text "Cut selection"]  \
            {ObexList::menu_edit cut  }]\
      [list editcopy [get_text "Copy selection"] \
            {ObexList::menu_edit copy }]\
      [list editpast [get_text "Paste selection"]\
            {ObexList::menu_edit paste}]\
    ]
    addtoolbar $tb1 3 [list \
      [list foldernew [get_text "Create a new folder"]\
            {ObexTree::new_folder}]\
      [list uplevel   [get_text "Up one folder level"]\
            {ObexTree::up_level  }]\
      [list filefind  [get_text "Find file or folder"]\
            {ObexTree::find_file }]\
    ]
    addtoolbar $tb1 4 [list \
      [list reload   [get_text "Reread folder contents"]    \
            {ObexTree::refresh_list ""}]\
      [list fileinfo [get_text "File or folder information"]\
            {ObexList::show_props     }]\
      [list editdele [get_text "Delete selection"]          \
            {ObexTool::file_delete    }]\
    ]
    addtoolbar $tb1 5 [list \
      [list iconlarg [get_text "Display large icons"] \
            {ObexList::update_list icons }]\
      [list iconlist [get_text "Display file list"]   \
            {ObexList::update_list listb }]\
      [list icondetl [get_text "Display file details"]\
            {ObexList::update_list detail}]\
    ]

    # Mainframe creation
    set frame [$mainframe getframe]
    set topframe [frame $frame.widget]

  # ObexTree::create $topframe
    pack $topframe -fill both -expand yes -padx 4 -pady 4
    pack $mainframe -fill both -expand yes -side top
    update idletasks
    return $topframe
  }

  proc main { w args } {
    variable version
    variable setup_variable
    variable setup_values
    variable top_window $w

    set wsize [getObexCfg gui initsize]
    debug_var wsize
    wm withdraw $w

    wm title $w "ObexTool $version"

    set widget [create_mainframe]
    set list_w [ObexTree::create $widget]
    set l_mode [ObexList::create $list_w]

    BWidget::place $w 0 0 center 
    raise $w
    focus -force $w
    wm deiconify $w
    wm geometry $w $wsize
    debug_out "wm geometry $w $wsize"
    ObexTool::get_obexftp_ver
  }
}

### MAIN PART BEGIN ###
###
### Adding our private icon path to search path - ger
###
Bitmap::get cut
lappend Bitmap::path $OBEXDIR/images

set top .
ObexTool::main $top $argv
if [info exists OBX_Debug] {bind $top <ButtonPress-3> "debug_win %W"}

ObexTree::init

after idle {
  ObexList::update_list [getObexCfg list initmode]
  eval "DynamicHelp::configure [getObexCfg gui help_options]"
  ObexTool::file_status 0
}

