# Module:         poFti
# Copyright:      Paul Obermeier 2026 / obermeier@poSoft.de
# First Version:  2026 / 03 / 11
#
# Distributed under BSD license.
#
# Batch utility program for the poFti package.
#
############################################################################

set scriptDir [file normalize [file dirname [info script]]]
set auto_path [linsert $auto_path 0 $scriptDir]

package require poFti

proc PrintUsage { progName } {
    puts ""
    puts "Usage: $progName \[Options\] File1 \[FileN\]"
    puts ""
    puts "Options:"
    puts "--help        : Display this usage message and exit." 
    puts "--verbose     : Verbose mode." 
    puts "--write <file>: Write FTI file." 
    puts "--image <file>: Write canvas to image file." 
    puts "--convert     : Batch convert icons to image files." 
    puts "--format <fmt>: Write images in specified format." 
    puts "                Default: $::optImgFmt"
    puts "--show        : Show FTI file in canvas." 
    puts "--grid <type> : Draw canvas grid of type \"square\" or \"isometric\"." 
    puts "                Default: No grid."
    puts "--size <x> <y>: Specify FTI icon display size." 
    puts "                Default: $::optWidth x $::optHeight pixel."
    exit
}

set optWidth    500
set optHeight   500
set optOutFile  ""
set optImgFile  ""
set optImgFmt   "png"
set optGridType ""
set optConvert  false
set optShow     false
set optVerbose  false
set argList     [list]

set curArg 0
while { $curArg < $argc } {
    set curParam [lindex $argv $curArg]
    if { [string compare -length 1 $curParam "-"]  == 0 || \
         [string compare -length 2 $curParam "--"] == 0 } {
        set curOpt [string tolower [string trimleft $curParam "-"]]
        if { $curOpt eq "help" } {
            PrintUsage $argv0
        } elseif { $curOpt eq "size" } {
            incr curArg
            set optWidth [lindex $argv $curArg]
            incr curArg
            set optHeight [lindex $argv $curArg]
        } elseif { $curOpt eq "write" } {
            incr curArg
            set optOutFile [lindex $argv $curArg]
        } elseif { $curOpt eq "image" } {
            incr curArg
            set optImgFile [lindex $argv $curArg]
            set optShow true
        } elseif { $curOpt eq "convert" } {
            set optConvert true
        } elseif { $curOpt eq "format" } {
            incr curArg
            set optImgFmt [lindex $argv $curArg]
        } elseif { $curOpt eq "show" } {
            set optShow true
        } elseif { $curOpt eq "grid" } {
            incr curArg
            set optGridType [lindex $argv $curArg]
        } elseif { $curOpt eq "verbose" } {
            set optVerbose true
        }
    } else {
        # A DOS shell does no file expansion, as is done with a Unix style shell.
        # So we do this here. Only "?" and "*" are recognized.
        if { $::tcl_platform(platform) eq "windows" } {
            if { [string match "*\\**" $curParam] || [string match "*\\?*" $curParam] } {
                foreach f [lsort -dictionary [glob -nocomplain -- [file normalize $curParam]]] {
                    lappend argList $f
                }
            } else {
                lappend argList $curParam
            }
        } else {
            lappend argList $curParam
        }
    }
    incr curArg
}

if { $optShow || $optConvert } {
    package require Tk
    bind . <Escape> exit

    canvas .c -width $optWidth -height $optHeight -background "#C9C9C9" \
              -borderwidth 0 -highlightthickness 0
    pack .c
    if { $optGridType ne "" } {
        if { $optVerbose } {
            puts "Drawing grid of type $optGridType"
        }
        poFti DrawGrid .c -width $optWidth -height $optHeight -type $optGridType
    }
}

foreach fileName $argList {
    set iconObj [poFti Icon new]
    if { $optVerbose } {
        puts "Reading FTI file $fileName"
    }
    set retVal [catch {$iconObj Read $fileName} errVal]
    if { $retVal != 0 } {
        puts "Error reading $fileName: $errVal"
        continue
    }
    if { $optOutFile ne "" } {
        if { $optVerbose } {
            puts "Writing FTI file $optOutFile"
        }
        $iconObj Write $optOutFile
    }
    if { $optShow || $optConvert } {
        wm title . [file tail $fileName]
        $iconObj Draw .c -width $optWidth -height $optHeight
        if { $optVerbose } {
            puts "Icon has [$iconObj GetNumItems] items"
        }
        if { $optConvert } {
            set outFileName [format "%s.%s" $fileName $optImgFmt]
            if { $optVerbose } {
                puts "Writing image file $outFileName"
            }
            $iconObj WriteToImageFile .c $outFileName $optImgFmt
            .c delete all
        } elseif { $optImgFile ne "" } {
            if { $optVerbose } {
                puts "Writing image file $optImgFile"
            }
            $iconObj WriteToImageFile .c $optImgFile $optImgFmt
        }
    }
    $iconObj destroy
}

if { ! $optShow } {
    exit 0
}

