#!/bin/sh
#
# Generate sqlite3_namespace.h from sqlite3.c
#
# This script extracts all public SQLite3 function and data symbols
# (marked with SQLITE_API) and generates a header that renames them
# with a heim_ prefix to avoid symbol conflicts.
#
# Usage: ./gen-sqlite3-namespace.sh [sqlite3.c]
#
# Run this script after importing a new version of SQLite3.

set -e

SQLITE3_C="${1:-sqlite3.c}"
OUTPUT="sqlite3_namespace.h"

if [ ! -f "$SQLITE3_C" ]; then
    echo "Error: $SQLITE3_C not found" >&2
    exit 1
fi

# Extract symbols from SQLITE_API declarations in sqlite3.c
# This catches functions and data symbols like sqlite3_open, sqlite3_version, etc.
# Also catches sqlite3session_*, sqlite3changegroup_*, sqlite3rbu_*, etc.
grep '^SQLITE_API' "$SQLITE3_C" | grep -oE 'sqlite3[a-zA-Z0-9_]*' | sort -u > /tmp/sqlite_symbols.$$

# Clean up temp files on exit
trap "rm -f /tmp/sqlite_symbols.$$" EXIT

# Count symbols
NUM_SYMBOLS=$(wc -l < /tmp/sqlite_symbols.$$)
echo "Found $NUM_SYMBOLS symbols to rename"

# Generate the header
cat > "$OUTPUT" << 'HEADER_EOF'
/*
 * Copyright (c) 2025 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * This header renames all public SQLite3 function and data symbols to use
 * a heim_ prefix to avoid symbol conflicts when both libheimsqlite and
 * libsqlite3 are loaded in the same process.
 *
 * This file is auto-generated by gen-sqlite3-namespace.sh
 * Regenerate after importing a new SQLite version:
 *
 *   cd lib/sqlite && ./gen-sqlite3-namespace.sh
 */

#ifndef SQLITE3_NAMESPACE_H
#define SQLITE3_NAMESPACE_H

HEADER_EOF

# Generate the #define lines
while read sym; do
    echo "#define $sym heim_$sym"
done < /tmp/sqlite_symbols.$$ >> "$OUTPUT"

cat >> "$OUTPUT" << 'FOOTER_EOF'

#endif /* SQLITE3_NAMESPACE_H */
FOOTER_EOF

echo "Generated $OUTPUT with $NUM_SYMBOLS symbol renames"
