#!/usr/bin/env bash
# gccmakedep for MinGW
# Does not include system headers -- just local files
#  mingwmakedep -C compiler <-I, -D, -U> <source-files>

usage()
{
    echo >&2 "usage: $me [-C compiler] [-I... -D... -U...] source-files"
    exit 1
}

me=${0##*/}

compiler=gcc
if [ "X$1" = X-C ]
then	compiler=$2
	shift 2
fi

unset args
unset srcs
while [ $# -gt 0 ]
do	case "$1" in
	-I*|-D*|-U*)
		args="$args $1"
		shift
		;;
	-*)
		echo >&2 "unknown option $1"
		usage
		;;
	*)	srcs="$srcs $1"
		shift
		;;
	esac
done

for cfile in $srcs
do	deps=$($compiler -E $args $cfile |
	 grep '^# 1 ' |
	 sed 's/^# 1 "\(.*\)".*/\1/' |
	 sed 's/^\.\///' |
	 sort -u |
	 grep -v '^<' |
	 grep -v '^/usr/lib' |
	 grep -v $cfile)
	echo "${cfile%.c}.o: $(echo $deps)"
done
