<?
	proc iconfile {type name {checkUnknown true}} {
		set file [file join [file dirname [info script]] $type "${name}.svg"]
		if {![file exists $file]} {
			if {$checkUnknown} {
				tailcall iconfile $type unknown false
			} else {
				error "No icon available for type=$type name=$name"
			}
		}

		return $file
	}

	proc icon {type name {x 5px} {y 2px} {width 16px} {height 16px}} {
		set file [iconfile $type $name]

		set fd [open $file]
		set data [read $fd]
		close $fd

		puts -nonewline [string map [list @@XPOS@@ $x @@YPOS@@ $y @@WIDTH@@ $width @@HEIGHT@@ $height] $data]
	}

	proc textColorFromBackground {color} {
		return "#fff"
	}

	set args(action) [list "build"]
	set args(status) [list "unknown"]
	set args(os)     [list "unknown"]
	set action_color_default "#555"
	set status_color_default "#4c1"
	set mapping([list status_color pass])    #00ff97
	set mapping([list status_color passed])  #00ff97
	set mapping([list status_color passing]) #00ff97
	set mapping([list status_color fail])    #f00
	set mapping([list status_color failed])  #f00
	set mapping([list status_color failing]) #f00
	set mapping([list status_color unknown]) #9932CC
	set errList [list]

	load_response args

	foreach arg {action status status_color action_color os} {
		if {![info exists args($arg)]} {
			continue
		}

		set args($arg) [lindex [set args($arg)] end]
	}

	foreach arg {status_color action_color} {
		if {![info exists args($arg)]} {
			catch {
				set args($arg) $mapping([list $arg $args([lindex [split $arg _] 0])])
			} err
			lappend errList $err
			
		}

		if {![info exists args($arg)]} {
			set args($arg) [set ${arg}_default]
		}
	}

	set timestamp "Tue, 24 Oct 2017 00:00:00 GMT"
	set etag [binary encode base64 [list $timestamp $args(action) $args(status) $args(action_color) $args(status_color) $args(os)]]

	set actionTextColor [textColorFromBackground $args(action_color)]
	set statusTextColor [textColorFromBackground $args(status_color)]

	headers type image/svg+xml
	headers add Last-Modified $timestamp
	headers add ETag $etag

	set q [format %c 63]
	puts -nonewline "<${q}xml version=\"1.0\"${q}>"
?>
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="20">
<linearGradient id="a" x2="0" y2="100%">
    <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
    <stop offset="1" stop-opacity=".1"/>
</linearGradient>
<rect rx="3" width="120" height="20" fill="<? puts -nonewline $args(action_color) ?>"/>
<rect rx="3" x="65" width="55" height="20" fill="<? puts -nonewline $args(status_color) ?>"/>
<rect rx="3" width="120" height="20" fill="url(#a)"/>
<? catch {icon os $args(os)} ?>
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
    <text x="44" y="15" fill="#010101" fill-opacity=".3"><? puts -nonewline $args(action) ?></text>
    <text x="44" y="14" fill="<? puts -nonewline $actionTextColor ?>"><? puts -nonewline $args(action) ?></text>
    <text x="92" y="15" fill="#010101" fill-opacity=".3"><? puts -nonewline $args(status) ?></text>
    <text x="92" y="14" fill="<? puts -nonewline $statusTextColor ?>"><? puts -nonewline $args(status) ?></text>
</g>
<!-- <? #puts $errList ?> -->
</svg>
