#!/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.8 2014/10/07 16:38:53 babolo Exp $
# @BABOLO V.M %%VERS%%  %%DATE%% http://www.babolo.ru/
#
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=`${cat} "${i}" | ${awk} '\
BEGIN {
    c = 0;
    d = 0;
}\
(c == 1) {
    e = "";
    if  ($3) e = e " " $3;
    if  ($4) e = e " " $4;
    if  ($5) e = e " " $5;
    if  ($6) e = e " " $6;
    if  ($7) e = e " " $7;
    if  ($8) e = e " " $8;
    if  ($9) e = e " " $9;
    if  ($10) e = e " " $10;
    if  ($11) e = e " " $11;
    if  ($12) e = e " " $12;
    if  ($13) e = e " " $13;
    if  ($14) e = e " " $14;
    if  ($15) e = e " " $15;
    if  ($16) e = e " " $16;
    if  ($17) e = e " " $17;
    if  ($18) e = e " " $18;
    if  ($19) e = e " " $19;
    if  (/^[]/) {
        if  (d) print $2 "+" e; else print $2 e;
        exit
    }
    c = 3;
}\
(c == 0) {
    if  (/^2[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/) c=1; else c=3;
}\
/^$/{
    c = 0;
    d = 1;
}\
' | ${tr} -d '\n'`
    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 '<td>'"${m}"'</td><td>&nbsp;'"${k}"'</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
