#!/bin/bash
# This file is to be run from a bash script under unix/linux/macOS
# for windows 10 and up, WSL needs to be installed
SP=""
RE=""
REGEXT=".*\.\(php\|inc\|css\|md\|js\|sql\)$"
if [[ $(uname) == "Darwin" ]]; then
# Needed for macOS X
SP=" " # Needed for portability with sed
RE="-E"
REGEXT=".*\.(php|inc|css|md|js|sql)$" # no escaping regex parts
fi
pushd() { builtin pushd $1 > /dev/null; }
popd() { builtin popd $1 > /dev/null; }
pushd ..
echo make line endings standard
#echo excluding hidden files
find ${RE} . -type f -not -path '*/\.*' -regex ${REGEXT} \
-exec bash -c "LC_CTYPE=C LANG=C sed -i${SP}'.bak' \"s/$(printf '\r')//g\" {}" \;
echo replace tab char with 4 spaces in every file from here
find ${RE} . -type f -not -path '*/\.*' -regex ${REGEXT} \
-exec bash -c "LC_CTYPE=C LANG=C sed -i${SP}'.bak' -e \"s/$(printf '\t')/ /g\" {}" \;
echo replace trailing spaces
find ${RE} . -type f -not -path '*/\.*' -regex ${REGEXT} \
-exec bash -c "LC_CTYPE=C LANG=C sed -i${SP}'.bak' -e \"s/ *$//g\" {}" \;
echo remove backup files generated in the process
find ${RE} . -type f -not -path '*/\.*' -regex ${REGEXT} \
-exec test -f {}.bak \; -exec rm {}.bak \;
popd
#eof