This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
FREAX/replace.sh
2022-07-18 19:33:40 +02:00

96 lines
2.9 KiB
Bash
Executable file

echo "» replace.sh"
FIND_REGEX=$1
FIND_REPLACEMENT=$2
REPLACE_PROCESSED_FILES=
REPLACE_PROCESSED_DIRECTORIES=
REPLACE_SKIPPED_FILES=
REPLACE_SKIPPED_DIRECTORIES=
REPLACE_SKIPPED_UNKNOWN=
function convert() {
if [ "$REPLACE_VERBOSE" == "true" ]; then
echo "convert($1 $2 $3)"
fi
sed -i "s/$2/$3/g" "$1"
}
function runProcess() {
cd "$file"
echo "$2» Processing directory $1"
REPLACE_PROCESSED_DIRECTORIES=$REPLACE_PROCESSED_DIRECTORIES+1
echo "$2»» Processing files"
for file in $(ls -1); do
if [ -f "$file" ]; then
if [ "$file" == "replace.sh" ] || [ "$file" == "README.md" ] || [ "$file" == "LICENSE.md" ]; then
REPLACE_SKIPPED_FILES=$REPLACE_SKIPPED_FILES+1
if [ "$REPLACE_SHOWERR" == "true" ]; then
echo "$2»»» Skipping file $file (blacklisted)"
else
echo -n ""
fi
else
if [ "${file: -4}" == ".lua" ] || [ "${file: -3}" == ".db" ] || [ "$file" == "fstab" ]; then
REPLACE_PROCESSED_FILES=$REPLACE_PROCESSED_FILES+1
echo "$2»»» Processing file $file"
convert "$file" "$FIND_REGEX" "$FIND_REPLACEMENT"
else
REPLACE_SKIPPED_FILES=$REPLACE_SKIPPED_FILES+1
if [ "$REPLACE_SHOWERR" == "true" ]; then
echo "$2»»» Skipping file $file (unknown file type)"
else
echo -n ""
fi
fi
fi
else
if [ -d "$file" ]; then
runProcess "$1/$file" "$2»"
else
REPLACE_SKIPPED_UNKNOWN=$REPLACE_SKIPPED_UNKNOWN+1
if [ "$REPLACE_SHOWERR" == "true" ]; then
echo "$2»»» Skipping unknown file $file (not a file or directory)"
else
echo -n ""
fi
fi
fi
done
if [ ! "$1" == "." ]; then
cd ..
fi
}
runProcess "$(pwd)" "»"
echo "»» Generating statistics"
if [ "$REPLACE_PROCESSED_FILES" == "" ]; then
REPLACE_PROCESSED_FILES=" 0"
else
echo "»»» Calculating PROCESSED_FILES"
REPLACE_PROCESSED_FILES=$(calc $REPLACE_PROCESSED_FILES)
fi
if [ "$REPLACE_PROCESSED_DIRECTORIES" == "" ]; then
REPLACE_PROCESSED_DIRECTORIES=" 0"
else
echo "»»» Calculating PROCESSED_DIRECTORIES"
REPLACE_PROCESSED_DIRECTORIES=$(calc $REPLACE_PROCESSED_DIRECTORIES)
fi
if [ "$REPLACE_SKIPPED_FILES" == "" ]; then
REPLACE_SKIPPED_FILES=" 0"
else
echo "»»» Calculating SKIPPED_FILES"
REPLACE_SKIPPED_FILES=$(calc $REPLACE_SKIPPED_FILES)
fi
if [ "$REPLACE_SKIPPED_DIRECTORIES" == "" ]; then
REPLACE_SKIPPED_DIRECTORIES=" 0"
else
echo "»»» Calculating SKIPPED_DIRECTORIES"
REPLACE_SKIPPED_DIRECTORIES=$(calc $REPLACE_SKIPPED_DIRECTORIES)
fi
if [ "$REPLACE_SKIPPED_UNKNOWN" == "" ]; then
REPLACE_SKIPPED_UNKNOWN=" 0"
else
echo "»»» Calculating SKIPPED_UNKNOWN"
REPLACE_SKIPPED_UNKNOWN=$(calc $REPLACE_SKIPPED_UNKNOWN)
fi
echo "Processed files: $REPLACE_PROCESSED_FILES"
echo "Processed directories: $REPLACE_PROCESSED_DIRECTORIES"
echo "Skipped files: $REPLACE_SKIPPED_FILES"
echo "Skipped directories: $REPLACE_SKIPPED_DIRECTORIES"
echo "Skipped unknown: $REPLACE_SKIPPED_UNKNOWN"