Blame sofort/tools/srctree.sh

28d4ad
#!/bin/sh
28d4ad
28d4ad
# srctree.sh: support for out-of-tree builds in posix make mode.
28d4ad
# this file is covered by COPYING.SOFORT.
28d4ad
28d4ad
set -eu
28d4ad
28d4ad
usage()
28d4ad
{
28d4ad
cat << EOF >&2
28d4ad
28d4ad
Usage:
28d4ad
  --help              show this HELP message
28d4ad
  --srctree=SRCTREE   set source directory
28d4ad
28d4ad
EOF
28d4ad
exit 1
28d4ad
}
28d4ad
28d4ad
28d4ad
# one
28d4ad
workdir=$(pwd -P)
28d4ad
srctree=
28d4ad
argloop=
28d4ad
28d4ad
28d4ad
for arg ; do
28d4ad
	case "$arg" in
28d4ad
		--help)
28d4ad
			usage
28d4ad
			;;
28d4ad
28d4ad
		--srctree=*)
28d4ad
			srctree=${arg#*=}
28d4ad
			;;
28d4ad
28d4ad
		--)
28d4ad
			argloop='done'
28d4ad
			;;
28d4ad
28d4ad
		*)
28d4ad
			if [ -z "$argloop" ]; then
28d4ad
				printf 'Invalid option: %s\n' "$arg" >&2
28d4ad
				usage
28d4ad
			fi
28d4ad
			;;
28d4ad
	esac
28d4ad
done
28d4ad
28d4ad
28d4ad
# two
28d4ad
if [ -z "$srctree" ] ; then
28d4ad
	usage
28d4ad
fi
28d4ad
28d4ad
cd -- "$srctree"
28d4ad
srctree=$(pwd -P)
28d4ad
cd -- "$workdir"
28d4ad
28d4ad
if [ "$srctree" = "$workdir" ]; then
28d4ad
	exit 0
28d4ad
fi
28d4ad
28d4ad
28d4ad
# three
28d4ad
for arg ; do
28d4ad
	case "$arg" in
28d4ad
		--srctree=*)
28d4ad
			;;
28d4ad
28d4ad
		--)
28d4ad
			;;
28d4ad
28d4ad
		*)
28d4ad
			stat "$arg" > /dev/null 2>&1 \
28d4ad
				|| ln -s -- "$srctree/$arg" "$arg"
28d4ad
			;;
28d4ad
	esac
28d4ad
done
28d4ad
28d4ad
28d4ad
# all done
28d4ad
exit 0