#!/bin/bash -e

(
    flock -n 9

    VERSION=$(curl -s https://raw.githubusercontent.com/OneFinityCNC/onefinity-release/master/latest.txt)
    PKG_NAME=onefinity-$VERSION
    PKG=/var/lib/bbctrl/firmware/update.tar.bz2
    PKG_URL=https://raw.githubusercontent.com/OneFinityCNC/onefinity-release/master/$PKG_NAME.tar.bz2

    logger Installing onefinity 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
