#!/usr/pkg/bin/bash
##   DO NOT EDIT. This is an auto-generated file.
##   Created on Fri Jul 24 17:02:28 UTC 2026

prefix="/usr/pkg"
pkgdatadir="${prefix}/share/git-sqlite"  # e.g. /usr/local/share/PACKAGE/

. "$pkgdatadir/util.sh"

# we get the /tmp/ param in a different spot depending on
# what kind of diff logic/context git is performing
# sometimes it's in $2, sometimes in $5
# if we don't choose the /tmp/ file, we just end up diffing
# the same file against itself and lose the proper diff.
# even weirder, we sometimes have to swap the arguments (see below)
getFirstTmpFileIdx()
{
    local path1="$1"
    local path2="$2"
    local path1start=${path1:0:1}
    local path2start=${path2:0:1}

    # not actually looking in /tmp/ because MacOS stores its temp files elsewhere
    # just look for any absolute path
    if [ "$path1start" = '/' ] \
        && [ "$path2start" = "/" ]
    then
        # we are probably doing a `git show-sql`
        echo 0
    elif [ "$path1start" = '/' ]; then
        echo 1
    elif [ "$path2start" = '/' ]; then
        echo 2
    else
        printErr "getFirstTmpFileVal ERROR: neither '$path1' nor '$path2' are /tmp/ values"
    fi
}

#printErr "$@"
toDbCandidate1="$2"
toDbCandidate2="$5"
#printErr "toDbCandidate1 = $toDbCandidate1"
#printErr "toDbCandidate2 = $toDbCandidate2"

# we are always interested in the /tmp/ file which has the version we're comparing to
# depending on which arg has the /tmp/ file, we need to change the order of
# the sqldiff args for some reason barely unknown to us
tempFileIdx="$(getFirstTmpFileIdx "$toDbCandidate1" "$toDbCandidate2")"
#printErr "tempFileIdx $tempFileIdx"

if [ $tempFileIdx = 0 ]; then
    fromDb="$2"
    toDb="$5"
elif [ $tempFileIdx = 1 ]; then
    fromDb="$toDbCandidate1"
    toDb="$1"
elif [ $tempFileIdx = 2 ]; then
    fromDb="$toDbCandidate2"
    toDb="$1"
else
    printErr 'ERROR: tmpDb not set to either $2 or $5'
fi

sha="$2"
perm="$3"

#printErr "fromDb $fromDb"
#printErr "$(sqlite3 "$fromDb" ".dump")"
#printErr "toDb $toDb"
#printErr "$(sqlite3 "$toDb" ".dump")"

#todo #fixme - this echos a newline if there's no diff
diff="$(diffDb "$fromDb" "$toDb")"
if [ -n "$diff" ]; then

    # display a bold diff header
    printErr "$(tput bold)sqldiff $toDb $fromDb$(tput sgr0)"
    printErr "$(tput bold)index $sha $perm$(tput sgr0)"
    echo "$diff"
fi

#exit 1;
