#!/bin/bash -e

(
    flock -n 9

    VERSION=$(curl -s https://buildbotics.com/bbctrl/latest.txt)
    PKG_NAME=bbctrl-$VERSION
    PKG=/var/lib/bbctrl/firmware/update.tar.bz2
    PKG_URL=https://buildbotics.com/bbctrl/$PKG_NAME.tar.bz2

    logger Installing bbctrl firmware $VERSION

    cd /var/lib/bbctrl
    mkdir -p firmware

    echo Downloading $PKG_URL
    curl -s $PKG_URL > $PKG

    /usr/local/bin/update-bbctrl

    echo Success

    logger bbctrl firmware $VERSION installed

) 9> /var/lock/bbctrl.upgrade.lock
