Improve Zoom script

Manage arguments.
Better versions comparison.
This commit is contained in:
Jeremy Gardais 2022-06-02 13:56:34 +02:00
parent 93d8a5a395
commit 65767cfffd
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 280 additions and 64 deletions

View File

@ -5,11 +5,12 @@
## 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.
## 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 repo
#00 20 * * * root /opt/repos/ipr.scripts/app/check.zoom.update --mode repo
### 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.
@ -19,75 +20,223 @@
## Inspired by https://github.com/pazepaze/zoom-autoupdater/blob/master/autoupdate-zoom.sh
# }}}
# Expect maximum 1 argument {{{
if [ $# -gt 1 ]
then
# 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() { # {{{
cat << HELP
check.zoom.update --
usage: $PROGNAME [check_mode] [-m|-d|-h]
Compare current version of an installed Zoom client and the last available.
EXAMPLE:
- Compare the current version installed from .deb file
check.zoom.update
EXAMPLES:
- Compare the current version of Zoom installed from a .deb file
${PROGNAME}
${PROGNAME} --mode file
- Compare the current version from apt's repository
check.zoom.update repo
- Compare the current version of Zoom available in the APT repo
${PROGNAME} repo
${PROGNAME} --mode repo
OPTIONS:
-m,--mode
Set the check_mode to use to get current version of Zoom client
Available mode :
* repo
* file (default behaviour)
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
HELP
exit 1
fi
}
# }}}
debug_message() { # {{{
# Vars {{{
DEBUG=1
local_debug_message="${1}"
if [ $# -eq 1 ] && [ "${1}" = "repo" ] ## If repository's version should be compared
then
## 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
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## 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))
### Shift to the next variable
shift
done
unset local_total_var_empty
unset loop_count_var_empty
return "${return_var_empty}"
}
# }}}
define_vars() { # {{{
## 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}"
## }}}
## 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/')
else
;;
"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
## 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
## }}}
## 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
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 "${zoom_new_version}" | sed -e 's/\(.*\) .*/\1/')
zoom_new_version_minor=$(echo "${zoom_new_version}" | sed -e 's/.* (\(.*\))/\1/')
unset first_value
unset value_to_compare
zoom_new_version_file="/tmp/.zoom.upgrade"
zoom_new_pkg_path="/tmp/zoom_${zoom_new_version_major}.${zoom_new_version_minor}_amd64.deb"
zoom_tmp_pkg_path="/tmp/.zoom_${zoom_new_version_major}.${zoom_new_version_minor}_amd64.deb"
return "${return_is_version_greater_than}"
}
# }}}
# Check if the current version is the last one {{{
if [ "${zoom_current_version}" != "${zoom_new_version_major}.${zoom_new_version_minor}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — Current version (${zoom_current_version}) and new one (${zoom_new_version_major}.${zoom_new_version_minor}) seems to be different."
main() { # {{{
## If it doesn't already exists, download this new package
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}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Download .deb file from zoom.us to ${zoom_new_pkg_path} ."
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 {{{
# 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_major}.${zoom_new_version_minor}" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Check dpkg version — \
New version and .deb file informations are correct."
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_major}.${zoom_new_version_minor}." >> "${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}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Check dpkg version — \
debug_message "Check dpkg version \
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}"
@ -100,10 +249,8 @@ New version and .deb file informations mismatch, don't need to go further."
# }}}
# }}}
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test version — The current version is up-to-date."
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}"
@ -111,5 +258,74 @@ else
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
# }}}
main
exit 255