#!/bin/bash # # Open Rowing Monitor, https://github.com/laberning/openrowingmonitor # # Update script for Open Rowing Monitor, use at your own risk! # # treat unset variables as an error when substituting set -u # exit when a command fails set -e print() { echo "$@" } cancel() { print "$@" exit 1 } ask() { local prompt default reply if [[ ${2:-} = 'Y' ]]; then prompt='Y/n' default='Y' elif [[ ${2:-} = 'N' ]]; then prompt='y/N' default='N' else prompt='y/n' default='' fi while true; do echo -n "$1 [$prompt] " read -r reply /dev/null || true sudo git checkout -b $CURRENT_BRANCH origin/$CURRENT_BRANCH print "Updating Runtime dependencies..." sudo rm -rf node_modules sudo npm install sudo npm run build print "Starting Open Rowing Monitor..." sudo systemctl start openrowingmonitor print print "Switch to branch \"$CURRENT_BRANCH\" complete, Open Rowing Monitor now has the following exciting new features:" git log --reverse --pretty=format:"- %s" $LOCAL_VERSION..HEAD } CURRENT_DIR=$(pwd) SCRIPT_DIR="$( cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd )" INSTALL_DIR="$(dirname "$SCRIPT_DIR")" GIT_REMOTE="https://github.com/laberning/openrowingmonitor.git" cd $INSTALL_DIR CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) LOCAL_VERSION=$(git rev-parse HEAD) print "Update script for Open Rowing Monitor" print if getopts "b:" arg; then if [ $CURRENT_BRANCH = $OPTARG ]; then cancel "No need to switch to branch \"$OPTARG\", it is already the active branch" fi echo "Checking for the existence of branch \"$OPTARG\"..." if [ $(git ls-remote --heads $GIT_REMOTE 2>/dev/null|awk -F 'refs/heads/' '{print $2}'|grep -x "$OPTARG"|wc -l) = 0 ]; then cancel "Branch \"$OPTARG\" does not exist in the repository, can not switch" fi if ask "Do you want to switch from branch \"$CURRENT_BRANCH\" to branch \"$OPTARG\"?" Y; then print "Switching to branch \"$OPTARG\"..." CURRENT_BRANCH=$OPTARG switch_branch else cancel "Stopping update - please run without -b parameter to do a regular update" fi else print "Checking for new version..." REMOTE_VERSION=$(git ls-remote $GIT_REMOTE refs/heads/$CURRENT_BRANCH | awk '{print $1;}') if [ "$LOCAL_VERSION" = "$REMOTE_VERSION" ]; then print "You are using the latest version of Open Rowing Monitor from branch \"$CURRENT_BRANCH\"." else if ask "A new version of Open Rowing Monitor is available from branch \"$CURRENT_BRANCH\". Do you want to update?" Y; then update_branch fi fi fi cd $CURRENT_DIR