#!/bin/bash

. ${TOOLKIT}-functions.sh

for link in `jtconf --list links.`
do
	desktop=`jtconf $link.desktop 2>/dev/null`	|| continue
	action=`jtconf $link.action 2>/dev/null`
	link=`jtconf $link.link 2>/dev/null` 		|| continue
	set $link
	source=$1

	desktop=$HOME/$desktop
	target=$desktop/`basename $source`

	[ "$action" = "remove" ] && {
		if [ -L "$target" ]
		then
			echo "  remove link $target"
			rm -f "$target"
		else
			[ -f "$target" -o -d "$target" ] && {
				echo "  can't remove $target: not a symlink"
			}
		fi
		continue
	}

	[ -d $source -o -f $source ] || continue

	[ -d $desktop ] || {
		mkdir $desktop || exit $?
		user=`id | sed -e 's/.*uid=//' -e 's/(.*//'`
		group=`id | sed -e 's/.*gid=//' -e 's/(.*//'`
		chown $user:$group $desktop
	}
	updatelink $source $target || exit $?
	chown $user:$group $target
done

exit 0
