how/how.sh

59 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env sh
set -e
HOWROOT="${HOWROOT:-$(dirname "$(readlink -f "$0")")}"
if [ ! -d "$HOWROOT" ]; then
echo "$HOWROOT is not a directory!" >&2
exit 1
fi
{ [ "$1" ] && [ "$2" ]; } \
|| { eval "echo \"$(cat "$HOWROOT/USAGE")\"" && exit 0; }
. "$HOWROOT/lib/log.sh"
pkg="$1"
ver="$2"
tgt="$3"
if [ "$tgt" ] && [ ! -d "$tgt" ]; then
echo "$tgt isn't a directory!" >&2
exit 1
fi
{ vercmp formats | grep -q "^$pkg"; } \
&& fmt="$pkg" \
|| fmt="default"
[ ! -d "$HOWROOT/pkg/$pkg/default" ] || layers="$HOWROOT/pkg/$pkg/default:"
if [ -d "$HOWROOT/pkg/$pkg" ]; then
layers="$(ls -d $HOWROOT/pkg/$pkg/*/ \
| rev | cut -d/ -f2 | rev \
| shsort "vercmp -f $fmt" \
| while read dir; do
[ "$dir" ] || continue;
[ "$dir" != "default" ] || continue;
if [ "$(vercmp -f "$fmt" "$dir <= $ver")" = "yes" ]; then
printf "$HOWROOT/pkg/$pkg/$dir:"
fi
done)$layers"
fi
[ "$tgt" ] || { echo "$layers$HOWROOT/default" && exit 0; }
# mounting an overlayfs fails if only given a single lower directory, so use a
# read-only bind mount if just mounting the default scripts directory. the
# behaviour of a read-only bind mount should be roughly equivalent to an
# overlayfs mounted with a single lower directory and no upper directory. (the
# only difference which comes to mind is that a bind mount does not count
# against overlayfs' nesting limit.)
if [ "$layers" ]; then
mount -t overlay -o lowerdir="$layers$HOWROOT/default" none "$tgt"
else
mount -o ro,bind "$HOWROOT/default" "$tgt"
fi