Blame sofort/tools/srctree.sh

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