#!/bin/sh -e
#
# broken-symlinks - find broken symlinks in a directory

usage() {
  printf %s "\
$(basename "$0")
  a simple script to find broken symlinks in given/current directory

commands:
  [d]           - Max depth to search. [default:5]
"
  exit 1
}

# maximum depth to search
maxdepth=5

while getopts d:h o; do
  case "${o}" in
    d) maxdepth="${OPTARG}" ;;
    h) usage ;;
    *) shift ;;
  esac
done

shift "$((OPTIND - 1))"
dir="${1:-.}"

find "${dir}" -maxdepth "${maxdepth}" -type l -exec test ! -e {} \; -print
