#!/bin/sh -e
# See http://jdebp.uk./FGA/slashpackage.html
if test \! -d package || test \! -d source
then
	echo "You are not in the right directory." 1>&2
	exit 100
fi
case "`uname`" in
(Linux)		extended_regexp="-r" ; no_target_dir=-T ;;
(OpenBSD)	extended_regexp="-E" ; no_target_dir="" ;;
(*BSD)		extended_regexp="-E" ; no_target_dir=-h ;;
esac
cwd="`/bin/pwd`"
base_plus_version="`basename \"${cwd}\"`"
base="`echo "${base_plus_version}" | sed ${extended_regexp} -e 's/-[[:digit:]][[:alnum:].]*$//'`"
if [ "${base}" != "${base_plus_version}" ]
then
	echo "Creating symlink ${base} -> ${base_plus_version}."
	case "`uname`" in
	(OpenBSD)
		ln -s -f -- "${base_plus_version}" "${base}"
		mv -f -- "${base}" ../
		;;
	(*)
		ln -s -f -- "${base_plus_version}" ../"${base}"{new}
		mv -f ${no_target_dir} -- ../"${base}"{new} ../"${base}"
		;;
	esac
else
	echo 1>&2 "This is not a versioned package."
fi
exit 0
