#!/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: sddsplot3 [-notrim] <outputRootname> <sddsplotArguments> }
set args $argv
if {[llength $args]==0} {
    return -code error "$usage"
}
if {[lindex $args 0] == "-notrim"} {
    set trim 0
    set args [lreplace $args 0 0]
    set argv $args
} else {
    set trim 1
}

foreach colorFile {blackBGColors.sdds 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 
set colorsOptMotif ",(-linetype $env(HOME)/blackBGColors.sdds)"
if [file exists whiteBGColors.sdds] {
    set colorsOpt ,linetypetable=whiteBGColors.sdds 
}
if [file exists blackBGColors.sdds] {
    set colorsOptMotif ",(-linetype blackBGColors.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 colorsOptMotif ""
    }
}

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 sddsplots:
# motif display
# PNG file
# EPS file

catch {eval exec sddsplot \"-device=motif${colorsOptMotif}\" \
  [lrange $argv 1 end] &}

if {$trim} {
    if [catch {eval exec sddsplot -device=hpng,onwhite$colorsOpt \
                 -output=[lindex $argv 0].tmp.png \
                 [lrange $argv 1 end] } result] {
        puts stderr "$result"
    }
    exec convert +repage -trim +repage -background white -flatten [lindex $argv 0].tmp.png [lindex $argv 0].jpg
    exec convert [lindex $argv 0].jpg [lindex $argv 0].png 
    file delete [lindex $argv 0].tmp.png  [lindex $argv 0].jpg
} else {
    if [catch {eval exec sddsplot -device=hpng,onwhite$colorsOpt \
                 -output=[lindex $argv 0].png \
                 [lrange $argv 1 end] } result] {
        puts stderr "$result"
    }
}

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


