#!/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.7 2000/07/11 13:19:45 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 tkdvi0.3.1]}]
set tkdvi(binlib) [expr {[info exists env(TKDVI_BINLIB)] \
	? $env(TKDVI_BINLIB) : [file join ${exec_prefix}/lib tkdvi0.3.1]}]

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

set tkdvi_version {0.3.1 (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

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
	}
	-pr* {
	    set arg(-shrink) 3
	    set arg(-mode) single
	    set arg(-size) a6r
	    set arg(-fullscreen) 1
	    set arg(-scrollbars) {}
	    set arg(-menubar) 0
	    set arg(-toolbar) 0
	    set arg(-pageselector) 0
	    set arg(-b1action) draw
	}
	default {
	    if {[string match -* $a]} {
		set arg($a) [lindex $argv $i]
		incr i
	    } else {
		if {![file exists $a] \
			&& [string length [file extension $a]]==0} {
		    append a ".dvi"
		}
		if {[catch {set f [dvi::code create -file $a]} msg]} {
		    puts stderr "$a: $msg"
		} else {
		    eval [list tkdvi::browser::browser .dvi] -file $f \
			    [array get arg]
		    set success 1
		}
	    }
	}
    }
}

if {$success == 0} { exit }
