#!/bin/sh

ref="$1"
tmp="`echo "x$1" | sed -e 's:.*/tests/::' -e 's:\\.out$::' -e 's:^x\\./::'`"
stem="`echo "${tmp}" | sed -e 's:-spice-.*:@@@:' -e 's:\\(-[^-@]*\\|@@@\\)$::'`"
backend="`echo "${tmp}" | sed -e "s:^${stem}-::"`"

GNETLIST="${abs_top_builddir}/gnetlist-legacy/src/gnetlist-legacy"
GEDADATA="${abs_top_srcdir}/gnetlist-legacy" # HACKHACKHACK
GEDADATARC="${abs_top_builddir}/gnetlist-legacy/lib"
GEDALOG="."
export GEDADATA
export GEDADATARC
export GEDALOG

schematic="${abs_srcdir}/${stem}.sch"

rundir="run.$$"

# create temporary run directory
mkdir -p "${rundir}"

# Create the files needed
if test -d "${srcdir}/${stem}-extras" ; then
    for f in "${srcdir}/${stem}-extras"/* ; do
        cp "${f}" "${rundir}/"
    done
fi

(echo refdes; echo value; echo device) > "${rundir}/attribs"

cat > "${rundir}/gafrc" << EOF
;; We want to point at the scheme code that hasn't been installed yet so that
;; 'make check' during development can work correctly.
(scheme-directory "${abs_top_srcdir}/gnetlist-legacy/scheme")
(scheme-directory "${abs_top_builddir}/gnetlist-legacy/scheme")
(scheme-directory "${abs_top_srcdir}/libgeda/scheme")
(scheme-directory "${abs_top_builddir}/libgeda/scheme")

;; We want to use the symbols that haven't been installed yet
(map (lambda (x)
       (component-library
        (string-join (list "${abs_top_srcdir}/symbols/" x) "")))
     '("io" "74" "analog" "power" "misc" "diode" "linear" "connector" "switch"
       "asic" "spice" "cascade" "titleblock" "memory"))

;; Rather than checking what m4 and pcb's m4 files do with
;; the output we produce, just see what output gnetlist-legacy produces.
(define gsch2pcb:pcb-m4-command-line "cat - >>")
EOF

if test -d "${srcdir}/${stem}-symbols" ; then
    cat >> "${rundir}/gafrc" << EOF
(component-library "${abs_srcdir}/${stem}-symbols")
EOF
fi
if test -d "${srcdir}/${stem}-sources" ; then
    cat >> "${rundir}/gafrc" << EOF
(source-library "${abs_srcdir}/${stem}-sources")
EOF
fi

args=
case "${backend}" in
spice-sdb-nomunge)
    backend=spice-sdb
    args="-O nomunge_mode"
    ;;
spice-sdb-include)
    backend=spice-sdb
    args="-O include_mode"
    ;;
spice-sdb-include-nomunge)
    backend=spice-sdb
    args="-O include_mode -O nomunge_mode"
    ;;
spice-sdb-sort)
    backend=spice-sdb
    args="-O sort_mode"
    ;;
spice-sdb-sort-nomunge)
    backend=spice-sdb
    args="-O sort_mode -O nomunge_mode"
    ;;
esac

# run gnetlist-legacy
echo "${GNETLIST} -g ${backend} ${args} ${schematic}"
(cd "${rundir}" && "${GNETLIST}" -g "${backend}" ${args} "${schematic}")
rc1=$?

echo "${GNETLIST} -g ${backend} -o - ${args} ${schematic} > stdout.net"
(cd "${rundir}" && "${GNETLIST}" -g "${backend}" \
                                 -o - ${args} "${schematic}" > stdout.net)
rc2=$?

echo "${GNETLIST} -g ${backend} -v -o verbose.net ${args} ${schematic}"
(cd "${rundir}" && "${GNETLIST}" -g "${backend}" -v \
                                 -o verbose.net ${args} "${schematic}")
rc3=$?

# OK, now check results of run.
status=99

out="${rundir}/output.net"
std="${rundir}/stdout.net"
vrb="${rundir}/verbose.net"

# Hack to help with vams backend
if [ -f "${rundir}/default_entity_arc.net" ]; then
    mv "${rundir}/default_entity_arc.net" "${out}"
    # vams intentionally outputs data into several files, so checking it with
    # the option '-o verbose.net' is nonsense
    cp "${out}" "${vrb}"
fi

if test "X${REGEN}" = "X1" ; then
    # copy output on top of golden output
    cp "${out}" "${ref}"
    echo "Regenerated ${ref}"
    status=0
elif test ${rc1} -ne 0 ; then
    echo "FAILED:  gnetlist-legacy -g ${backend} returned ${rc1}"
    status=1
elif test ${rc2} -ne 0 ; then
    echo "FAILED:  gnetlist-legacy -g ${backend} -o - returned ${rc2}"
    status=1
elif test ${rc3} -ne 0 ; then
    echo "FAILED:  gnetlist-legacy -g ${backend} -v returned ${rc3}"
    status=1
else
    sed '/gnetlist -g/d' "${ref}" > "${out}.tmp1"
    sed '/gnetlist-legacy -g/d' "${out}" > "${out}.tmp2"
    sed '/gnetlist-legacy -g/d' "${std}" > "${out}.tmp3"
    sed '/gnetlist-legacy -g/d' "${vrb}" > "${out}.tmp4"

    # Hack to help with allegro backend
    # Device files are ignored as yet
    if test "X${backend}" = "Xallegro" ; then
        sed '/gnetlist-legacy -g/d' "${std}" | sed '/^\$END$/ q' > "${out}.tmp3"
    fi

    if ! diff "${out}.tmp1" "${out}.tmp2" >/dev/null; then
        echo "FAILED: Wrong plain output. See diff ${ref} ${out}"
        status=1
    elif ! diff "${out}.tmp1" "${out}.tmp3" >/dev/null; then
        echo "FAILED: Wrong stdout output. See diff ${ref} ${std}"
        status=1
    elif ! diff "${out}.tmp1" "${out}.tmp4" >/dev/null; then
        echo "FAILED: Wrong verbose output. See diff ${ref} ${vrb}"
        status=1
    else
        echo "PASS"
        status=0
    fi
fi

# Delete the run directory in prep for the next test
rm -fr "${rundir}"

exit ${status}
