diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e6bb828 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root = true + +[*.{sh,bash,cfg,example}] +charset = utf-8 +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79fd34c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/compare +webdiff.conf* +!webdiff.dist diff --git a/webdiff.conf.dist b/webdiff.conf.dist new file mode 100644 index 0000000..98f5be4 --- /dev/null +++ b/webdiff.conf.dist @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +# wd_callback_error +wd_callback_error() { + local diffname="${1:-}" + local diffurl="${2:-}" + local errortxt="${3:-}" + + echo "Webdiff failed for ${diffname} with error: ${errortxt}" + echo " ${diffurl}" +} + +# wd_callback_diff +wd_callback_diff() { + local diffname="${1:-}" + local diffurl="${2:-}" + local difflines="${3:-}" + local difffile="${4:-}" + + echo "Webdiff complete for ${diffname} with ${difflines} lines from" + echo " ${diffurl}" + echo "Result can be found: ${difffile}" +} diff --git a/webdiff.sh b/webdiff.sh new file mode 100755 index 0000000..f13437c --- /dev/null +++ b/webdiff.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +# webdiff.sh [CUSTOM SED] +# +# Author: Martin Winkler +# License: Please refer to the repository +# Origin: https://winklerfamilie.eu/git/SmallThings/webdiff.git + +# To get error from any command in the pipe +set -o pipefail + +webdiff() { + local retval=0 + local diffname="$1" + local diffurl="$2" + local diffline= # line count of current diff + + readonly _origin="$(cd "$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" + readonly newgrab="/tmp/webdiff/new_${diffname}" + readonly oldgrab="${_origin}/compare/old_${diffname}" + readonly diffnow="/tmp/webdiff/diff_${diffname}.txt" + + readonly wd_config="${_origin}/webdiff.conf" + + # end if no configuration is found + . "${wd_config}" 2>/dev/null || { printf "No configuration found\n"; return 1; } + + # check parameter + if [ $# -lt 2 ]; then + echo Error + exit 1 + else + shift 2 + fi + + mkdir -p "$(dirname "${newgrab}")" "$(dirname "${oldgrab}")" + + # Save only the html part for comparison + curl -sL "${diffurl}" | grep -v "script id=" \ + | sed -n '//,/<\/body/p' > "${newgrab}" \ + || { wd_callback_error "${diffname}" "${diffurl}" "curl"; return 1; } + + # Apply custom sed command + if [[ "${1:-}" ]]; then + cat "${newgrab}" | sed "$@" > "${newgrab}.2" + mv "${newgrab}.2" "${newgrab}" + fi + + # Initial diff shall not fail + [ ! -e "${oldgrab}" ] && cp "${newgrab}" "${oldgrab}" + + # Write diff to file ${diffnow} and count lines + difflines=$(diff "${newgrab}" "${oldgrab}" 2>/dev/null | tee "${diffnow}" | wc -l 2>/dev/null) + [ $? -gt 1 ] && { wd_callback_error "${diffname}" "${diffurl}" "diff"; return 1; } + + if [ ${difflines} -ne 0 ]; then + wd_callback_diff "${diffname}" "${diffurl}" "${difflines}" "${diffnow}" + install -b -S "_$(date +%Y%m%d_%H%M%S).bck" "${newgrab}" "${oldgrab}" + retval=1 + fi + + rm -f "${newgrab}" + + return ${retval} +} + +webdiff "$@" +