#!/bin/sh
# Compare the stage1 and stage2 versions of every automatically-generated
# file of the specified kinds in the library directory. The possible kinds
# are interface files, optimization files, and C headers and sources.
#
# The intended use is debugging changes in the compiler that affect the
# generation of such files. The stage1 versions are generated by a known-good
# compiler and are known to work (if they didn't work, there would be
# no stage 1 compiler to generate stage 2), so if stage 2 won't build
# due to a problem with an interface file in the library, the differences
# output by this script should contain the culprit(s).

cd library

for filekind in "$@"
do
    case "${filekind}" in
        "int")
            kind_exts="int3 int2 int int0"
            ;;
        "opt")
            kind_exts="opt trans_opt"
            ;;
        "hdr")
            kind_exts="mih mh"
            ;;
        "c")
            kind_exts="c"
            ;;
        *)
            echo "${filekind} is not a kind of file"
            exit 1
            ;;
    esac

    for module in *.m
    do
        for ext in ${kind_exts}
        do
            m=`basename ${module} .m`
            f="${m}.${ext}"
            if test -f "${f}"
            then
                if test -f "../stage2/library/${f}"
                then
                    diff -u "${f}"  "../stage2/library/${f}" > .diff
                    if test -s .diff
                    then
                        echo "=== $f ==="
                        cat .diff
                        rm .diff
                    fi
                else
                    echo "XXX ${f} not in stage2 XXX"
                fi
            fi
        done
    done
done
