#!/bin/sh
#-
# $FreeWRT$
#-
# This file is part of the FreeWRT project. FreeWRT is copyrighted
# material, please see the LICENCE file in the top-level directory
# or at http://www.freewrt.org/licence for details.

# Copyright (c) Waldemar Brodkorb <wbx@freewrt.org>

# change here the base variables
topdir=/home/wbx/freewrt_1_0
#database=${topdir}/foo.db
database=/srv/database/wib_dev3.db
snapdir=/srv/apache2/downloads/brcm-2.4/packages

# check for sqlite 
sqlite=$(which sqlite3)
[ -x ${sqlite} ] || "sqlite application is missing"

allpackages=$(find ${topdir}/package -name Makefile -maxdepth 2 -mindepth 2 |grep -v package\/config)

for i in ${allpackages};do
	pkg_name=$(basename $(dirname $i))
	pkg_version=$(awk "/^PKG_VERSION/ { print \$2 }" $i)
	pkg_release=$(awk "/^PKG_RELEASE/ { print \$2 }" $i)
	#pkg_description=$(awk "/^Description:/ { print $2 }" ${topdir}/package/${pkg_name}/ipkg/${pkg_name}.control)
	pkg_list=$(ls ${topdir}/package/${pkg_name}/ipkg/*.control)
	for k in ${pkg_list};do
		p=$(basename $k .control)
		pkg_description=$(awk "/^Description:/ { print $2 }" ${topdir}/package/${pkg_name}/ipkg/${p}.control|sed "s#Description: ##")
		pkg_size=$(ls -la ${snapdir}/${p}_${pkg_version}*.ipk|awk '{ print $5 }')
		if [ -z ${pkg_size} ];then pkg_size=0;fi
		echo "inserting $p with ${pkg_size}"
		$sqlite $database "insert into packages (name,description,size,version,release) values (\"$p\",\"$pkg_description\",$pkg_size,\"$pkg_version\",$pkg_release)"
	done
done
