Blame sofort/tools/srctree.sh

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