#!/usr/bin/env bash

MSYS_BIN=/c/msys64/mingw64/bin/
EMACS_DEPS_FILE=$(mktemp)
trap 'rm "$EMACS_DEPS_FILE"' EXIT

# Go to the msys64 directory and expand the mingw64 DLL filenames.
cd "$MSYS_BIN"
files=$(echo libharfbuzz*dll \
	     libjansson*dll \
	     libXpm*dll \
	     libgif*dll \
	     libtiff*dll \
	     libjpeg*dll \
	     libpng*dll \
	     librsvg*dll \
	     liblcms*dll \
	     libgnutls*dll \
	     libxml*dll \
	     zlib*dll)

for f in $files; do
    # Record Emacs's direct dependencies.
    echo "$f"

    # Record Emacs's transitive dependencies, excluding w32 system DLLs.
    ldd "$f" \
	| grep /mingw64/bin/ \
	| cut -f1 -d' ' \
	| tr -d '\040\011\015'
done >> "$EMACS_DEPS_FILE"

# Remove duplicate DLLs and print.
sort "$EMACS_DEPS_FILE" | uniq
