blob: acd48fb0ba731a90edab6301e49ad8aa3949520d [file] [log] [blame] [raw]
get_version() {
sed -En 's/^ *"?rncn-config-version"? *: *([0-9]+),?$/\1/p'
}
# Usage: check_and_update_config <remote-url> <local-path> [<service-name>]
check_and_update_config() {
local cfg remote_version local_version
cfg="`wget --no-verbose \"$1\" -O -`" || return
remote_version="`printf '%s\\n' "$cfg" | get_version`"
if [ -z "$remote_version" ]; then
echo "Newly fetched configuration file has no version field?!" 1>&2
return 1
fi
if [ -f "$2" ]; then
local_version="`get_version < \"$2\"`"
[ -n "$local_version" ] && [ "$local_version" -ge "$remote_version" ] && return
fi
rm -f "$2.old"
printf %s\\n "$cfg" > "$2"
[ $# = 2 ] && return
if ! service "$3" restart; then
printf "Failed to restart %s, rollback to old version\\n" "$2"
mv "$2.old" "$2"
service "$3" restart
return
fi
echo "Newer configuration has been found and updated." 1>&2
}