#!/usr/local/bin/tclsh
#
#	viewfile filename filetype program postargs
#
global env

#########################################################################
#####	Set the required environment variables
#########################################################################

if {[info exists env(HOME)] && $env(HOME)=="/"} {set env(HOME) "/root"}

if {$tcl_platform(platform)=="windows"} {
	if {![info exists env(TKAPPS)]} {
		set env(TKAPPS) {//?/C\:/TKAPPS}
	}
	if {![file isdirectory $env(TKAPPS)]} {
		puts stdout "Directory $env(TKAPPS) not found"
		exit 1
	}
} elseif {$tcl_platform(platform)=="unix"} {
	if {![info exists env(TKAPPS)]} {
		if {[file isdirectory $env(HOME)/TKAPPS]} {
			set env(TKAPPS) $env(HOME)/TKAPPS
		} elseif {[file isdirectory /usr/local/TKAPPS]} {
			set env(TKAPPS) /usr/local/TKAPPS
		} else {
			puts stdout "Could not find where TKAPPS is installed"
			puts stdout "Please define the TKAPPS environment variable"
			exit 1
		}
	}
} else {
	puts stdout "This application is not supported under $tcl_platform(platform)"
}

set auto_path [linsert $auto_path 0 $env(TKAPPS)/lib]

set name [lindex $argv 0]
set type [string tolower [lindex $argv 1]]
set program [lindex $argv 2]
set postargs [lindex $argv 3]

#############################################################################
#	Main program
#############################################################################

if {$type!="application/url"} {
	if {![file readable $name]} {
		puts stdout "ERROR - File $name could not be found"
		exit 0
	}
}

#
#	Check to see if a program was supplied
#
if {$program!=""} {
	set cmd "($program $name $postargs) </dev/null >/dev/null 2>/dev/null &"
	catch "exec sh -c \"$cmd\"" res
	exit 0
}

#
#	Determine the filetype
#
if {$type=="" \
	|| $type=="unknown" \
	|| $type=="application/octet-string" \
	|| $type=="application/octet-stream"} {
	set cmd "exec vfile $name"
	if {[catch "eval $cmd" res]} {
		puts stdout "ERROR - Could not determine the file type for $name"
		puts stdout "Command=$cmd"
		puts stdout "Result=$res"
		exit 0
	}
	set type [lindex $res 1]
}

set type [string tolower [string trim $type]]
set type_main [lindex [split $type "/"] 0]

#
#	Generate the command to execute
#
set files "$env(HOME)/.mailcap /etc/mailcap $env(TKAPPS)/etc/mailcap"
set command ""
foreach filename $files {
	if {![file exists $filename]} {continue}

	set fp [open $filename r]
	while {[gets $fp line]>=0} {
		set fields [split $line ";"]
		set mimetype [string trim [string tolower [lindex $fields 0]]]
		if {$type==$mimetype} {
			set command [string trim [join [lrange $fields 1 end] ";"]]
			break
		}
		set mimetype [split $mimetype "/"]
		set mimetype_main [lindex $mimetype 0]
		set mimetype_sub [lindex $mimetype 1]
		if {$type_main==$mimetype_main && $mimetype_sub=="*"} {
			set command [string trim [join [lrange $fields 1 end] ";"]]
			break
		}
	}
	close $fp
	if {$command!=""} {break}
}

regsub {%s} $command "$name" command

if {$command!=""} {
	set cmd "($command $postargs) </dev/null >/dev/null 2>/dev/null &"
	catch "exec sh -c \"$cmd\"" res
} else {
	puts stdout "ERROR - Cannot determine filetype for $name"
	puts stdout "It was not possible to determine the filetype of"
	puts stdout ">>>$name<<<"
	puts stdout ""
	puts stdout "Please add an entry to your .mailcap file that recognizes"
	puts stdout "attachments of type >>>$type<<< and launches an"
	puts stdout "appropriate application"
}

exit 0

