#!/bin/sh

set -euf

if [ -f $1/README ]; then
    cat $1/README
fi

found=""
find $1 -name 'COPYING' -o -name '*LICENSE*' -print | LC_ALL=C sort | while read filename; do
    directory=$(dirname $filename)
    name=$(basename $directory)

    case $found in
    "* $name *")
        # skip
        ;;
    *)
        found="$found $name "
        printf "\n\n--------------------------------------------------------------\n## %s\n\n" $name
        cat $filename
        ;;
    esac
done
