#!/bin/bash

# Copyright (C) 2025 Tether Operations Limited
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Author: Gotam Gorabh <gautamy672@gmail.com>
#
# Scans specified source directories for 'gchar' usage.
#
# Usage:
#   ./g-check src tests plugins
# If no directories are provided, defaults to 'src'.

set -e

# Use command-line arguments if given, otherwise default to 'src'
SOURCE_DIRS=("$@")
if [ ${#SOURCE_DIRS[@]} -eq 0 ]; then
    SOURCE_DIRS=("src")
fi

# Use a temporary file for the list of files to check
temp_files=$(mktemp)
trap 'rm -f "$temp_files"' EXIT

# Collect .c and .h files from the given directories
find "${SOURCE_DIRS[@]}" -type f \( -name "*.c" -o -name "*.h" \) > "$temp_files"

status=0

while read -r file; do
    # Check each file for 'gchar'
    if grep -nH "\bgchar\b" "$file"; then
        echo ":: warning: Avoid using 'gchar' in $file"
        status=1
    fi
done < "$temp_files"

if [ $status -ne 0 ]; then
    echo
    echo "gchar usage detected. Please use 'char' over 'gchar'."
fi

exit $status
