Skip to content

Instantly share code, notes, and snippets.

@gnanet
Created December 13, 2019 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnanet/4f5344b8d9f3811b12631619e4b067de to your computer and use it in GitHub Desktop.
Save gnanet/4f5344b8d9f3811b12631619e4b067de to your computer and use it in GitHub Desktop.
Check if an update to viber.deb may be available, and automatically download latest viber.deb from viber.com
#!/bin/bash
#
# check_viber_deb.sh - Gergely Nagy (https://github.com/gnanet) 2019
#
# This script is intended to check if an update to viber.deb may be available,
# and automatically download latest viber.deb from viber.com
#
# You should run this script from cron
#
if [[ "x$(which curl)" == "x" ]];then echo "Please install curl using: sudo apt-get -y install curl"; exit; fi
if [[ "x$(which dpkg-deb)" == "x" ]];then echo "Please install dpkg-deb using: sudo apt-get -y install dpkg"; exit; fi
script_dir=$(dirname $0)
dl_header=$(curl --silent -I https://download.cdn.viber.com/cdn/desktop/Linux/viber.deb)
last_mod=$(echo "${dl_header}" | grep -E "^Last-Modified:" | sed -e "s/Last-Modified: //g")
lastmod_ts=$(date -d "${last_mod}" +%s)
dl_bytes=$(echo "${dl_header}" | grep -E "^Content-Length:" | grep -oE "[0-9]*")
if [ -f ${script_dir}/check_viber_deb.state ]; then
stored_bytes=$(cat ${script_dir}/check_viber_deb.state | cut -d';' -f2)
stored_ts=$(cat ${script_dir}/check_viber_deb.state | cut -d';' -f1)
stored_version=$(cat ${script_dir}/check_viber_deb.state | cut -d';' -f3)
else
echo "first run"
curl --silent https://download.cdn.viber.com/cdn/desktop/Linux/viber.deb > ${script_dir}/viber.deb
if [ -f ${script_dir}/viber.deb ]; then
if [[ "$(du -b ${script_dir}/viber.deb | grep -oE "[0-9]*")" == "${dl_bytes}" ]]; then
stored_version=$(dpkg-deb --info ${script_dir}/viber.deb | grep -E "^ Version: " | cut -d' ' -f3)
echo "${lastmod_ts};${dl_bytes};${stored_version}" > ${script_dir}/check_viber_deb.state
exit 0
else
echo "Downloaded file size differs from header"
exit 1
fi
else
echo "Download failed, missing ${script_dir}/viber.deb"
exit 127
fi
fi
if [[ ${lastmod_ts} -gt ${stored_ts} ]] && [[ ${stored_bytes} != ${dl_bytes} ]]; then
if [ -f ${script_dir}/viber.deb ]; then rm ${script_dir}/viber.deb; fi
echo "Viber Last-Modified, and download-size changed"
curl --silent https://download.cdn.viber.com/cdn/desktop/Linux/viber.deb > ${script_dir}/viber.deb
if [ -f ${script_dir}/viber.deb ]; then
if [[ "$(du -b ${script_dir}/viber.deb | grep -oE "[0-9]*")" == "${dl_bytes}" ]]; then
stored_version=$(dpkg-deb --info ${script_dir}/viber.deb | grep -E "^ Version: " | cut -d' ' -f3)
echo "${lastmod_ts};${dl_bytes};${stored_version}" > ${script_dir}/check_viber_deb.state
echo "Nev Viber version ${stored_version} available to install from ${script_dir}/viber.deb"
exit 0
else
echo "Downloaded file size differs from header"
exit 1
fi
else
echo "Download failed, missing ${script_dir}/viber.deb"
exit 127
fi
else
echo "viber still ${stored_version} with ${stored_bytes} bytes last modified: $(date -d@${stored_ts})" | logger -t $(basename $0 .sh)
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment