#!/bin/sh
set -e

usage() {
	printf '%s\n' "usage: $(basename "$0") [--file|--word] pattern" \
	              "       $(basename "$0") -h|--help"
}

basic_regex_quote() { printf "%s" "$1" | sed 's|[\\.*^$[]|\\&|g'; }
glob_quote () { printf "%s" "$1" | sed 's|[\\?*[]]|\\&|g'; }

COMPLETE_WORD=0
FIND_FILE_LIMIT=1000

while [ $# -gt 0 ]; do
	case "$1" in
	--file)
		COMPLETE_WORD=0
		shift
		;;
	--word)
		COMPLETE_WORD=1
		shift
		;;
	--)
		shift
		break
		;;
	-h|--help)
		usage
		exit 0
		;;
	-*)
		usage
		exit 1
		;;
	*)
		break
		;;
	esac
done

if [ $# -ne 1 ]; then
	usage
	exit 1
fi

PATTERN="$1"

if [ $COMPLETE_WORD = 1 ]; then
	tr -s '\t {}()[],<>%^&.\\' '\n' |
		grep "^$(basic_regex_quote "$PATTERN")." |
		sort -u
else
	# Expand to absolute path because of the -path option below.
	case $PATTERN in
		/*)
			XPATTERN=$PATTERN
			;;
		'~'|'~/'*)
			XPATTERN=$HOME$(echo $PATTERN | tail -c +2)
			;;
		*)
			XPATTERN=$PWD/$PATTERN
			;;
	esac

	# The first path condition rules out paths that start with "." unless
	# they start with "..". That way, hidden paths should stay hidden, but
	# non-normalised paths should still show up.
	find $(dirname "$XPATTERN") \
		-name '.*' -prune \
		-o \( \
			! -name '.*' \
			-a -path "$(glob_quote "$XPATTERN")*" \
			-print \
		\) 2>/dev/null |
		head -n $FIND_FILE_LIMIT |
		sort |
		sed "s|^$(dirname $XPATTERN)/||"
fi |
	vis-menu -b |
	sed "s|^$(basename $PATTERN)$(echo $PATTERN | tail -c 2 | grep -F /)||" |
	tr -d '\n'
