#!/usr/pkg/bin/bash
##   DO NOT EDIT. This is an auto-generated file.
##   Created on Tue Oct 21 23:55:49 UTC 2025

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

. "$pkgdatadir/util.sh"

die()
{
    local base="$(basename "$0")"
    printErr "$base: error: $@"
    exit 1
}

show_version_and_exit()
{
    echo "git-sqlite - alpha"
    echo "License: MIT"
    exit 0
}

show_help_and_exit()
{
    local base="$(basename "$1")"
    echo "git-sqlite - a diff & merge driver for sqlite

Usage: $BASE [OPTIONS] <COMMAND> DATABASE <SCHEMA>

COMMAND - one of 'init' or 'attach'
    init - initialize the database
    attach - attach the config
DATABASE - sqlite3 database
SCHEMA - alternate schema. Only used during 'init'. Sourced from package_dir (typically /usr/local/share/git-sqlite)

Options:
    -h        = This help screen.
    -V        = print version.

Website: 
bug-reports/questions: cannadayr@gmail.com

"

    exit
}

prjCheck()
{
    local repo="$1"
    local db="$2"

    # test if theres a .git directory
    if [ ! -d "$repo/.git" ]
    then
        printErr "'$repo' isn't the parent directory of a git repository"
        exit 1
    fi

    # test that the db path is relative
    if isAbsPath "$db"; then
        printErr "please use a relative path to the database"
        exit 1
    fi
}

init()
{
    local repo="$(pwd)"
    local db="$1"

    schema=
    if [ -n "$2" ]; then
        if [ ! -f "$pkgdatadir/$2" ]; then
            die "schema $2 doesn't exist"
        else
            schema="$2"
        fi
    else
        schema="schema.sql"
    fi

    prjCheck "$repo" "$db"

    # test that the db file doesn't exist
    if [ -f "$repo/$db" ]; then
        printErr "'$db' exists, refusing to create"
        exit 1
    fi

    sqlite3 "$db" < "$pkgdatadir/$schema"
}

attach()
{
    local repo="$(pwd)"
    local db="$1"

    prjCheck "$repo" "$db"

    # test that the db file exists
    if [ ! -f "$repo/$db" ]; then
        printErr "'$db' doesn't exist"
        exit 1
    fi

    # test that we've got a sqlite3 database
    if  ! stringContains "SQLite format 3" "$(head -c 15 < "$repo/$db")"; then
        echo "'$db' is not sqlite database" >&2
        exit 1
    fi

    # add diff section
    git config diff.sqlite.binary "true"
    git config diff.sqlite.command "git-sqlite-diff"

    # add merge section
    git config merge.sqlite.name "sqlite merge"
    git config merge.sqlite.driver "git-sqlite-merge %O %A %B %L %P"

    # add git show-sql alias
    git config alias.show-sql "show --ext-diff -m"

    # add git apply-sql alias
    git config alias.apply-sql "!sqlite3 ${db} < ${db}-.merge_file.sql && rm ${db}-.merge_file.sql && git add ${db}"

    # modify local .gitattributes
    attributes="$db diff=sqlite merge=sqlite"

    touch "$repo/.gitattributes"

    if ! stringContains "$attributes" "$(cat "$repo/.gitattributes")"; then
        echo "$attributes" >> "$repo/.gitattributes"
    fi
}

# Default values for parameters
show_usage=
show_version=
verbose=

# Parse parameters
while getopts Vvh param
do
    case $param in
    h)   show_help=1;;
    V)   show_version=1;;
    ?)   die "unknown command line option";;
    esac
done
shift $(($OPTIND - 1))

# Validate parameters
test -n "$show_help" && show_help_and_exit
test -n "$show_version" && show_version_and_exit
test -z "$1" && die "missing command. See -h for help"
test -z "$2" && die "missing database. See -h for help"

# Set inputs
cmd="$1"
db="$2"
schema="$3"

# run the command
case $cmd in
    "init")   init "$db" "$schema";;
    "attach") attach "$db";;
    *)      die "unknown command. See -h for help"
esac
