70 lines
1.9 KiB
Bash
Executable File
70 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# webdiff.sh <DIFFNAME> <DIFFURL> [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 difflines= # line count of current diff
|
|
|
|
readonly _origin="$(cd "$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
|
|
readonly newcurl="/tmp/webdiff/new_${diffname}"
|
|
readonly oldcurl="${_origin}/compare/old_${diffname}"
|
|
readonly diffnow="/tmp/webdiff/diff_${diffname}.txt"
|
|
|
|
readonly wd_config="${_origin}/webdiff.conf"
|
|
|
|
# end if no configuration is found
|
|
# shellcheck disable=SC1090
|
|
. "${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 "${newcurl}")" "$(dirname "${oldcurl}")"
|
|
|
|
# Save only the html <body> part for comparison
|
|
curl -sL "${diffurl}" | grep -v "script id=" \
|
|
| sed -n '/<body.*>/,/<\/body/p' > "${newcurl}" \
|
|
|| { wd_callback_error "${diffname}" "${diffurl}" "curl"; return 1; }
|
|
|
|
# Apply custom sed command
|
|
if [[ "${1:-}" ]]; then
|
|
sed "$@" < "${newcurl}" > "${newcurl}.2"
|
|
mv "${newcurl}.2" "${newcurl}"
|
|
fi
|
|
|
|
# Initial diff shall not fail
|
|
[ ! -e "${oldcurl}" ] && cp "${newcurl}" "${oldcurl}"
|
|
|
|
# Write diff to file ${diffnow} and count lines
|
|
difflines=$(diff "${newcurl}" "${oldcurl}" 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" "${newcurl}" "${oldcurl}"
|
|
retval=1
|
|
fi
|
|
|
|
rm -f "${newcurl}"
|
|
|
|
return ${retval}
|
|
}
|
|
|
|
webdiff "$@"
|
|
|