#!/bin/sh -e

[ x"$1" = x ] && {
	echo "E: You loose. Need the package build directory."
	exit 1
}

[ x"$2" = x ] && {
	echo "E: You loose. Need the subdirectory with symlinks to check up."
	exit 1
}

DEB_BLD_DIR=$1
CRIS_CHK=$2
LCHK=`echo "$DEB_BLD_DIR$CRIS_CHK" | sed -e 's,//,/,g'`

echo "I: Checking symlinks in $LCHK"
for L in `find $LCHK/ -type l | sort`
do
	L1=`readlink $L`
	#[ "$L1" ] && echo $L1
	[ "$L1" ] && echo $L1 | grep -q $CRIS_CHK && {
		L2=`echo $L1 | sed -e "s#^.*\($CRIS_CHK.*$\)#\1#"`
		[ "$L1" = "$L2" ] || {
			rm -f $L
			ln -s $L2 $L
			echo "$L relinked"
			echo -e "\tfrom: $L1"
			echo -e "\tto:	$L2"
		}
	}
done

exit 0
