Check an online ics file for changes
This commit is contained in:
102
icscheck/icscheck.sh
Executable file
102
icscheck/icscheck.sh
Executable file
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Author: Martin Winkler
|
||||
# License: Please refer to the repository
|
||||
# Origin: https://winklerfamilie.eu/git/SmallThings/icsutils.git
|
||||
|
||||
|
||||
# Exit on error inside any functions or subshells.
|
||||
set -o errtrace
|
||||
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
|
||||
set -o nounset
|
||||
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
|
||||
set -o pipefail
|
||||
|
||||
icsusage() {
|
||||
cat<<USAGE_END
|
||||
Usage: ${0##/*} [OPTIONS] [ICSNAME]
|
||||
|
||||
Checks an online ics file for changes. A diff is created
|
||||
excluding "DTSTAMP:" lines, which change with every download.
|
||||
|
||||
[OPTIONS]
|
||||
--help, -h : Displays this help
|
||||
-- : End of options
|
||||
|
||||
[ICSNAME]
|
||||
An unique identifier for the comparisson process. Used for
|
||||
configuration ([ICSNAME].conf), temporary and cached files.
|
||||
Default: icscheck
|
||||
|
||||
USAGE_END
|
||||
}
|
||||
|
||||
error() {
|
||||
printf '%b%s%b\n\n' "${color_red}" "${1:-}" "${color_less}"
|
||||
icsusage
|
||||
}
|
||||
|
||||
# icscheck <ICSNAME>
|
||||
icscheck() {
|
||||
readonly origin="$(cd "$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
|
||||
|
||||
local arg=
|
||||
local ics_name=
|
||||
local ics_url=
|
||||
local ics_output=
|
||||
local ics_config=
|
||||
local diff_ignore=
|
||||
local renew=0
|
||||
|
||||
color_red= ; [ -t 1 ] && color_red='\033[0;31m'
|
||||
color_less= ; [ -t 1 ] && color_less='\033[0m' # No Color
|
||||
|
||||
for arg in "${@}" ; do
|
||||
case "${1:-}" in
|
||||
--help|-h)
|
||||
icsusage && return 0 ;;
|
||||
--renew|-r)
|
||||
renew=1 && shift ;;
|
||||
--)
|
||||
shift && break ;;
|
||||
esac
|
||||
done
|
||||
|
||||
ics_name="${1:-"icscheck"}"
|
||||
ics_config="${origin}/${ics_name}.conf"
|
||||
. "${ics_config}" 2>/dev/null || { error "${ics_config} not found" && return 1; }
|
||||
|
||||
ics_url="${icsconf_url:?"$(error "URL to ics file must not be empty.")"}"
|
||||
ics_output="${icsconf_output:?"$(error "Download destination must not be empty")"}"
|
||||
|
||||
readonly ics_temp_dir="/tmp/icscheck"
|
||||
readonly ics_store_dir="${origin}/store"
|
||||
readonly ics_down_file="$ics_temp_dir/${ics_name}_new.ics"
|
||||
readonly ics_store_file="${ics_store_dir}/${ics_name}.last.ics"
|
||||
readonly ics_diff="${ics_store_dir}/${ics_name}.diff"
|
||||
|
||||
mkdir -p "${ics_temp_dir}" "${ics_store_dir}"
|
||||
|
||||
wget -O "$ics_down_file" "$ics_url" >/dev/null 2>&1
|
||||
|
||||
# -s : exists and size greater zero
|
||||
if [ ! -s "$ics_down_file" ] || [ ! -r "$ics_down_file" ]; then
|
||||
icsError
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Delete storage if manual renew is requested
|
||||
(( renew )) && rm -f "${ics_store_file}"
|
||||
|
||||
# Find differences in download and send notification
|
||||
diff_ignore="^DTSTAMP:.*"
|
||||
diff <(grep -vE "$diff_ignore" "$ics_down_file") <(grep -vE "$diff_ignore" "$ics_store_file" 2>/dev/null) > "$ics_diff" 2>/dev/null
|
||||
if [ $? -eq 1 ]; then
|
||||
icsChanged "${ics_down_file}" "${ics_store_file}" "${ics_diff}"
|
||||
|
||||
# Save download for next comparison
|
||||
cp -f "$ics_down_file" "$ics_store_file"
|
||||
fi
|
||||
}
|
||||
|
||||
icscheck "${@}"
|
Reference in New Issue
Block a user