#! /bin/bash
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2021-05-04 04:37:44 +0000 (Tue, 04 May 2021) $
#$Revision: 8926 $
#$URL: file:///home/saulius/svn-repositories/paskaitos/VU/software/assignment-evaluation/trunk/scripts/do-check--line-lengths $
#------------------------------------------------------------------------------
#*
# Check that no files contain lines longer than the permitted length
# (usually 80 columns).
#**

set -ue
## set -x

DIRECTORY="$1"

ID='$Id: do-check--line-lengths 8926 2021-05-04 04:37:44Z saulius $'

# Maximum allowed line of text in program code files:
MAX_LINE_LENGTH=80

# Penalty, in points, for each line that is too long:
PENALTY_PER_LINE=2

# Number of extra long lines to show:
NLONG=5

cd "${DIRECTORY}"

find . \
     -name '*~' -prune -o \
     -name .svn -prune -o \
     -name .git -prune -o \
     -name __MACOSX -prune -o \
     -name bibliophile -prune -o \
     -type f -print \
| grep -v 'hooks/post-revprop-change.tmpl' \
| while read FILE
do
    ## echo "$0: checking if I need to examine '$FILE'" >&2
    ## head -1 "$FILE" >&2
    if [[ $(strings "$FILE" | head -1) =~ ^#!" "*/ ]]
    then
        ## echo "$0: checking file '$FILE'" >&2
        COUNT=$(awk -v MAX=$MAX_LINE_LENGTH '!/\$(Id|URL)/{if(length > MAX) print}' "$FILE" | wc -l)
        if [ $COUNT -gt 0  ]
        then
            cat <<EOF
-- [AUTO;STYLE;$(echo $ID | sed 's/\$//g')]
   Jūsų faile "$FILE" yra per ilgų eilučių,
   jų skaičius: $COUNT.

   Ilgesnės už $MAX_LINE_LENGTH simbolių gali būti tik Subversijos Id \
ir URL eilutės.
   Už kiekvieną nepagrįstai per ilgą eilutę atimami $PENALTY_PER_LINE balai,
   taigi viso (-$(expr $PENALTY_PER_LINE \* $COUNT) balai/-ų).
   Žr. (išvesta iki $NLONG per ilgų eilučių):

$(awk -v MAX=$MAX_LINE_LENGTH '!/\$(Id|URL)/{if(length > MAX) print}' "$FILE" \
| head -n $NLONG)

EOF
        fi
    fi
done
