#! /bin/bash
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2021-05-04 04:58:48 +0000 (Tue, 04 May 2021) $
#$Revision: 8935 $
#$URL: file:///home/saulius/svn-repositories/paskaitos/VU/software/assignment-evaluation/trunk/scripts/do-check--Perl-syntax $
#------------------------------------------------------------------------------
#*
# Check that all Perl programs compile.
#**

set -ue
## set -x

DIRECTORY="$1"

ID='$Id: do-check--Perl-syntax 8935 2021-05-04 04:58:48Z saulius $'

cd "${DIRECTORY}"

EXTRA_PERL5PATH=$(find -name '*.pm' \
                  | xargs --no-run-if-empty -n1 dirname \
                  | sort -u \
                  | tr "\n" ":" \
                  | sed 's/:$//' \
)

PERL5LIB=${EXTRA_PERL5PATH:+${EXTRA_PERL5PATH}:}${PERL5LIB}

export PERL5LIB

find . \
     -name .svn -prune -o \
     -name .git -prune -o \
     -name __MACOSX -prune -o \
     \( -type f -perm /111 -o -name '*.pl' \) -print \
| sort \
| while read FILE
do
    ## echo "$0: checking if I need to examine '$FILE'" >&2
    ## head -1 "$FILE" >&2
    if ( [[ $(strings "$FILE" | head -1) =~ ^#!" "*/ ]] \
             && grep -q 'bin/perl' $FILE ) || \
           [[ "$FILE" == *.pl ]]
    then
        ## echo "$0: checking file '$FILE'" >&2
        if ! perl -c $FILE > /dev/null 2>&1
        then
cat <<EOF
-- [AUTO;CODE;$(echo $ID | sed 's/\$//g')]
   Jūsų programa '$FILE' turi sintaksės klaidų (kelios pradinės parodytos)
   (-30 balų):

$( (set -x; perl -c $FILE) 2>&1 | head -7)

EOF
        fi
    fi
done
