From 83eb7d861edf57a432ed69127b7dbd945cd3cec0 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 12 Jul 2023 22:44:08 +0300 Subject: [PATCH] feat(tools): sha256-matcher, cheat script tweaks --- local/bin/x-sha256sum-matcher | 50 ++++++++++++++++++++++++++ scripts/install-cheat-purebashbible.sh | 6 ++-- scripts/install-cheat-tldr.sh | 6 ++-- scripts/shared.sh | 39 ++++++++++++++++++++ 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100755 local/bin/x-sha256sum-matcher diff --git a/local/bin/x-sha256sum-matcher b/local/bin/x-sha256sum-matcher new file mode 100755 index 0000000..bd049f5 --- /dev/null +++ b/local/bin/x-sha256sum-matcher @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# x-sha256sum-matcher +# +# Check if two files are the same +# +# Ismo Vuorinen 2023 +# MIT License + +# ENV Variables +: "${VERBOSE:=0}" # VERBOSE=1 x-sha256sum-matcher file1 file2 + +file_1="$1" +file_2="$2" + +# return sha256sum for file +# $1 - filename (string) +get_sha256sum() { + sha256sum "$1" | head -c 64 +} + +[ $# -eq 0 ] && { + echo "Usage: $0 file1.sh file2.sh" && exit 1 +} + +msg() { + [[ "$VERBOSE" -eq 1 ]] && echo "$1" +} + +error() { + msg "(!) ERROR: $1" && exit 1 +} + +if [ ! -f "$file_1" ]; then + error "File 1 does not exist: $file_1" +fi + +if [ ! -f "$file_2" ]; then + error "File 2 does not exist: $file_2" +fi + +file_1_hash=$(get_sha256sum "$file_1") +file_2_hash=$(get_sha256sum "$file_2") + +if [ "$file_1_hash" != "$file_2_hash" ]; then + error "Files do not match" +else + msg "(*) Success: Files do match" + exit 0; +fi + diff --git a/scripts/install-cheat-purebashbible.sh b/scripts/install-cheat-purebashbible.sh index 5c1d7df..43c9a7d 100755 --- a/scripts/install-cheat-purebashbible.sh +++ b/scripts/install-cheat-purebashbible.sh @@ -35,8 +35,10 @@ for f in ${PBB_CHAPTERS[@]}; do HEADER=$(grep -e '^[#] ' "$f" | head -1 | awk '{print tolower($2)}') CHEAT_FILE="$CHEAT_DEST/${HEADER}" - if [ ! -f "$CHEAT_FILE" ]; then - cp "$f" "$CHEAT_FILE" && msg_run "$CHEAT_FILE" + replacable "$f" "$CHEAT_FILE" + override=$? + if [ "$override" -ne 1 ]; then + cp "$f" "$CHEAT_FILE" && msg_run "Updated: $CHEAT_FILE" fi LC_ALL=C perl -pi.bak -e 's/\<\!-- CHAPTER END --\>//' "$CHEAT_FILE" diff --git a/scripts/install-cheat-tldr.sh b/scripts/install-cheat-tldr.sh index c02d2f4..2d2968f 100755 --- a/scripts/install-cheat-tldr.sh +++ b/scripts/install-cheat-tldr.sh @@ -51,8 +51,10 @@ for d in "$TLDR_TEMP_DIR"/pages/*; do TLDR_FILE="$SECTION_DIR/${FILENAME}" # echo "-> dest: $TLDR_FILE" - if [ ! -f "$TLDR_FILE" ]; then - cp "$FILE" "$TLDR_FILE" && msg_run "$TLDR_FILE" + replacable "$FILE" "$TLDR_FILE" + override=$? + if [ "$override" -ne 1 ]; then + cp "$FILE" "$TLDR_FILE" && msg_run "Updated: $TLDR_FILE" fi if [ -f "$TLDR_FILE" ] && [ '---' != "$(head -1 < "$TLDR_FILE")" ]; then diff --git a/scripts/shared.sh b/scripts/shared.sh index 41bc00b..f6e31d6 100755 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -230,3 +230,42 @@ rnd() { echo $RANDOM | md5sum | head -c 20 } + +# return sha256sum for file +# $1 - filename (string) +function get_sha256sum() +{ + sha256sum "$1" | head -c 64 +} + +# Replacable file +# +# $1 - filename (string) +# $2 - filename (string) +# +# Returns 1 when replacable, 0 when not replacable. +function replacable() +{ + FILE1="$1" + FILE2="$2" + + [[ ! -r "$FILE1" ]] && { msg_err "File 1 ($FILE1) does not exist" && return 1; } + [[ ! -r "$FILE2" ]] && { msg_err "File 2 ($FILE2) does not exist" && return 1; } + + FILE1_HASH=$(get_sha256sum "$FILE1") + FILE2_HASH=$(get_sha256sum "$FILE2") + + [[ $FILE1_HASH = "" ]] && { + msg_err "Could not get hash for file 1 ($FILE1)" && return 1; + } + [[ $FILE2_HASH = "" ]] && { + msg_err "Could not get hash for file 2 ($FILE2)" && return 1; + } + + [[ "$FILE1_HASH" == "$FILE2_HASH" ]] && { + msg_ok "Files match, not replacable" && return 0; + } + + return 1; +} +