#!/bin/sh
# Copyright (C)2006 .. 2013 @BABOLO http://www.babolo.ru/
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: cvsreleased,v 1.6 2013/11/29 18:26:52 babolo Exp $
# @BABOLO V.M %%VERS%% %%DATE%%
#
odir="${1}"
ofle="${2}"
    null=/dev/null
      cp=/bin/cp
      mv=/bin/mv
      rm=/bin/rm
     cat=/bin/cat
    test=/bin/test
   chmod=/bin/chmod
      tr=/usr/bin/tr
     awk=/usr/bin/awk
     sed=/usr/bin/sed
    head=/usr/bin/head
    tail=/usr/bin/tail
    make=/usr/bin/make
    diff=/usr/bin/diff
    find=/usr/bin/find
    grep=/usr/bin/grep
    perl=/usr/bin/perl
    sort=/usr/bin/sort
    stat=/usr/bin/stat
  mktemp=/usr/bin/mktemp
basename=/usr/bin/basename
     cvs=/usr/local/bin/cvs

${cat} << "EOF"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head><title>ChangeLogs</title></head>
 <body><h1>ChangeLogs</h1>
  <pre><table cellPadding=0 cellSpacing=0>
   <tr>
    <td valign=bottom><a href="index.htm">Last modified</a><hr></td>
    <td valign=bottom><hr></td>
    <td valign=bottom>&nbsp;<a href="index.html">Name</a><hr></td>
    <td valign=bottom><hr></td>
    <td valign=bottom><hr></td>
   </tr>
EOF

if ${test} "${ofle}" = "sort"; then
    s="${sort} -r"
else
    s="${cat}"
fi

( for i in `${find} "${odir}" -type f | ${grep} -vE "^${odir}/index\.html?\$" | ${sort}`; do
    j=`${basename} "${i}"`
    k=`${head} -2 "${i}" | ${tail} -1`
    m=`${head} -1 "${i}" | ${awk} '{print $2}'`
    ${stat} -nt'%F %H:%M' -f'   <tr><td>%Sm</td><td align=right>' "${i}"
    l=`${stat} -nf'%b' "${i}"`
    echo -n '&nbsp;'$((${l} / 2))'k</td><td>&nbsp;<a href="'"${j}"'">'"${j}"'</a></td>'
    echo -n '<td>'"${m}"'</td><td>&nbsp;'
    if echo "${k}" | ${grep} -q '^[]'; then
        echo "${k}" | ${sed} -Ee 's|[^ ]* +||' | ${tr} -d '\n'
    fi
    echo '</td></tr>'
  done
) | ${s}
${cat} << "EOF"
   <tr>
    <td valign=top><hr><a href="index.htm">Last modified</a></td>
    <td valign=top><hr></td>
    <td valign=top><hr>&nbsp;<a href="index.html">Name</a></td>
    <td valign=top><hr></td>
    <td valign=top><hr></td>
   </tr>
  </table></pre>
 </body>
</html>
EOF
