#!/bin/sh

tree=$(realpath $PWD)

# check for reasonable symlinks
! find . -type l | while read file; do
    # detect symlinks that don't link to anything
    if ! realpath "$file" 1>/dev/null; then
        echo "symlink links to nothing: $file -> $(readlink "$file")"
    fi
    # detect symlinks that link outside the tree
    if [ -n "$(realpath "$file" | grep -Pv "^\\Q$tree/")" ]; then
        echo "symlink outside of pwd: $file -> $(readlink "$file")"
    fi
done | grep . || exit 1

# check for prebuilt binary files
list=$(find . -type f -executable -not -empty | perl -lne 'print if -B')
if [ -n "$list" ]; then
    echo "prebuilt binary files:"
    echo "$list"
    exit 1
fi
