#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

# tget --
# A frontend to "wget", a program to get ftp/http files.
# By Paul Rahme [Mr. Bogus] -- (paulrahme@hotmail.com) 1998.
# Thanks to Hrvoje Niksic, Lindsay Marshall & Ken Corey.

wm title . "Tget : Wget frontend"
wm minsize . 1 1


frame .output -relief raised -borderwidth 1
   label .output.lbl -text "Output window"
   text .output.t -height 10 -yscrollc {.output.sb set}
   scrollbar .output.sb -command {.output.t yview}
   pack .output.lbl -side top
   pack .output.sb -side right -fill y
   pack .output.t -side bottom -fill both -expand 1
pack .output -side bottom -fill x

frame .buttons
pack .buttons -side bottom -fill x -pady 2m
button .buttons.quit -text "Quit" -command {
    destroy .
}
pack .buttons.quit -side right

button .buttons.ok -text "Go get it!" -command {
    set url [.url.urlName get]
    set file [.file.fileName get]

    if {[expr "{$inputFile} == {on}"]} then {
        set url "-i $file"
    }

    set tries [.options.tries.ent get]
    set level [.options.level.ent get]

    if {[expr "{$filter} == {on}"]} then {
        set filters "-A [.options.filter.ent get]"
    } else {
        set filters ""
    }

    if {[expr "{$exclude} == {on}"]} then {
        set excludes "-X [.options.exclude.ent get]"
    } else {
        set excludes ""
    }

    set xx [ open "|wget $logfile $recursive -t $tries -l $level $continue $noparent $filters $excludes $relative $timestamp $url" r]

    fileevent $xx readable {
       if {[eof $xx]} {
           close $xx
           .output.t insert end "got $url."
       } else {
           set thisLine [gets $xx]
           .output.t insert end "$thisLine\n"
           .output.t yview end
           update idletasks
       }
    }
}
pack .buttons.ok -side bottom

frame .url -relief raised -borderwidth 1
   label .url.urlHeading -text "Enter a URL to get: " -anchor e
   entry .url.urlName -width 65
pack .url.urlHeading .url.urlName -side top

frame .file -relief raised -borderwidth 1
   frame .file.topHalf
      label .file.topHalf.fileHeading -text "Rather take a list of URL's from an input file: " -anchor e
      checkbutton .file.topHalf.listFile -onvalue "on" -offvalue "off" -variable inputFile -relief flat
   pack .file.topHalf.listFile .file.topHalf.fileHeading -side left
entry .file.fileName -width 50
button .file.but -text "Browse ..." -command "fileDialog . .file.fileName open"
pack .file.topHalf -side top
pack .file.but .file.fileName -side right -fill x

pack .url -side top -fill x
pack .file -side top -fill x

proc fileDialog {w ent operation} {
    set types {
        {"All files"            *}
    }

    set file [tk_getOpenFile -filetypes $types -parent $w]

    if [string compare $file ""] { 
        $ent delete 0 end
        $ent insert 0 $file
        $ent xview end
    }
}

frame .options -relief raised -borderwidth 1
   label .options.optHeading -text "Options" -anchor e
      checkbutton .options.recurse -onvalue "-r" -offvalue "" -variable recursive \
         -text "Recurse into all links followed from the page (subdir's for an ftp:// address)"
   frame .options.level
      label .options.level.heading -text "Number of levels to recurse (0 for infinite)"
      entry .options.level.ent -width 3
   pack .options.level.ent .options.level.heading -side left
   checkbutton .options.logfile -onvalue "-olog.txt" -offvalue "" -variable logfile \
      -text "Create a logfile in the current directory (./log.txt) with no output in window"
   frame .options.tries
      label .options.tries.heading -text "Number of retries (0 for infinite)"
      entry .options.tries.ent -width 3
   pack .options.tries.ent .options.tries.heading -side left
   checkbutton .options.continue -onvalue "-c" -offvalue "" -variable continue \
      -text "Continue a download that died before it finished"
   checkbutton .options.timestamp -onvalue "-N" -offvalue "" -variable timestamp \
      -text "Only download new/newer files or where file size differs"
   checkbutton .options.relative -onvalue "-L" -offvalue "" -variable relative \
      -text "Follow only relative links"
   frame .options.filter
   checkbutton .options.filter.but -onvalue "on" -offvalue "off" -variable filter \
      -text "Filter file extensions to (list seperated by commas eg. gif,jpeg,jpg) :"
      entry .options.filter.ent
   pack .options.filter.but .options.filter.ent -side left
   frame .options.exclude
   checkbutton .options.exclude.but -onvalue "on" -offvalue "off" -variable exclude \
      -text "Exclude files with extensions (list seperated by commas eg. gif,jpeg,jpg) :"
      entry .options.exclude.ent
   pack .options.exclude.but .options.exclude.ent -side left
   checkbutton .options.noparent -onvalue "--no-parent" -offvalue "" -variable noparent \
      -text "Do not ascend to parent directory (ie. ignore ../ link in ftp)"
pack .options.optHeading -side top
pack .options.recurse -side top -anchor w
pack .options.level -side top -anchor w
pack .options.relative -side top -anchor w
pack .options.logfile -side top -anchor w
pack .options.tries -side top -anchor w
pack .options.continue -side top -anchor w
pack .options.timestamp -side top -anchor w
pack .options.filter -side top -anchor w
pack .options.exclude -side top -anchor w
pack .options.noparent -side top -anchor w

.options.level.ent insert 0 0
.options.tries.ent insert 0 0

pack .options -side top -fill x

