# Module:         poIrix
# Copyright:      Paul Obermeier 2000-2026 / paul@poSoft.de
# First Version:  2026 / 04 / 16
#
# Distributed under BSD license.
#
# Module with functionality specific for SGI Irix.

namespace eval poIrix {
    variable ns [namespace current]

    namespace ensemble create

    namespace export GetAssoProg


    # Get the program associated with specified file extension.
    # "extension" must be specified with a dot, ex. ".bmp",
    # as returned by Tcl command "file extension".
    proc GetAssoProg { extension { dir "/usr/lib" } } {
        variable sAssoCache

        set retVal ""
        if { [info exists sAssoCache] } {
            if { [info exists sAssoCache($extension)] } {
                set retVal $sAssoCache($extension)
            }
            return $retVal
        }
        set mimeFile [file join $dir "mime.types"]
        set capFile  [file join $dir "mailcap"]

        set catchVal [catch {open $mimeFile "r"} fp]
        if { $catchVal != 0 } {
            error "Could not open file \"$mimeFile\" for reading."
        }
        while { [gets $fp line] >= 0 } {
            if { [string index $line 0] ne "#" } {
                scan $line "type=%s" type
                set extInd [string first "exts=" $line]
                if { $extInd > 0 } {
                    scan [string range $line $extInd end] "exts=%s" extensions
                    foreach ext [split $extensions ","] {
                        lappend mimeExtensions($type) ".$ext"
                    }
                }
            }
        }
        close $fp

        set mimeTypes [lsort -dictionary [array names mimeExtensions]]

        set catchVal [catch {open $capFile "r"} fp]
        if { $catchVal != 0 } {
            error "Could not open file \"$capFile\" for reading."
        }
        set fullLine ""
        while { [gets $fp line] >= 0 } {
            if { [string index $line 0] eq "#" || $line eq "" } {
                continue
            } elseif { [string index $line end] eq "\\" } {
                append fullLine [string range $line 0 end-1]
            } else {
                append fullLine [string range $line 0 end]
                set components [split $fullLine ";"]
                set mimeType [string trim [lindex $components 0]]
                if { [lsearch -exact $mimeTypes $mimeType] >= 0 } {
                    set mimeCmd [string trim [lindex $components 1]]
                    if { ! ( [string match "description=*" $mimeCmd] || \
                             [string match "compose=*" $mimeCmd] || \
                             [string match "x11-bitmap=*" $mimeCmd] ) } {
                        set mimeCmds($mimeType) $mimeCmd
                    }
                }
                set fullLine ""
            }
        }
        close $fp

        set validMimeTypes [lsort -dictionary [array names mimeCmds]]
        foreach mimeType $validMimeTypes {
            if { [info exists mimeExtensions($mimeType)] } {
                foreach ext $mimeExtensions($mimeType) {
                    set sAssoCache($ext) $mimeCmds($mimeType)
                }
            }
        }
        if { [info exists sAssoCache($extension)] } {
            set retVal $sAssoCache($extension)
        }
        return $retVal
    }
}
