#!/usr/bin/env bash
#
# trailblank - find trailing blanks and other file format picky issues
#
# This tools is used by "make prep".
#
# If all is well, this tool prints nothing and does an exit 0.
# If a problem is found, this tool prints messages and does a non-0 exit.
#
# Copyright (C) 2017,2018,2021-2023  Landon Curt Noll
#
# Calc is open software; you can redistribute it and/or modify it under
# the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
#
# Calc is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
# Public License for more details.
#
# A copy of version 2.1 of the GNU Lesser General Public License is
# distributed with calc under the filename COPYING-LGPL.  You should have
# received a copy with calc; if not, write to Free Software Foundation, Inc.
# 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
#
# Under source code control:    2017/05/22 03:28:20
# File existed as early as:     2017
#
# chongo <was here> /\oo/\      http://www.isthe.com/chongo/
# Share and enjoy!  :-)         http://www.isthe.com/chongo/tech/comp/calc/

# exit code
#
export EXIT_CODE=0

# Look for leading whiitespace before a tab
#
# We exclude binary files, RCS source code history, intermediate
# compiled files, patch droppings and compiled binary libraries.
#
# We also ignore filenames with a single letter, and files
# that end in .out, and files  containing the any of the following
# in any letter case:
#
#       foo bar baz curds whey rmme
#
# Such files are temporary/test files and are not part of calc.
#
# Last, we have two files that have long lines that, for now,
# we cannot be as picky about without significant work.
#
LEADING_SPACES_BEFORE_TAB=$(
    find . \( -path './NOTES' -o -path './calc' -o -path './ver_calc' -o \
              -name '*.o' -o -name '*.a' -o -name '*ptch*' -o -name 'core*' -o \
              -name '*.orig' -o -name '*.rej' -o -name '*.bak' -o \
              -name '*.ptch' -o -name 'core.*' -o \
              -name '*.so*' -o -name 'calc-static' -o -name 'libcalc.*' -o \
              -name 'sample_many' -o -name 'sample_rand' -o -name 'errcode' -o \
              -path './help/funclist' -o -path './have_stdvs' -o \
              -path './endian' -o -path './no_implicit' -o -name 'chk_c' -o \
              -path './longbits' -o -name '.*.swp' -o -name 'conf.h' -o \
              -name '.git' -o -path './custom/libcustcalc*' -o -path './libcustcalc*' -o \
              -name 'sample_many-static' -o -name 'sample_rand-static' -o \
              -name 'codeql-analysis.yml' -o -name tags -o -name '*.out' -o \
              -name '?' -o -iname '*foo*' -o -iname '*bar*' -o -iname '*baz*' -o \
              -iname '*curds*' -o -iname '*whey*' -o -iname '*rmme*' \
           \) -prune -o -type f -print0 | \
    xargs -0 egrep -l '^        *  *    '
)
if [[ -n $LEADING_SPACES_BEFORE_TAB ]]; then
    echo
    echo '# files with leading spaces before a tab'
    echo "$LEADING_SPACES_BEFORE_TAB" | sed -e 's/^\.\///'
    EXIT_CODE=1
fi

# Look for trailing whitespace
#
# We exclude binary files, RCS source code history, intermediate
# compiled files, patch droppings and compiled binary libraries.
#
# We also ignore filenames with a single letter, and files
# that end in .out, and files  containing the any of the following
# in any letter case:
#
#       foo bar baz curds whey rmme
#
# Such files are temporary/test files and are not part of calc.
#
# Last, we have two files that have long lines that, for now,
# we cannot be as picky about without significant work.
#
TRAILING_WHITESPACE=$(
    find . \( -path './NOTES' -o -path './calc' -o -path './ver_calc' -o \
              -name '*.o' -o -name '*.a' -o -name '*ptch*' -o -name 'core*' -o \
              -name '*.orig' -o -name '*.rej' -o -name '*.bak' -o \
              -name '*.ptch' -o -name 'core.*' -o \
              -name '*.so*' -o -name 'calc-static' -o -name 'libcalc.*' -o \
              -name 'sample_many' -o -name 'sample_rand' -o -name 'errcode' -o \
              -path './help/funclist' -o -path './have_stdvs' -o \
              -path './endian' -o -path './no_implicit' -o -name 'chk_c' -o \
              -path './longbits' -o -name '.*.swp' -o -name 'conf.h' -o \
              -name '.git' -o -path './custom/libcustcalc*' -o -path './libcustcalc*' -o \
              -name 'sample_many-static' -o -name 'sample_rand-static' -o \
              -name '?' -o -iname '*foo*' -o -iname '*bar*' -o -iname '*baz*' -o \
              -iname '*curds*' -o -iname '*whey*' -o -iname '*rmme*' \
           \) -prune -o -type f -print0 | \
    xargs -0 egrep -l '[         ]$'
)
if [[ -n $TRAILING_WHITESPACE ]]; then
    echo
    echo '# files with trailing whitespace'
    echo "$TRAILING_WHITESPACE" | sed -e 's/^\.\///'
    EXIT_CODE=2
fi

