#!/bin/sh
# The next line is executed by /bin/sh, but not tcl \
exec wish $0 ${1+"$@"}

# TkDVI
# Copyright  1999 Anselm Lingnau <lingnau@tm.informatik.uni-frankfurt.de>.
# See file COPYING for conditions for use and distribution.
# $Id: tkdvi.in,v 1.2 1999/06/16 07:44:53 lingnau Exp $

# Keep Autoconf happy.
set prefix /usr
set exec_prefix ${prefix}

set tkdvi(tcllib) [expr {[info exists env(TKDVI_TCLLIB)] \
	? $env(TKDVI_TCLLIB) : [file join ${prefix}/share tkdvi]}]
set tkdvi(binlib) [expr {[info exists env(TKDVI_BINLIB)] \
	? $env(TKDVI_BINLIB) : [file join ${exec_prefix}/lib tkdvi]}]

lappend auto_path $tkdvi(tcllib) $tkdvi(binlib)
lappend auto_path [file join $env(HOME) .tk tkdvi]

set tkdvi_version {0.2 (Alpha test)}

wm group . .
wm command . "$argv0 $argv"
wm withdraw .

option readfile [file join $tkdvi(tcllib) tkdvi.ad] startup
if {[file exists [file join $env(HOME) .tkdvi-resources]]} {
    option readfile [file join $env(HOME) .tkdvi-resources] startup
}

catch { userInit }

package require tkdvi
tkdvi::version $tkdvi_version

array set arg [list -size a4 -mode single -shrink 8]

set success 0
set i 0
while {$i < [llength $argv]} {
    set a [lindex $argv $i]
    incr i
    switch -glob -- $a {
	-pa* {
	    set arg(-size) [lindex $argv $i]
	    incr i
	}
	-sp* {
	    set arg(-mode) spread
	}
	-si* {
	    set arg(-mode) single
	}
	-s* {
	    set arg(-shrink) [lindex $argv $i]
	    incr i
	}
	-o* {
	    set arg(-mode) overview
	}
	default {
	    if {![file exists $a] && [string length [file extension $a]]==0} {
		append a ".dvi"
	    }
	    if {[catch {set f [dvi::file open $a]} msg]} {
		puts stderr "$a: $msg"
	    } else {
		eval [list tkdvi::browser::openWithFile {}] $f [array get arg]
		set success 1
	    }
	}
    }
}

if {$success == 0} { exit }