#!/usr/bin/env bash
function revision(){
    if [[ ! -e $1 ]]; then
	exit 1
    fi

    echo $(basename $1) | sed 's/.*-\([^-]*\)-.*/\1/'
}

head=$1
current=$2

cat <<EOF
<html>
<head>
 <link rel="stylesheet" href="css/screen.css" type="text/css" media="screen, projection" />
 <link rel="stylesheet" href="css/print.css" type="text/css" media="print" />
 <!--[if IE]><link rel="stylesheet" href="css/ie.css" type="text/css" media="screen, projection" /><![endif]-->
 <link rel="stylesheet" href="css/style.css" type="text/css" media="screen, projection" />
 <title>Snapshot - HappyABC</title>
</head>
<body>
<div class="container">
<h1 class="title">Snapshot - HappyABC</h1>
<div class="prepend-2 span-14 append-2">
<h2>Repository</h2>
<p>HEAD: <a href="http://github.com/mzp/scheme-abc/commit/${head}">${head}</a></p>

<h2>Snapshot</h2>
<table>
  <tr>
EOF

for f in $current/*; do
    r=$(revision $(readlink $f))

    if [[ $? != 0 ]]; then
	cat <<EOF
<tr>
 <td class="error"><a href="current/$(basename $f)">$(basename $f)</a></td>
 <td class="error">-</td>
</tr>
EOF

    elif [[ $r != $head ]]; then
	cat <<EOF
<tr>
 <td class="notice"><a href="current/$(basename $f)">$(basename $f)</a></td>
 <td class="notice">$r</td>
</tr>
EOF
    else
	cat <<EOF
<tr>
 <td class="success"><a href="current/$(basename $f)">$(basename $f)</a></td>
 <td class="success">$r</td>
</tr>
EOF
    fi
done

cat <<EOF
  </tr>
</table>
<p><a href="./snapshot/">other snapshot</a></p>

<div class="prepend-10 span-4 last">
<table>
<tr><td class="success">head</td></tr>
<tr><td class="notice">older than head</td></tr>
<tr><td class="error">some error</td></tr>
</table>
</div>

<h2>Release</h2>
<p>not yet</p>

<h2>Related</h2>
<ul>
 <li><a href="http://happyabc.org/">Toppage</a></li>
 <li><a href="http://twitter.com/scheme_abc">scheme_abc - Twitter</a></li>
 <li><a href="http://github.com/mzp/scheme-abc/tree/master">scheme_abc - GitHub</a></li>
</ul>
</div>
</div>
</body>
</html>
EOF
