#!/bin/bash
# usage: /n for last n changes
#        [/n]/all for showing even hidden webs
set -a

# autodetects path if script is in WIKIDIR/bin/scripts/ or a subdir of it
wiki=${SCRIPT_FILENAME%/bin/*}
wiki_bin=${wiki}/bin
wiki_lib=${wiki}/lib

parse_webs () {
    Webs=
    for i in *;do 
      if test -d "$i"; then
        if grep "^		[*] Set  *NOSEARCHALL  *=  *on" "$i/WebPreferences.txt">/dev/null
          then case "$PATH_INFO" in */all) :;;
	    *) continue;; esac
        fi
        Webs="$Webs,$i"
	color=`grep "^		[*] Set  *WEBBGCOLOR  *= " "$i/WebPreferences.txt"`
	color="${color##*= }"
	echo "span.$i {background: $color;}"
      fi
    done
    Webs="$Webs,"
}

parse_webs_koalaskin () {
    Webs=
    for i in *;do 
      if test -d "$i"; then
        if grep "^		[*] Set  *NOSEARCHALL  *=  *on" "$i/WebPreferences.txt">/dev/null
          then case "$PATH_INFO" in */all) :;;
	    *) continue;; esac
        fi
        Webs="$Webs,$i"
      fi
    done
    Webs="$Webs,"
    cat ../templates/Main/style.koala.tmpl 
}

parse_log () {
  IFS='|'
  echo "<table border=0><tr><th>Web<th>Topic<th>date<th>who"
  while read nope when who how what rest;do
    #echo "<br>nope=$nope, when=$when, who=$who, how=$how, what=$what, rest=$rest"
    what="${what## }";what="${what%% }"
    web=${what%.*}
    day="${when%-*}"
    case "$Webs" in *,$web,*) 
      topic=`echo ${what#*.} | tr ,+=- _` # take care of - in topics
      case "$topic" in
	[A-Z]*)
	  if let "$web$topic<1"; 
	  then let "$web$topic=2"
	    if test "$prevday" != "$day"; then
	      let ndays=ndays+1
	      echo "<tr><td bgcolor=#dddddd>&nbsp;<td bgcolor=#dddddd>&nbsp;<td bgcolor=#dddddd><b>$day</b><td bgcolor=#dddddd>&nbsp;"
	      fi
              echo "<tr><td>"
#	      echo "<span class='$web'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"
# koalaskin
	      echo "<span class='bg1-${web}'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"
	      echo "<td><a href=../view$scriptSuffix/$web/$topic>$what</a><td> <i>${when#*-}</i> <td><a href=../view$scriptSuffix/Main/${who#*.}>${who#*.}</a>"
	      prevday="$day"
	      let ntopics=ntopics+1
	      if let 'ntopics>=max'; then terminate; exit 0;fi
	  fi;;
      esac;;
    esac
  done
}

terminate () {
  echo "</table></body></html>"
  active=false
  exit 0
}

echo "Content-type: text/html"
echo "Expires: Monday, 1-Jan-99 01:01:01 GMT"
echo

#wikiprefs
cd $wiki/data

rev=`which tac`;if test ! -x "$rev";then rev="tail -r"; fi

max=100
case "$PATH_INFO" in /[0-9]*) max="${PATH_INFO#/}"; max="${max%%/*}"; esac

wikiname=`grep '^[$]wikiToolName' $wiki_lib/TWiki.cfg | sed -e 's/[^"]*["]//' -e 's/["].*//'`
wikiname=${wikiname:-Wiki}

wikiurl=`grep '^[$]wikiHomeUrl' $wiki_lib/TWiki.cfg | sed -e 's/[^"]*["]//' -e 's/["].*//'`
wikiurl=${wikiurl:-../view/Main/WebHome}

title="Last $max changed topics in"
echo "<html><head><title>$title $wikiname</title>"
echo "<style type='text/css'>"
if grep '^		[*]  *Set  *SKIN  *=  *koala' TWiki/TWikiPreferences.txt >/dev/null 2>&1; then
    parse_webs_koalaskin 2>&1
else
    parse_webs 2>&1
fi
echo "</style></head><body>"
echo "<h2>$title <a href=$wikiurl>$wikiname</a></h2>"
OIFS="$IFS";prevday='';ntopics=0;ndays=0
logs=`ls -1t log*.txt`

for log in $logs;do fgrep ' | save | ' $log|$rev; done | parse_log 2>/dev/null

terminate
