#!/usr/bin/tclsh8.5
#
# $Id: 3acebc3dc3ba7c640d60adf33a3c9f2f41a4329f $
#
###########################################################################
# usage: nbsppngcvt [-g] file.png
#
# The program writes the name of the output file (file.jpg) to stdout,
# or an error message in case of an error. The program returns an exit code 
# of 0 if there were no errors, or 1 otherwise. With the [-g] option
# the conversion is to gif.
#
# The netpbm tools pngtopnm, pnmtojpeg and ppmtogif are required.
#
# This file is distributed with nbsp.
###########################################################################
package require cmdline;

set usage {nbsppngcvt [-g] <file.png>};
set optlist {{g 0}};

array set option [::cmdline::getoptions argv $optlist $usage];

## The common defaults file (this script uses netpbm programs)
source "/usr/local/etc/nbsp/filters.conf";

package require nbsp::errx;

## Defaults for this filter.
set jpgextension ".jpg";
set gifextension ".gif";

#
# main
#
if {$argc == 0} {
    ::nbsp::errx::err $usage;
} else {
    set fpath [lindex $argv 0];
}

set rootname [file rootname $fpath];
if {$option(g) == 0} {
    append fpathout $rootname $jpgextension;
    set prog2 $filtersprogs(pnmtojpeg);
} else {
    append fpathout $rootname $gifextension;
    set prog2 $filtersprogs(ppmtogif);
}
set prog1 $filtersprogs(pngtopnm);

set status [catch {exec $prog1 $fpath | $prog2 > $fpathout} errmsg];

# ppmtogif writes to stderr by default. So we don't check
# the status but whether or not the output file was produced.
if {[file exists $fpathout] == 0} {
    set status 1;
    puts $errmsg;
} else {
    set status 0;
    puts $fpathout;
} 

return $status;
