#! /bin/bash
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2018-04-02 12:20:19 +0000 (Mon, 02 Apr 2018) $
#$Revision: 4494 $
#$URL: file:///home/saulius/svn-repositories/paskaitos/VU/software/assignment-evaluation/trunk/scripts/do-check-BPKM-3rd--getfasta-line-lengths $
#------------------------------------------------------------------------------
#*
# Check that no executable files contain lines longer than the permitted
# length (usually 80 columns).
#**

set -ue
## set -x

DIRECTORY="$1"

ID='$Id: do-check-BPKM-3rd--getfasta-line-lengths 4494 2018-04-02 12:20:19Z 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

PROGNAME=$(basename $0 | awk -F- '{print $6}')

cd "${DIRECTORY}"

find . -name "${PROGNAME}" \
| while read FILE
do
    ## echo "$0: checking if I need to examine '$FILE'" >&2
    ## head -1 "$FILE" >&2

    # Files with correct shebang will be checked by a generic
    # 'do-check--line-lengths' script, no need to check them here:
    if ! [[ $(head -1 "$FILE") =~ ^#!" "*/ ]]
    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
