scripts/app/check.zoom.update

332 lines
9.9 KiB
Plaintext
Raw Permalink Normal View History

2021-06-09 16:33:01 +02:00
#!/bin/sh
# Purpose {{{
## Create a temp file (to monitor) if an upgrade is available for Zoom client
## from official website https://zoom.us/download
## It's based on .deb package installation to check the current version.
## It can also compare the current available version in APT repositories
## if "repo" is given as first argument.
## If a new version is available, the script will try to download it.
2021-06-09 16:33:01 +02:00
## How-to use {{{
### 1. Create a cron job, eg:
#00 20 * * * root /opt/repos/ipr.scripts/app/check.zoom.update
### 2-1 Create a cron job to compare the version available in an APT repository:
#00 20 * * * root /opt/repos/ipr.scripts/app/check.zoom.update --mode repo
2021-06-09 16:33:01 +02:00
### 2. Monitor the temp file: /tmp/.zoom.upgrade
# Or enable MAILTO in cronjob and edit the script to print a message.
# Or send a mail.
# …
## }}}
## Inspired by https://github.com/pazepaze/zoom-autoupdater/blob/master/autoupdate-zoom.sh
# }}}
# Vars {{{
PROGNAME=$(basename "${0}"); readonly PROGNAME
PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR
ARGS="${*}"; readonly ARGS
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script
export DEBUG
## Default values for some vars
CHECK_MODE_DEFAULT="file"
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
2021-06-09 16:33:01 +02:00
cat << HELP
usage: $PROGNAME [check_mode] [-m|-d|-h]
2021-06-09 16:33:01 +02:00
Compare current version of an installed Zoom client and the last available.
EXAMPLES:
- Compare the current version of Zoom installed from a .deb file
${PROGNAME}
${PROGNAME} --mode file
- Compare the current version of Zoom available in the APT repo
${PROGNAME} repo
${PROGNAME} --mode repo
2021-06-09 16:33:01 +02:00
OPTIONS:
-m,--mode
Set the check_mode to use to get current version of Zoom client
Available mode :
* repo
* file (default behaviour)
2021-06-09 16:33:01 +02:00
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
2021-06-09 16:33:01 +02:00
HELP
}
# }}}
debug_message() { # {{{
2021-06-09 16:33:01 +02:00
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
unset local_debug_message
return 0
}
2021-06-09 16:33:01 +02:00
# }}}
error_message() { # {{{
2021-06-09 16:33:01 +02:00
local_error_message="${1}"
local_error_code="${2}"
2021-06-09 16:33:01 +02:00
## Print message
printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
unset local_error_message
exit "${local_error_code:=66}"
}
# }}}
is_var_empty() { # {{{
## Return False by default
return_var_empty="1"
## Total number of variables to test
local_total_var_empty="${#}"
loop_count_var_empty="0"
## While it remains a variable to test
while [ "${local_total_var_empty}" -gt "${loop_count_var_empty}" ]; do
debug_message "is_var_empty \
Test var: ${1}."
### Test if this is empty and set return value to True
[ -z "${1}" ] && return_var_empty="0"
### Increase the number of tested variables
loop_count_var_empty=$((loop_count_var_empty+1))
2021-06-09 16:33:01 +02:00
### Shift to the next variable
shift
done
2021-06-09 16:33:01 +02:00
unset local_total_var_empty
unset loop_count_var_empty
2021-06-09 16:50:43 +02:00
return "${return_var_empty}"
}
2021-06-09 16:33:01 +02:00
# }}}
define_vars() { # {{{
2021-06-09 16:33:01 +02:00
## If check_mode wasn't defined (argument) {{{
## Use default value
is_var_empty "${check_mode}" \
&& debug_message "define_vars Use default value (${CHECK_MODE_DEFAULT}) for check_mode variable." \
&& check_mode="${CHECK_MODE_DEFAULT}"
## }}}
2021-06-09 16:33:01 +02:00
## Get zoom_current_version according to the check_mode {{{
case "${check_mode}" in
"repo" ) ## Check zoom version from repository
zoom_current_version=$(apt-cache policy -- zoom | awk '/Candidate:/ {print $2}' | sed 's/.:\(.*\)-.*/\1/')
;;
"file" ) ## Check zoom version from installed .deb file
zoom_current_version=$(dpkg --list -- zoom | awk '/^ii *zoom/ {print $3}' | sed 's/.:\(.*\)-.*/\1/')
;;
* ) ## unknow mode
error_message "define_vars Invalid check mode: ${check_mode}" 1
printf '%b\n' "---"
usage
exit 1
;;
esac
2021-06-09 16:50:43 +02:00
## If zoom_current_version is empty
is_var_empty "${zoom_current_version}" \
&& error_message "define_vars Error with zoom_current_version variable (${zoom_current_version})" 2
2021-08-23 19:02:42 +02:00
## }}}
2021-08-23 19:02:42 +02:00
## Zoom vars for new version {{{
temp_zoom_new_version=$(curl --silent 'https://zoom.us/support/download' --header 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36' | grep "class=\"linux-ver-text\"" | sed -e 's/.*Version \(.*\)<.*/\1/')
zoom_new_version_major=$(echo "${temp_zoom_new_version}" | sed -e 's/\(.*\) .*/\1/')
zoom_new_version_minor=$(echo "${temp_zoom_new_version}" | sed -e 's/.* (\(.*\))/\1/')
zoom_new_version="${zoom_new_version_major}.${zoom_new_version_minor}"
is_var_empty "${temp_zoom_new_version}" "${zoom_new_version_major}" "${zoom_new_version_minor}" "${zoom_new_version}" \
&& error_message "define_vars Error with new version variables (temp_zoom_new_version: ${temp_zoom_new_version} ; zoom_new_version_major: ${zoom_new_version_major} ; zoom_new_version_minor: ${zoom_new_version_minor} ; zoom_new_version: ${zoom_new_version})." 3
## }}}
## Vars for temp files
zoom_new_version_file="/tmp/.zoom.upgrade"
zoom_new_pkg_path="/tmp/zoom_${zoom_new_version}_amd64.deb"
zoom_tmp_pkg_path="/tmp/.zoom_${zoom_new_version}_amd64.deb"
}
# }}}
is_version_greater_than() { # {{{
first_value="${1}"
value_to_compare="${2}"
## Return False by default
return_is_version_greater_than="1"
debug_message "is_version_greater_than \
Is first value (${first_value}) greater than the second value (${value_to_compare})."
if printf '%s\n' "${first_value}" "${value_to_compare}" | sort --check=quiet --version-sort; then
debug_message "is_version_greater_than ${first_value} <= ${value_to_compare} ."
return_is_version_greater_than="1"
else
debug_message "is_version_greater_than ${first_value} > ${value_to_compare} ."
return_is_version_greater_than="0"
fi
unset first_value
unset value_to_compare
return "${return_is_version_greater_than}"
}
# }}}
main() { # {{{
define_vars
# Behaviour can be tested by overriding this variable
#zoom_current_version="66.11.4.2845"
#zoom_current_version="${zoom_new_version}"
#zoom_current_version="1.9.4.2845"
if is_version_greater_than "${zoom_new_version}" "${zoom_current_version}"; then
debug_message "Test version \
New version (${zoom_new_version}) seems more recent than the current one (${zoom_current_version})."
## If it doesn't already exists, download the package for this new version
if [ ! -f "${zoom_new_pkg_path}" ]; then
debug_message "Deb file \
Download .deb file from zoom.us to ${zoom_new_pkg_path} ."
wget --quiet https://zoom.us/client/latest/zoom_amd64.deb --output-document="${zoom_new_pkg_path}"
fi
# Verify downloaded package
# Check the version from dpkg info {{{
zoom_dpkg_version=$(dpkg --info -- "${zoom_new_pkg_path}" | awk '/ Version/ { print $2 }')
if [ "${zoom_dpkg_version}" = "${zoom_new_version}" ]; then
debug_message "Check dpkg version \
New version and .deb file informations are similar."
## Create a temp file to monitor
touch -- "${zoom_new_version_file}"
printf '\e[1;35m%-6s\e[m\n' "An upgrade is available for Zoom client (current: ${zoom_current_version}): ${zoom_new_version}." >> "${zoom_new_version_file}"
## Exit
exit 0
else
debug_message "Check dpkg version \
2021-08-23 19:02:42 +02:00
New version and .deb file informations mismatch, don't need to go further."
# }}}
# Remove useless file {{{
## Ensure to remove the file to monitor
rm --force -- "${zoom_new_version_file}"
## Keep a record of the downloaded package because as a new release might come soon
mv --force -- "${zoom_new_pkg_path}" "${zoom_tmp_pkg_path}"
## Exit
exit 0
fi
# }}}
2021-08-23 19:02:42 +02:00
else
debug_message "Test version The current version is the same or is more recent than the available one."
## Ensure to remove any temp file and useless .deb file
rm --force -- "${zoom_new_version_file}" "${zoom_new_pkg_path}" "${zoom_tmp_pkg_path}"
2021-08-23 19:02:42 +02:00
## Exit
exit 0
fi
# }}}
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to arguments
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
## If the first argument is not an option
if ! printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+";
then
## Consider it as the mode to use to get current zoom version
check_mode="${1}"
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
fi
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+"; do
case "${1}" in
-m|--mode ) ## Define check_mode
## Move to the next argument
shift
## Define var
readonly check_mode="${1}"
;;
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
-- ) ## End of options list
## End the while loop
break
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
2021-06-09 16:33:01 +02:00
# }}}
main
2021-08-23 19:02:42 +02:00
exit 255