#!/usr/bin/env bash

link_modules="alphasort dirfd getopt scandir strcasestr strndup xvasprintf"
header_modules="stdint"

test_link() {
	echo "main() { "$1"(); }" | gcc -x c -fno-builtin -o /dev/null - 2>/dev/null
}

test_header() {
	echo "#include <$1.h>" | cpp -o /dev/null - 2>/dev/null
}

for x in $link_modules; do
	[[ ${x:0:1} == x ]] && test=${x:1} || test=$x
	test_link "$test" || tmp="$tmp $x" >&2
done

for x in $header_modules; do
	test_header "$x" || tmp="$tmp $x" >&2
done

[[ -n "$tmp" ]] && echo "gnulib module(s) to add: $tmp"