#!/bin/sh

set -eu
distdir="$1"

cat "${distdir}/node_modules/README"

# include license information for
#   a) all directly-shipped node_modules
#   b) all node modules included by things we ship in dist/
modules="${distdir}/node_modules $(cat ${distdir}/dist/*/included-modules)"
files="$(find ${modules} -name 'COPYING' -o -name '*LICENSE*' | LC_ALL=C sort)"

found=""
for filename in ${files}; 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
