#!/bin/sh
# Rename a patch.
#
# By La Monte H.P. Yarroll <piggy@timesys.com>, <piggy@acm.org>
#
# Copyright (c); 2004 TimeSys Corporation (R), 925 Liberty Avenue, 6th Floor,
# Pittsburgh, PA 15222. This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of the License,
# or (at your option) any later version. This program is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details. A copy of the GNU General
# Public License in included with this program. You may also request a copy by
# writing to the Free Software Foundation, Inc., 59 Temple Place ? Suite 330,
# Boston, MA 02111-1307, USA. TimeSys Corporation will provide you with a
# complete machine-readable copy of the source code for TimeSys R Linux and/or
# any other GPL or LGPL programs contained in this distribution at your
# request for a charge not to exceed TimeSys? cost of physically providing
# such source to you. This offer is valid for three (3) years from the date of
# TimeSys initial distribution TimeSys Linux. You may contact TimeSys
# Corporation by writing to us at 925 Liberty Avenue, 6th Floor, Pittsburgh PA
# 15222. This notice may not be removed, but must be reproduced as is in any
# authorized copies of this distribution.

# If you a feeling forceful, you may use -f, but it doesn't do anything...

. patchfns 2>/dev/null ||
. /usr/lib/patch-scripts/patchfns 2>/dev/null ||
. $PATCHSCRIPTS_LIBDIR/patchfns 2>/dev/null ||
{
	echo "Impossible to find my library 'patchfns'."
	echo "Check your install, or go to the right directory"
	exit 1
}

usage()
{
	echo "Usage: mv-patch oldname newname"
	exit 1
}

do_rename()
{
    mv $P/pc/$1.pc $P/pc/$2.pc
    mv $P/txt/$1.txt $P/txt/$2.txt
    mv $P/patches/$1.patch $P/patches/$2.patch
    sed -e "s/^$1.patch$/$2.patch/g" < $P/series > /tmp/$$
    cat /tmp/$$ > $P/series
    sed -e "s/^$1$/$2/g" < $P/applied-patches > /tmp/$$
    mv /tmp/$$ $P/applied-patches
}

opt_force=0

for i in $*
do
	case "$i" in
	-f)
		opt_force=1
	        shift;;
	esac
done

src=$(stripit $1)
dest=$(stripit $2)

[ $opt_force = 1 ] && force="-f"

if [ ! -e $P/series ]
then
	echo 'File "$P/series" not found'
	exit 1
fi

if [ ! -e $P/series ]
then
	echo 'File "$P/applied-patches" not found'
	exit 1
fi

if [ 2 != $# ]
then
        usage
fi

if [ -e $P/pc/$dest.pc -o -e $P/txt/$dest.txt -o -e $P/patches/$dest.patch ]
then
    echo "$dest already exists";
    exit 1
fi

do_rename $src $dest

