#! /bin/bash
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2024-06-08 08:28:52 +0000 (Sat, 08 Jun 2024) $
#$Revision: 11449 $
#$URL: file:///home/saulius/svn-repositories/paskaitos/VU/software/assignment-evaluation/trunk/scripts/do-check--Ada-style $
#------------------------------------------------------------------------------
#*
# Check that Perl files are written according to the guidlines.
#**

set -ue
## set -x

DIRECTORY="$1"

PENALTY_PER_CRITIQUE=10

ID='$Id: do-check--Ada-style 11449 2024-06-08 08:28:52Z saulius $'

gnatmake=gnatmake

if ! type -p $gnatmake >& /dev/null
then
    echo $0: WARNING, this script depends on the \
         \"$gnatmake\" program to check Ada style >&2
    echo "" >&2
    exit 0
fi

cd "${DIRECTORY}"

find . \
     -name .svn -prune -o \
     -name .git -prune -o \
     -name __MACOSX -prune -o \
     \( -type f -name '*.ad[bs]' \) -print \
| sort \
| while read FILE
do
    ## echo "$0: checking if I need to examine '$FILE'" >&2
    ## head -1 "$FILE" >&2
    if [[ "$FILE" == *.ad[bs] ]]
    then
        ## echo "$0: checking file '$FILE'" >&2
        CRITIQUE="$(cd "$(dirname "${FILE}")"; \
                    gnatclean -q "$(basename "$FILE")" 1>&2; \
                    (gnatmake -q -gnaty3dSux+C "$(basename "$FILE")") 2>&1
        )"
        if [ -n "$CRITIQUE" ] && grep -qE '\(style\)|warning' <<< "$CRITIQUE"
        then
cat <<EOF
-- [AUTO;STYLE;$(echo $ID | sed 's/\$//g')]
   Jūsų programa '$FILE' turi nusiskundimų iš '$gnatmake' analizatoriaus:

${CRITIQUE}

EOF
        fi
    fi
done
