#!/bin/sh

. /etc/tazwok.conf
PURGE=
while true; do
	case "$1" in
	--purge) PURGE="yes";;
	*) break;;
	esac
	shift
done
WOK=/home/slitaz/${1:-cooking}/wok
SRC=/var/www/slitaz/mirror/sources/packages-${1:-cooking}
SRC2=/var/www/slitaz/mirror/sources/packages

for i in $WOK/*/receipt ; do
	unset TARBALL EXTRA_SOURCE_FILES WGET_URL WANTED PACKAGE VERSION
	ARCH=i486
	. $i
	[ -n "$WANTED" ] && continue
	if [ -z "$TARBALL$EXTRA_SOURCE_FILES" ]; then
		[ -n "$WGET_URL" ] || continue
		echo $(basename ${i%/receipt}) >> /tmp/notarball$$
		TARBALL="$PACKAGE-$VERSION.tar.bz2"
	fi
	[ -n "$WGET_URL" ] && echo "$TARBALL $WGET_URL" >> /tmp/wget_url$$
	for j in $TARBALL $EXTRA_SOURCE_FILES ; do
		basename $j
	done
done | sort | uniq > /tmp/tarball$$
for i in $WOK/*/receipt ; do
	HOST_ARCH=
	WANTED=
	. $i
	[ -n "$WANTED" ] && continue
	for a in $HOST_ARCH ; do
		[ $a == i486 ] && continue
		unset TARBALL EXTRA_SOURCE_FILES WGET_URL WANTED PACKAGE VERSION
		SLITAZ_ARCH=$a
		ARCH=$a
		. $i
		if [ -z "$TARBALL$EXTRA_SOURCE_FILES" ]; then
			[ -n "$WGET_URL" ] || continue 2
			grep -q $(basename ${i%/receipt}) /tmp/notarball$$ ||
			echo $(basename ${i%/receipt}) >> /tmp/notarball$$
			TARBALL="$PACKAGE-$VERSION.tar.bz2"
		fi
		[ -n "$WGET_URL" ] && 
		! grep -q "^$TARBALL " /tmp/wget_url$$ &&
		echo "$TARBALL $WGET_URL" >> /tmp/wget_url$$
		for j in $TARBALL $EXTRA_SOURCE_FILES ; do
			grep -q "^$(basename $j)$" /tmp/tarball$$ ||
			basename $j
		done
	done
done | sort | uniq >> /tmp/tarball$$
if [ -s /tmp/notarball$$ ]; then
	echo $(cat /tmp/notarball$$ 2> /dev/null) | \
		sed 's/ /, /g;s/^/No TARBALL for /'
	rm -f /tmp/notarball$$
fi
while read file; do
	[ -s $SRC/${file:0:1}/$file ] || [ -s $SRC/${file:0:1}/${file%.*}.lzma ] ||
	[ -s $SRC/${file:0:1}/${file%.*}.tar.lzma ] ||
	echo -e "Missing: $file\nURL: $(sed "/^$file /!d;s/^$file //;q" /tmp/wget_url$$)"
done < /tmp/tarball$$
for i in $SRC/?/* ; do
	[ -f "$i" ] || continue
	j=$(basename $i)
	[ -f $SRC2/${j:0:1}/$j ] || ln $i $SRC2/${j:0:1}/$j
	case "$i" in
	*lzma)
		grep -q ^$(basename ${i%lzma}) /tmp/tarball$$ && continue
		[ $(ls ${i%lzma}* | wc -l) -ne 1 ] && ls ${i%lzma}* | cat
		;;
	*)
		grep -q ^$(basename $i)$ /tmp/tarball$$ && continue
		;;
	esac
	echo $i && [ "$PURGE" == "yes" ] && \rm -i $i
done
rm -f /tmp/tarball$$ /tmp/wget_url$$
for i in $SRC2/?/* ; do
	[ $(stat -c "%h" $i) -eq 1 ] && echo $i &&
	[ "$PURGE" == "yes" ] && echo "Purge $i" && rm -f $i
done
