#!/bin/sh  
# \
exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

proc BuildPS {args} {
    global DirList workingFile
    set w .workdir.main
    foreach s [array names DirList $w:*:name] {
        set f $DirList($s)
        if {$DirList($w:$f:selected)} {
            if {[string tolower [file extension $f]] == ".tex"} {
                .workdir.bottom.tex.b1 configure -state disabled
                update
                set workingFile [file rootname [file tail $f]]
                APSExecLog .build \
                  -unixCommand "latex $workingFile" \
                  -callback BuildPSStep1Done \
                  -abortCallback BuildPSStep1Done \
                  -cancelCallback BuildPSStep1Done
            }
        }
    }
}

proc BuildPSStep1Done {args} {
    global workingFile
    APSExecLog .build \
                  -unixCommand "latex $workingFile" \
                  -callback BuildPSStep2Done \
                  -abortCallback BuildPSStep2Done \
                  -cancelCallback BuildPSStep2Done
}
proc BuildPSStep2Done {args} {
    global workingFile
    APSExecLog .build \
                  -unixCommand "dvips $workingFile -o ${workingFile}.ps" \
                  -callback BuildPSDone \
                  -abortCallback BuildPSDone \
                  -cancelCallback BuildPSDone
}
proc BuildPSDone {args} {
    change_dir [pwd]
    .workdir.bottom.tex.b1 configure -state normal
}

proc BuildPDF {args} {
    global DirList
    set w .workdir.main
    foreach s [array names DirList $w:*:name] {
        set f $DirList($s)
        if {$DirList($w:$f:selected)} {
            if {[string tolower [file extension $f]] == ".tex"} {
                .workdir.bottom.tex.b2 configure -state disabled
                update
                APSExecLog .build \
                  -unixCommand "pdflatex $f" \
                  -callback BuildPDFDone \
                  -abortCallback BuildPDFDone \
                  -cancelCallback BuildPDFDone
            }
        }
    }
}

proc BuildPDFDone {args} {
    change_dir [pwd]
    .workdir.bottom.tex.b2 configure -state normal
}

APSStandardSetup

if {[catch {exec which tkcvs} tkcvs]} {
    APSAlertBox .alert -errorMessage "tkcvs is not installed on this system." -type error -beep once
    exit
}
if {[lindex $tkcvs 0] == "no"} {
    APSAlertBox .alert -errorMessage "tkcvs is not installed on this system." -type error -beep once
    exit
}
source $tkcvs
set cvscfg(editor) "emacs"

frame .workdir.bottom.tex
pack .workdir.bottom.tex -fill x

button .workdir.bottom.tex.b1 -text "Build .PS from .TEX" -command BuildPS
pack .workdir.bottom.tex.b1 -side left
button .workdir.bottom.tex.b2 -text "Build .PDF from .TEX" -command BuildPDF
pack .workdir.bottom.tex.b2 -side left

if {[file exists /opt/local/share/texmf-dist/tex/latex]} {
    set env(TEXINPUTS) .:/home/oxygen/EMERY/mytex:/opt/local/share/texmf-dist/tex/latex//
} else {
    set env(TEXINPUTS) ".:/home/oxygen/EMERY/mytex:"
}
