#!/bin/sh

set -eu

search_dir=.
is_git=""
has_submodules=""
if root=$(git rev-parse --show-toplevel 2>/dev/null); then
  is_git=true
  if git check-ignore --quiet .; then
    search_dir="${root}"
  fi

  if [ -f "${root}/.gitmodules" ]; then
    has_submodules=true
  fi
fi

if [ -n "${is_git}" ]; then
  if [ -n "${has_submodules}" ]; then
    git ls-files "${search_dir}" --cached --exclude-standard --recurse-submodules 2>/dev/null && \
      git ls-files "${search_dir}" --exclude-standard --others 2>/dev/null
  else
    git ls-files "${search_dir}" --cached --exclude-standard --others 2>/dev/null
  fi
else
  if command -v fdfind >/dev/null; then
    fdfind --type file --type symlink "${search_dir}"
  else
    fd --type file --type symlink "${search_dir}"
  fi
fi
