#!/bin/bash
# Author: Guang Cheng Li (liguangc@cn.ibm.com)
# Relpace the links in xcat-dep
# Note: run this script under the xcat-dep directory
# usage:
# replacelinks oldrpmname newrpmname
# for example:
# replacelinks xCAT-genesis-base-x86_64-2.8-snap201308090229.noarch.rpm xCAT-genesis-base-x86_64-2.8-snap201403252335.noarch.rpm
OLDRPM=$1
NEWRPM=$2

if [ -z "$OLDRPM" ] || [ -z "$NEWRPM" ];
then
    echo "Wrong arguments: the syntax should be replacelinks oldrpmname newrpmname"
    exit 1
fi

for RPM in `find ./ -name $OLDRPM`;
do
    DIRNAME=`dirname $RPM`
    LINK=`readlink $RPM`
    if [ ! -z "$LINK" ];
    then
        LINKDIR=`dirname $LINK`
    fi
    # remove the old file or old link
    echo "rm -f $RPM"
    rm -f $RPM
    if [ ! -z "$LINK" ];
    then
        echo "ln -s $LINKDIR/$NEWRPM $DIRNAME/$NEWRPM"
        ln -s $LINKDIR/$NEWRPM $DIRNAME/$NEWRPM
    fi
done
