#!/bin/bash

# Usage: ./test.sh [test/dir]

# Get this script's directory (full, non-relative; agnostic to symlinks)
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
CURR_DIR=$(pwd)

DSR_TRIM=$SCRIPT_DIR/dsr-trim/dsr-trim
SAT_CHECK=$SCRIPT_DIR/check_sat.sh

KISSAT="$SCRIPT_DIR/kissat"
RUN_SATSUMA_KISSAT="$SCRIPT_DIR/run_satsuma_kissat.sh"
RUN_SATSUMA_AE="$SCRIPT_DIR/run_satsuma_ae_kissat.sh"

SOL_FILE="$SCRIPT_DIR/cnf.sol"
PROOF="$SCRIPT_DIR/proof.out"

TIMEOUT=120

ERR_CODE=0
SAT_CODE=10
UNSAT_CODE=20

if [ $# -gt 1 ]; then
    echo "Usage: $0 [test/dir]" >&2
    exit 1
fi

# Complain if dsr-trim and sat-filter aren't present
for TOOL in "$DSR_TRIM" "$SAT_CHECK"; do
    if [ ! -f "$TOOL" ]; then
        echo "c $TOOL not found!"
        exit 1
    fi
done

if [ $# -eq 1 ]; then
    DIR="$1"
    FORMULAS="*.cnf"
else
    DIR=$SCRIPT_DIR/tests
    FORMULAS="*.cnf assorted_sat/*.cnf assorted_unsat/*.cnf coloring/*.cnf"
fi

cd $DIR

for CNF in $FORMULAS ; do
    # All of the tests should be quick to solve
    echo "Testing $CNF"

    if [ -f "$PROOF" ]; then
        rm "$PROOF"
    fi

    # Run base kissat and store the return value
    # "$KISSAT" $CNF $PROOF --time=$TIMEOUT > /dev/null
    # KISSAT_RETURN_CODE=$?
    #
    # # Check the DRAT proof if it is UNSAT
    # if [ $KISSAT_RETURN_CODE -eq $UNSAT_CODE ]; then
    #     # Check output of dsr-trim is "s VERIFIED UNSAT"
    #     DSR_TRIM_OUTPUT=$($DSR_TRIM $CNF $PROOF /dev/null -q)
    #     if [[ $DSR_TRIM_OUTPUT != *"s VERIFIED UNSAT"* ]]; then
    #         echo "c dsr-trim failed to verify the proof for $CNF"
    #     fi
    # fi

    # Run satsuma + kissat
    "$RUN_SATSUMA_KISSAT" $CNF $SCRIPT_DIR > $SOL_FILE
    KISSAT_SB_RETURN_CODE=$?

    # if [ $KISSAT_SB_RETURN_CODE -ne $KISSAT_RETURN_CODE ] && [ $KISSAT_RETURN_CODE -ne $ERR_CODE ]; then
    #     echo "c Satsuma+Kissat returned different results for $CNF: $KISSAT_RETURN_CODE vs $KISSAT_SB_RETURN_CODE"
    # fi
    #
    if [ $KISSAT_SB_RETURN_CODE -eq $UNSAT_CODE ]; then
        DSR_TRIM_OUTPUT=$($DSR_TRIM -f $CNF $PROOF /dev/null)
        if [[ $DSR_TRIM_OUTPUT != *"s VERIFIED UNSAT"* ]]; then
            echo "c dsr-trim failed to verify the appended proof for $CNF" >&2
        fi
    elif [ $KISSAT_SB_RETURN_CODE -eq $SAT_CODE ]; then
        # Check the model satisfies the formula
        $SAT_CHECK $CNF $SOL_FILE > /dev/null
        RES=$?
        if [ $RES -ne 0 ]; then
            echo "c Model check failed for $CNF with Satsuma+Kissat" >&2
        fi
    else
        echo "c Unexpected return code from Satsuma+Kissat for $CNF: $KISSAT_SB_RETURN_CODE" >&2
    fi

    # Run satsuma + AE kissat
    "$RUN_SATSUMA_AE" $CNF $SCRIPT_DIR > $SOL_FILE
    AE_KISSAT_SB_RETURN_CODE=$?

    # if [ $AE_KISSAT_SB_RETURN_CODE -ne $KISSAT_RETURN_CODE ] && [ $KISSAT_RETURN_CODE -ne $ERR_CODE ]; then
    #     echo "c AE Kissat returned different results for $CNF: $KISSAT_RETURN_CODE vs $AE_KISSAT_SB_RETURN_CODE"
    # fi

    if [ $AE_KISSAT_SB_RETURN_CODE -eq $UNSAT_CODE ]; then
        DSR_TRIM_OUTPUT=$($DSR_TRIM -f $CNF $PROOF /dev/null)
        if [[ $DSR_TRIM_OUTPUT != *"s VERIFIED UNSAT"* ]]; then
            echo "c dsr-trim failed to verify the appended proof for $CNF with AE Kissat" >&2
        fi
    elif [ $AE_KISSAT_SB_RETURN_CODE -eq $SAT_CODE ]; then
        # Check the model satisfies the formula
        $SAT_CHECK $CNF $SOL_FILE > /dev/null
        RES=$?
        if [ $RES -ne 0 ]; then
            echo "c Model check failed for $CNF with Satsuma+AE_Kissat" >&2
        fi
    else
        echo "c Unexpected return code from Satsuma+AE_Kissat for $CNF: $AE_KISSAT_SB_RETURN_CODE" >&2
    fi
done

# Cleanup
if [ -f "$PROOF" ]; then
    rm "$PROOF"
fi

cd $CURR_DIR
