#!/bin/bash
# upkg-reinstall
#
# tool to remove and reinstall / repair a package
#
# Copyright (C) 2004 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>

for i in $*
do
	case $i in
		-*)
			;;
		*)
			UNIQUENAME=$i
			break
			;;
	esac
done

if [ -z \"$UNIQUENAME\" ]
then
	echo "Usage: $0 PACKAGE"
	exit 1
fi

[ -L /var/lib/upkg/packages/$UNIQUENAME ] || { echo "ERROR: Package $UNIQUENAME not found." ; exit 1 ; }

VERSIONEDNAME=$(readlink /var/lib/upkg/packages/$UNIQUENAME)
[ -e /var/lib/upkg/packages/$VERSIONEDNAME ] || exit 1

mv -f /var/lib/upkg/packages/$VERSIONEDNAME{,-old}
ln -sf $VERSIONEDNAME-old /var/lib/upkg/packages/$UNIQUENAME

for file in /var/lib/upkg/{packages,files,logs}/$VERSIONEDNAME.*
do
	mv -f $file $(echo $file | sed -e "s/$VERSIONEDNAME/&-old/")
done

upkg-upgrade $(echo "$@" | sed -e "s/$UNIQUENAME//")
