#!/bin/sh
# Code to swap links with their originals

filelist=$(ls)

localdir=$PWD
for fn in $filelist
do
    if [ -h $fn ]; then
	tg=$(realpath $fn )
	# act only if target is not a link
	if [ ! -h $tg ]; then
	    origbasename=$(basename $tg)
	    origdir=$(dirname $tg)
	    newtarget=$(realpath --relative-to $origdir ./)/$origbasename
	    newlink=$(realpath --relative-to ./ $origdir)/$fn
            #echo $fn, $tg, $newtarget, $newlink
	    # do swap:
	    rm $fn
	    mv $tg $fn
	    ln -s $newtarget $newlink
	fi
    fi
done
