#!/bin/sh -efu

. girar-sh-functions

PROG=girar-ls

usage()
{
	echo >&2 "$PROG: $*"
	echo >&2 "usage: $PROG [<path to directory>]"
	exit 1
}

[ "$#" -le 1 ] ||
	usage 'Too many arguments.'

[ -n "${GIRAR_HOME:-}" ] ||
	fatal 'GIRAR_HOME undefined'

cd

if [ "$#" -ge 1 ]; then
	dir="$1"; shift
	printf %s "$dir" |egrep -qs '^[A-Za-z0-9/][-A-Za-z0-9_./]+$' ||
		fatal "$dir: directory name contains unacceptable symbols"
else
	dir=.
fi

[ -d "$dir" ] ||
	fatal "$dir: directory not available"

[ -z "${dir%%*/}" ] || dir="$dir/"
[ -n "${dir##/*}" -o -z "${dir##$GIRAR_HOME/*}" ] ||
	fatal "$dir: directory does not belong to allowed directory tree"
d="$(readlink -e "$dir")/" ||
	fatal "$dir: directory not available"
p="$(readlink -e "$GIRAR_HOME")" ||
	fatal "$dir: directory not available"
[ -z "${d##$p/*}" ] ||
	fatal "$dir: directory does not belong to allowed directory tree"

exec ls -log -- "$dir"
