#!/bin/sh
#
# fup - look upwards from $PWD for filename $1, output to stdout if found
# does not follow symlinks.
#
# ---
# source: https://github.com/davidosomething/dotfiles

set -eu

main() {
  current_path="${PWD}"
  filename="$1"
  filepath=
  while :; do
    filepath="${current_path}/${filename}"
    [ -f "${filepath}" ] && printf '%s\n' "${filepath}" && break
    if [ "${current_path}" = '/' ] || [ ! -e "${current_path}" ]; then
      return 1
    fi
    current_path=$(dirname "${current_path}")
  done
}
main "$@"
