#!/bin/sh
# compile-rules.sh - generate make rules for prog.mk and lib.mk
# usage: compile-rules.sh 'srcs' 'rcfiles' > rules.mk

if [ $# != 2 ]; then
    echo "$0: usage: $0 'srcs' 'rcfiles'" 1>&2
    exit 1
fi

SRCS="$1"
RCFILES="$2"

OBJS=`echo " $SRCS " | sed 's, \./, ,g;s/\.[^\.]* /$(OBJEXT) /g'`
OBJS=`echo "$OBJS" | sed 's/^ //;s/ *$//'`
echo "OBJS=$OBJS"

RESFILES=`echo $RCFILES " " | sed 's/\.[^\.]* /.res /g;s/ *$//'`
echo "RESFILES=$RESFILES"

for S in $SRCS; do
    echo $S | awk '
	{
	    src=$1;
	    if (src ~ "^\\./") {
		sub("^\\./", "", src);
		fullsrc = src;
	    }
	    else {
		fullsrc = "$(SRCDIR)/" src;
	    }

	    obj=src;
	    sub("\\.[^\\.]*$", "$(OBJEXT)", obj);

	    printf "%s: %s\n", obj, fullsrc;
	    printf "\t@echo \"        [CC]      %s\"\n", src;
	    printf "\t@$(CC) $(CFLAGS) $(CFLAGS.$(TYPE)) $(INCFLAGS)";
	    printf " -c %s\n", fullsrc;
	}
    '
done

for R in $RCFILES; do
    echo $R | awk '
	{
	    src=$1;
	    obj=src;
	    sub("\\.[^\\.]*$", ".res", obj);
	    printf "%s: $(SRCDIR)/%s\n", obj, src;
	    printf "\t@echo \"        [WINDRES] %s\"\n", src;
	    printf "\t@$(WINDRES) $(WINDRESFLAGS) -fo$@ $(SRCDIR)/%s\n", src;
	}
    '
done
