#!/bin/bash
# check for new shiny version and download it if necessary

#set -x
#trap read debug

rawstring=$(curl -s https://rstudio.com/products/shiny/download-server/ubuntu/ |grep "wget.*amd64.deb")
prefix="<code>$ "
suffix="</code><br />"
noprefix=${rawstring#$prefix}
nosuffix=${noprefix%$suffix}
dlcommand="$nosuffix -q -P /home/monitoring/"
cur_version=$(shiny-server --version | head -n1 | cut -d "v" -f 3)
avail_version=$(cut -d "-" -f 4 <<< "$dlcommand")

if [ "$avail_version" != "$cur_version" ]
  then
    if [ ! -e /home/monitoring/shiny-server-"${avail_version}"-amd64.deb ]
      then
        eval "$dlcommand"
    fi
    echo "Shiny version $avail_version available (in use $cur_version)"
fi