#!/bin/sh
# the next line restarts using tclsh \
TCL_LIBRARY=$SMV_DIR/lib/smv/tcl_library TK_LIBRARY=$SMV_DIR/lib/smv/tk_library TIX_LIBRARY=$SMV_DIR/lib/smv/tix_library  exec $SMV_DIR/lib/smv/tixwish "$0" "$@"

# puts "using smv in $smvdir..."
set smvdocdir [file join $env(SMV_DIR) doc/smv]
# puts "looking for tutorial in $smvdocdir..." 

if {[string compare $tcl_platform(os) Darwin] == 0} {
    exec osascript -e "open location \"file:$smvdocdir/tutorial/tutorial.html\""
    exit 0
}

if {0} {

    # make a temporary directory
    
    set count 0
    set tempfile /tmp/smvtut0
    while {[file exists $tempfile]} {
	incr count
	set tempfile /tmp/smvtut$count
    }
    
#    puts "making a temporary copy of the tutorial in $tempfile..."
    exec mkdir $tempfile
    exec /bin/sh -c "(cd $smvdocdir; tar cf - tutorial examples) | (cd $tempfile; tar xf -)"
}


if {[catch {exec grep smv $env(HOME)/.mime.types}]} {
#  puts "configuring netscape..."
  if {![catch {file type $env(HOME)/.netscape/lock}]} {
    puts stderr "A file $env(HOME)/.netscape/lock was detected."
    puts stderr "This means that you may currently have Netscape running.\n"
    puts stderr "In order that Netscape's MIME types may be configured to"
    puts stderr "run the SMV tutorial, you must first exit netscape and"
    puts stderr "try the tutorial again.\n"
    puts stderr "If Netscape is not running, then just delete the file"
    puts stderr "$env(HOME)/.netscape/lock."
    exit 1
  }

#    puts "adding mime types..."
    set foo [open $env(HOME)/.mime.types a+]
    puts $foo "\#mime types added by SMV"
    puts $foo "type=application/smv  \\"
    puts $foo "desc=\"SMV language source file\"  \\"
    puts $foo "exts=\"smv,v\""
    close $foo
#    puts "adding helper applications..."
    set foo [open $env(HOME)/.mailcap a+]
    puts $foo "\#mailcap entry added by SMV"
    puts $foo "application/smv;vw %s >/dev/null"
    close $foo
}

#puts "making temporary files..."
exec touch /tmp/.smv_history
exec chmod 666 /tmp/.smv_history
exec touch /tmp/.smv_lock
exec chmod 666 /tmp/.smv_lock

#puts "running netscape..."

#puts "netscape -remote openURL(file:$smvdocdir/tutorial/tutorial.html)"
if {[catch {exec netscape -remote openURL(file:$smvdocdir/tutorial/tutorial.html)}]} {
    exec xdg-open $smvdocdir/tutorial/tutorial.html
}

#puts "cleaning up /tmp"
exec rm -rf /tmp/.smv_history /tmp/.smv_lock

