#!/usr/bin/tclsh

proc printUsage {} {
   puts ""
   puts "Image Conversion Utility 1.02  -- Feb 14th 1997 (MS)"
   puts ""
   puts "  This program does no actual conversion itself, but instead adds"
   puts "  a simpler and more versatile commandline interpreter to the"
   puts "  Wavefront 'imgcvt' utility.  Images may be converted one at a"
   puts "  time or multiple images of many different formats can be converted"
   puts "  simultaneously to a single output file format."
   puts ""
   puts "     Usage:  cv <image-format> \[-q percent\] filespec1 \{filespec2 ...\}"
   puts ""
   puts "             The '-q <percent>' is optional and may be used to specify"
   puts "             a quality of JPEG output.  If this option is not specified,"
   puts "             the quality for output will default to 100%"
   puts ""
   puts "  Any number of filespecs may be used.  Wildcard substitution will be performed"
   puts "  by the current command shell, so use wildcards just as you would for the"
   puts "  'ls' command."
   puts ""
   puts "  Valid input/output image formats are TGA, TIF, YUV, ALS (Alias), JPG, RGB,"
   puts "  TIF, RLA.  GIF input support may be added in the future."
   puts ""
   puts "  examples:   cv rgb image.tif        (converts 1 image to rgb format)"
   puts "              cv jpg *rgb *tif        (converts all rgb and tif images to jpg)"
   puts "              cv jpg -q 75 *rgb *tif  (same as above except that JPEG output"
   puts "                                       will be reduced to 75%)"
}

set msg ""
set jpgQ -1
set validFmt { yuv als jpg rgb tif rla tga}
set firstFile 1

# check for command line arguments - exit if none
if { $argc <= 1 } {
   printUsage
   puts ""
   puts "error: There must be at least 2 command line arguments"
   puts ""
   exit
}

# check requested output format against valid formats
set i 0
set ok 1
while { 1 } {
   if { ( $i <= [expr [llength $validFmt] - 1] ) }  {
      set ok [string compare [ lindex $argv 0 ] [ lindex $validFmt $i ] ]
      incr i 1
   } else { break }

   if { $ok == 0 } break
}


#while { ( $ok != 0 ) && ( $i <= [expr [llength $validFmt] - 1] ) }  {
#   set ok [string compare [ lindex $argv 0 ] i] 
#   incr i 1
#}

# exit if FMT argument does not match any valid arguments
if {$ok != 0} {
   set msg [lindex $argv 0]
   printUsage
   puts ""
   puts "error: unknown output file format $msg"
   puts ""
   set msg ""
   exit
}

if { [string compare [ lindex $validFmt [expr $i - 1] ] "jpg"] == 0 }  {
   if { [string compare [ lindex $argv 1 ] "-q"] == 0 }  {
      set jpgQ [lindex $argv 2]
      puts "Using [lindex $argv 2]% quality for jpg output."
      set firstFile 3
   } else {
      set jpgQ 100
      puts "Assuming 100% quality for jpg output."
   }
}

### begin loop to process each command line input filespec

set x $firstFile

while { $x <= [expr $argc - 1] }   {

   set outFile ""
   set inFile [lindex $argv $x]

   # check is input file actually exists
   if [ file isfile $inFile ] {

      append outFile [file rootname $inFile] "." [ lindex $validFmt [expr $i - 1] ]

      if { ($inFile == $outFile) || ( [file isfile $outFile] ) } {
         puts "$inFile already exists...skipping image."
      } else {
         puts "$inFile --> $outFile"
      }

      if { $jpgQ < 1 }  {
         catch { exec imgcvt $inFile $outFile }
      } else {
         catch { exec imgcvt -q $jpgQ $inFile $outFile }
         # if output file already exists, the 'imgcvt' program will skip
         # the input file...
      } 

   } else {

   # if the input filespec does not exist
      puts "$inFile is a directory or does not exist."
   }

   incr x 1

}

