#!/bin/bash
# list all the software of WineTools
# usage: listit <wt$RELEASEjo >wt$RELEASEjo.html

count=0
VERSION="`ls wt*jo`"
RELEASE="`echo $VERSION | sed -e "s/wt\(.*\)jo/\1/"`"

removeblanks()
{
  LINE="`echo \"$1\"|sed -e \"s/^\ *\(.*\)\ *$/\1/\"`"
}

getentry()
{
  ENTRY="`echo \"$1\"|sed -e \"s/^\\\"\(.\+\)\\\")$/\1/\"`"
}

getname()
{
  NAME="`echo \"$1\"|grep \"=\"|sed -e \"s/^\(.\+\)=\\\".\+\\\"$/\1/\"`"
}

getvalue()
{
  VALUE="`echo \"$1\"|grep \"=\"|sed -e \"s/^.\+=\\\"\(.\+\)\\\"$/\1/\"`"
}

# start html output with headers
# No. Entry|URL sysname|dload dlsize LICENSE ALTERNATIVE comment
echo "<html><body><table border=1 frame="box"><colgroup><col><col><col><col><col><col><col></colgroup><tr><th>No.</th><th>Program</th><th>Binary</th><th>Size</th><th>License</th><th>Alternative</th><th>Comment</th></tr>"
while true; do
  read -t 1 LINE
  [ "$?" = "0" ] || break
  removeblanks "$LINE"
  # find out whether this is the start of a program definition
  echo "$LINE"|grep "^\".\+\")$" &>/dev/null
  if [ "$?" = "0" ]; then
    getentry "$LINE"
    URL=""
    CONTACT=""
    dload=""
    dlsize=""
    sysname=""
    installer=""
    testname=""
    script=""
    comment=""
    LICENSE=" "
    ALTERNATIVE=" "
    # read all definitions until the stop word ";;"
    while true; do
      read -t 1 LINE
      [ "$?" = "0" ] || break
      removeblanks "$LINE"
      [ "$LINE" = ";;" ] && break
      getname "$LINE"
      getvalue "$LINE"
      case "$NAME" in
        "URL")		URL="$VALUE" ;;
	"CONTACT" )	CONTACT="$VALUE" ;;
	"dload")	dload="$VALUE" ;;
	"dlsize")	dlsize="$VALUE" ;;
	"sysname")	sysname="$VALUE" ;;
	"installer")	installer="$VALUE" ;;
	"testname")	testname="$VALUE" ;;
	"script")	script="$VALUE" ;;
	"comment")	comment="$VALUE" ;;
      esac
    done
    # get the comment in it
    if [ "$comment" = "" ]; then
#      grep -e "\ *\"${ENTRY}\"\ \+\"${ENTRY}\"\ \+\".*\"\ \+\".*\"\ \+\"\(.*\)\"\ \+\\\\" $VERSION | sed -e "s/\ *\"${ENTRY}\"\ \+\"${ENTRY}\"\ \+\".*\"\ \+\".*\"\ \+\"\(.*\)\"\ \+\\\\/\1/" >comment.out
      grep -e "\ *\"${ENTRY}\"\ \+\"${ENTRY}\"\ \+\".*\"\ \+\".*\"\ \+\"\(.*\)\"\ \+\\\\" $VERSION| sed -e "s/\ *\"${ENTRY}\"\ \+\"${ENTRY}\"\ \+\".*\"\ \+\".*\"\ \+\"\`eval_gettext \"\(.*\)\"\`\"\ \+\\\\/\1/" >comment.out
      # extract data out of comment
      grep -e "License: " comment.out | sed -e "s/.*License: \([^|]\+\).*/\1/" >license.out
      grep -e "Alternative: " comment.out | sed -e "s/.*Alternative: \([^|]\+\).*/\1/" >alternative.out
      comment="`cat comment.out | sed -e \"s/\([^|]\+\).*$/\1/\"`"
      LICENSE="`cat license.out`"
      ALTERNATIVE="`cat alternative.out`"
    fi
    [ "$comment" = "" ] && comment="no comment"
    [ "$LICENSE" = "" ] && LICENSE="unknown"
    [ "$ALTERNATIVE" = "" ] && ALTERNATIVE="unknown"
    # html output of the line
    # No. Entry|URL sysname|dload dlsize LICENSE ALTERNATIVE comment
    if [ "$dload" != "" ] || [ "$dlsize" != "" ] || [ "$sysname" != "" ] || [ "$testname" != "" ] || [ "$script" != "" ]; then
      count=$(($count+1))
      echo "<tr><td>$count</td><td><a href=\"$URL\">$ENTRY</a></td><td><a href=\"$dload\">$sysname</a></td><td>$dlsize</td><td>$LICENSE</td><td>$ALTERNATIVE</td><td>$comment</td></tr>"
    fi
  fi
done
echo "</table></body></html>"
rm comment.out license.out alternative.out
