#!/usr/bin/q -c main

import system;

/* Build the Packages file for this feed. */

main		= do (process F) (glob "*.ipk") || exit 0
		    where F:File = fopen "Packages" "w";

process F NAME	= printf "%s (%s)\n" (NAME,size SIZE) ||
		  fputs F DESCR ||
		  unless (pos "Size:" DESCR >= 0)
		  (fprintf F "Size: %s\n" $ size SIZE) ||
		  fputs F "\n"
		    where DESCR:String = cmd
"ar p %s control.tar.gz | tar xfzO -" (str NAME),
		      // FIXME: this only gives a rough estimate,
		      // we should maybe use du -s here
		      SIZE:Int = val $ trim $ cmd
"ar p %s data.tar.gz | tar xfzO - | wc -c" (str NAME);

cmd CMD ARGS	= fget $ popen (sprintf CMD ARGS) "r";

size SIZE	= str SIZE if SIZE < 1024;
		= sprintf "%0.1fK" $ SIZE/1024
		    if SIZE < 1024*1024;
		= sprintf "%0.1fM" $ SIZE/1024/1024
		    otherwise;

trim S		= hd (split "\n" S);
