#!/bin/bash -pe
# Author: © 2013-2014 Gert-jan Los <los@hacke.rs>
# Licentie: GPL v3 of volgend (http://www.gnu.org/licenses/gpl-3.0.txt)

shopt -s extglob

find=(locate --null --all --existing)

while [ $# -gt 0 ] ;do
	if [ "$1" = '--' ] ;then
		shift
		break
	else
		case "$1" in
			*)  find+=("$1")   ;;
		esac
		shift
	fi
done

case "${0##*/}" in
	rlox) restrict() { grep -za "^$PWD/" ; } ;;
	*) restrict() { cat ; }
esac

if [ $# -ge 1 ] ;then
	exec "${find[@]}" |restrict |xargs -0r "$@"
elif [ -t 1 ] ;then
	exec "${find[@]}" |restrict |xargs -0r la -dF
else
	exec "${find[@]}" |restrict |xargs -0r ls -adF
fi
