#!/bin/bash
# upkg-repair-index
#
# cleans content of /var/lib/upkg
#
# Copyright (C) 2005 Raffaele Sandrini, Jürg Billeter
#
# This file is part of Upkg (http://www.upkg.org).
#
# Upkg is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# Upkg 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.
#
# You should have received a copy of the GNU General Public License
# along with Upkg; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Authors:
#   Raffaele Sandrini <rasa at paldo dot org>
#   Jürg Billeter <juerg at paldo dot org>

rm -rf /tmp/upkg-repair-index
mkdir -p /tmp/upkg-repair-index

echo "Checking package index..."

for file in /var/lib/upkg/packages/*
do
	if [ -L $file ] && [ -e $file ]
	then
		touch /tmp/upkg-repair-index/$(basename $file)
		package=$(readlink $file)
		for f in /var/lib/upkg/{packages,files,logs,scripts}/$package*
		do
			touch /tmp/upkg-repair-index/$(basename $f)
		done
	else
		case "$file" in
		*.select)
			touch /tmp/upkg-repair-index/$(basename $file)
			;;
		esac
	fi
done

for file in /var/lib/upkg/{packages,files,logs,scripts}/*
do
	if [ ! -e /tmp/upkg-repair-index/$(basename $file) ]
	then
		echo Removing $file
		rm $file
	fi
done

rm -rf /tmp/upkg-repair-index