# Perform special picky tool phase 0 processing
#
# Some of these files must have long lines.  Some of these files may not exist.
#
if [[ -x /usr/local/bin/picky ]]; then
    PICKY_PHASE_0A=$(/usr/local/bin/picky -w -s -v ./cal/set8700.line \
                                                   .gitignore README.md .lldbinit 2>&1)
    status="$?"
    if [[ $status -ne 0 ]]; then
        echo
        echo '# picky failed on one of the files with long lines'
        echo "$PICKY_PHASE_0A" | sed -e 's/\.\///'
        EXIT_CODE=3
    fi
    if [[ -e conf.h ]]; then
        PICKY_PHASE_0B=$(/usr/local/bin/picky -w -s -v conf.h 2>&1)
        status="$?"
        if [[ $status -ne 0 ]]; then
            echo
            echo '# picky failed on conf.h'
            echo "$PICKY_PHASE_0B" | sed -e 's/\.\///'
            EXIT_CODE=4
        fi
    fi
    if [[ -e errsym.h ]]; then
        PICKY_PHASE_0C=$(/usr/local/bin/picky -w -s -v errsym.h 2>&1)
        status="$?"
        if [[ $status -ne 0 ]]; then
            echo
            echo '# picky failed on errsym.h'
            echo "$PICKY_PHASE_0C" | sed -e 's/\.\///'
            EXIT_CODE=5
        fi
    fi
else
    echo "# $0: WARNING: /usr/local/bin/picky not found: skipping picky phase 0 checks!" 1>&2
    echo "#" 1>&2
fi

# Picky tool processing phase 1
#
# We exclude binary files, source code history, intermediate
# compiled files, patch droppings and compiled binary libraries.
#
# We also ignore filenames with a single letter, and files
# that end in .out, and files  containing the any of the following
# in any letter case:
#
#       foo bar baz curds whey rmme
#
# Such files are temporary/test files and are not part of calc.
#
# Last, we have files that have long lines that, for now,
# we cannot be as picky about without significant work.
#
PICKY_PHASE_1=$(
    find . \( -path './NOTES' -o -path './calc' -o -path './ver_calc' -o \
              -name '*.o' -o -name '*.a' -o -name '*ptch*' -o -name 'core*' -o \
              -name '*.orig' -o -name '*.rej' -o -name '*.bak' -o \
              -name '*.ptch' -o -name 'core.*' -o \
              -name '*.so*' -o -name 'calc-static' -o -name 'libcalc.*' -o \
              -name 'sample_many' -o -name 'sample_rand' -o -name 'errcode' -o \
              -path './help/funclist' -o -path './have_stdvs' -o \
              -path './endian' -o -path './no_implicit' -o -name 'chk_c' -o \
              -path './longbits' -o -name '.*.swp' -o -name 'conf.h' -o \
              -name '.git' -o -path './custom/libcustcalc*' -o -path './libcustcalc*' -o \
              -name 'sample_many-static' -o -name 'sample_rand-static' -o \
              -name 'codeql-analysis.yml' -o -name tags -o -name '*.out' -o \
              -iname '*foo*' -o -iname '*bar*' -o -iname '*baz*' -o \
              -name '.lldbinit' -o -iname '*curds*' -o -iname '*whey*' -o -iname '*rmme*' \
           \) -prune -o -type f -print0 | \
    if [[ -x /usr/local/bin/picky ]]; then
        xargs -0 /usr/local/bin/picky -s -v -w132
    else
        echo "# $0: WARNING: /usr/local/bin/picky not found: skipping picky phase 1 check!" 1>&2
        echo "#" 1>&2
        echo "# $0: NOTICE: The picky tool used is from the following GitHub repo:" 1>&2
        echo "#" 1>&2
        echo "#         https://github.com/lcn2/picky for the picky tool GitHub repo." 1>&2
        echo "#" 1>&2
    fi
)
if [[ -n $PICKY_PHASE_1 ]]; then
    echo
    echo '# files flagged by picky'
    echo "$PICKY_PHASE_1" | sed -e 's/^picky: \.\///' -e 's/ / - /'
    EXIT_CODE=6
fi

# look for backup Makefiles
#
BACKUP_MAKEILES=$(
    find . -name Makefile.bak -print
)
if [[ -n $BACKUP_MAKEILES ]]; then
    echo
    echo "# You need execute the following to remove backup Makefiles:"
    echo
    echo "$BACKUP_MAKEILES" | while read -r file; do
        echo "rm -f $file"
    done
    EXIT_CODE=7
fi


# look for ASCII tabs in non-Makefiles
#
TABS_FOUND=$(
    find . \( -path './NOTES' -o -path './calc' -o -path './ver_calc' -o \
              -name '*.o' -o -name '*.a' -o -name '*ptch*' -o -name 'core*' -o \
              -name '*.orig' -o -name '*.rej' -o -name '*.bak' -o \
              -name '*.ptch' -o -name 'core.*' -o \
              -name '*.so*' -o -name 'calc-static' -o -name 'libcalc.*' -o \
              -name 'sample_many' -o -name 'sample_rand' -o -name 'errcode' -o \
              -path './help/funclist' -o -path './have_stdvs' -o \
              -path './endian' -o -path './no_implicit' -o -name 'chk_c' -o \
              -path './longbits' -o -name '.*.swp' -o -name 'conf.h' -o \
              -name '.git' -o -path './custom/libcustcalc*' -o -path './libcustcalc*' -o \
              -name 'sample_many-static' -o -name 'sample_rand-static' -o \
              -name 'codeql-analysis.yml' -o -name tags -o -name '*.out' -o \
              -iname '*foo*' -o -iname '*bar*' -o -iname '*baz*' -o \
              -name '.lldbinit' -o -iname '*curds*' -o -iname '*whey*' -o -iname '*rmme*' -o \
              -iname 'Makefile*' -o -name rpm.mk -o -name 'trailblank' \
           \) -prune -o -type f -print0 | \
    xargs -0 grep -l -E '	'
)
if [[ -n $TABS_FOUND ]]; then
    echo
    echo '# ASCII TABs found in non-Makefiles:'
    echo "$TABS_FOUND"
    EXIT_CODE=8
fi

# All Done!!! -- Jessica Noll, Age 2
#
exit "$EXIT_CODE"
