#!/bin/sh
# ============================================================================
#
# This program is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Author: Mijzelf <Mijzelf@live.com>
#
# ============================================================================

Help()
{
	cat >&1 <<__EOF__
This script is intended to create a Packages.html file from a Packages or Packages.gz file.

Usage: $0 Packages[.gz]
__EOF__
}

ReadPackages()
{
	cat >&3 <<__EOF__
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<!-- Designed and coded by Entware team -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<title>Packages list</title>
<link rel="stylesheet" type="text/css" href="../resources/packages.css">
</head>
<script type="text/javascript" src="../resources/list.js"></script>
<body>
<div id="packages">
You may sort table by clicking any column headers and\or use <input class="search" placeholder="Search" /> field.

<table>
<thead>
<tr>
<th class="sort" data-sort="name">Name</th>
<th class="sort" data-sort="version">Version</th>
<th class="sort" data-sort="section">Section</th>
<th class="sort">Description</th>
</tr>
</thead>
<tbody class="list">
__EOF__

        local name=""
	local version=""
        local section=""
	local description=""
	local package=""

	while read line 
	do
		if [ "$line" = "" ] ; then
			if [ "$package" != "" ] ; then
				[ "$name" = "" ] && name="-"
				[ "$version" = "" ] && version="-"
				[ "$section" = "" ] && section="-"
				[ "$description" = "" ] && description="-"
				
				cat >&3 <<__EOF__
<tr><td class="name"><a href="${package}">${name}</a></td>
<td class="version">${version}</td>
<td class="section">${utils}</td>
<td class="description">${description}</td></tr>
__EOF__
			fi
			
			name=""
			version=""
    			section=""
			description=""
			package=""
			continue
		fi
    
		local value="$( echo $line | cut -d ' ' -f 2- )"
    
		case $line in
			Package:*)
				name="${value}"
				;;
			Version:*)
				version="${value}"
				;;
			Section:*)
				section="${value}"
				;;
			Description:*)
				description="${value}"
				;;
			Filename:*)
				package="${value}"
				;;
		esac
	done 
    
    cat >&3 <<__EOF__
</tbody>
</table>
</div>
<script type="text/javascript">
var options = {
        valueNames: [ 'name', 'version', 'section', 'description' ]
};
		
var userList = new List('packages', options);
</script>
</body>
</html>
__EOF__

}

Main()
{
	[ "$1" = "" ] && Help
	
	local dir=$( dirname $1 )
	local file=$( basename $1 )
	
	case $file in
		Packages)
			cat $1 | ReadPackages 3>${dir}/Packages.html
			;;
		Packages.gz)
			gzip -dc $1 | ReadPackages 3>${dir}/Packages.html
			;;
		*)
			Help
			;;
	esac
}

Main "$@"
