#!/bin/sh  
# \
exec oagtclsh "$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)]
#APSStandardSetup

set usage {usage: sddscontour3 <outputRootname> <sddscontourArguments> }
set args $argv
if {[llength $args]==0} {
    return -code error "$usage"
}

foreach colorFile {whiteBGColors.sdds whiteBGColors-eps.sdds} {
    set found 1
    if ![file exists $env(HOME)/$colorFile] {
        set found 0
        foreach location {/usr/local/oag/apps/configData/utils /home/oxygen/BORLAND/bin /home/phoebus/BORLAND/bin /home/borland/bin /clhome/BORLAND/bin} {
            if [file exists $location/$colorFile] {
                puts stderr "Copying $location/$colorFile to $env(HOME)"
                file copy $location/$colorFile $env(HOME)/
                set found 1
                break
            }
        }
    }
    if !$found {
        puts stderr "Error: Didn't find $colorFile in $env(HOME) or any of borland's directories"
        exit 1
    }
}

set colorsOpt ,linetypetable=$env(HOME)/whiteBGColors.sdds 
if [file exists whiteBGColors.sdds] {
    set colorsOpt ,linetypetable=whiteBGColors.sdds 
}
set colorsOptEps $colorsOpt
if [file exists $env(HOME)/whiteBGColors-eps.sdds] {
    set colorsOptEps ,linetypetable=$env(HOME)/whiteBGColors-eps.sdds 
}
if [file exists whiteBGColors-eps.sdds] {
    set colorsOptEps ,linetypetable=whiteBGColors-eps.sdds 
}

foreach item [lrange $argv 1 end] {
    if [string match "*order=*" $item] {
        set colorsOpt ""
        set colorsOptEps ""
    }
}

set inUse 0
if [file exists [lindex $argv 0].png] {
    puts stderr "error: in use: [lindex $argv 0].png"
    set inUse 1
}
if [file exists [lindex $argv 0].eps] {
    puts stderr "error: in use: [lindex $argv 0].eps"
    set inUse 1
}
if $inUse {
    exit
}

# Make 3 sddscontours:
# motif display
# PNG file
# EPS file


catch {eval exec sddscontour -device=motif$colorsOpt \
  [lrange $argv 1 end] &}

if [catch {eval exec sddscontour -device=hpng,onwhite$colorsOpt \
  -thick=4 -output=[lindex $argv 0].tmp.png \
  [lrange $argv 1 end] } result] {
        puts stderr "$result"
}

exec convert -trim [lindex $argv 0].tmp.png [lindex $argv 0].png 
file delete [lindex $argv 0].tmp.png 

catch {eval exec sddscontour -device=ceps$colorsOptEps -thick=2 \
  -output=[lindex $argv 0].eps \
  [lrange $argv 1 end] &}


