#!/bin/bash
# Part of the xs-sync package
# Author: Martin Langhoff <martin@laptop.org>
# Copyright: One Laptop per Child

set -e

VERBOSE=no

# Log a string via the syslog facility.
log()
{
    if test $1 != debug || expr "$VERBOSE" : "[yY]" > /dev/null; then
	logger -p user.$1 -t "xs-rsync[$$]" -- "$2"
    fi
}
STEPS=5

if [ -e $UM_MOUNTPOINT/xs-xobuilds -a \
     -e $UM_MOUNTPOINT/xs-xobuilds/manifest.sha1 ];then
    log notice 'Found xobuilds to install!';
    log notice "[1/$STEPS] Checking whether it has a manifest";

    if [ -e $UM_MOUNTPOINT/xs-xobuilds/manifest.sha1 ];then
	log notice "[2/$STEPS] Seems to have a manifest";
    else
	log err "[2/$STEPS] Missing manifest"
	exit 1;
    fi

    ## Do we have enough space?
    # note: we could use awk {'print $4'} instead of the
    # perl regex, but it breaks with long /dev nodes
    # such as those from LVMs -which wrap. The regex captures the
    # number just left of the number with the percentage sign.
    NEED=`du -s -B1M $UM_MOUNTPOINT/xs-xobuilds | awk {'print $1'}`
    HAVE=`df -B1M /library/xs-rsync | tail -n1 | \
	  perl -pe 'm/(\d+)\s+\d+\%/; $_=$1;'`
    if [ $NEED -gt $HAVE ];then
	log err 'Not enough free space in /library for this xo image - cancelling';
	exit 1;
    fi

    ### Copy it first - as the media is bound to be slow
    # - make this atomic by cp'ing to a tmpdir, and mv'ing into place
    #   to be fail-safe
    # - mv aside manifest.sha1 and its sig
    # - TODO? we could avoid cp'ing files we already have using
    #   rsync --copy-dest instead of cp
    #
    log notice "[3/$STEPS] Copying xo-builds to xobuilds-packed";
    TMPDEST=`mktemp -d -p /library/xs-rsync/tmp`
    cp $UM_MOUNTPOINT/xs-xobuilds/* $TMPDEST

    # In a tmpdir we own, safe from race conditions
    # run the checksums...
    log notice "[4/$STEPS] Checking the manifest";
    # mv the manifest to a different dir
    TMPMANIF=`mktemp -d -p /library/xs-rsync/tmp`
    mv $TMPDEST/manifest.sha1 $TMPMANIF/
    if [ -e $TMPDEST/manifest.sha1.sig ]; then
	mv $TMPDEST/manifest.sha1.sig $TMPMANIF/
    fi
    xs-sum -c $TMPMANIF/manifest.sha1 -d $TMPDEST

    mv $TMPDEST/* /library/xs-rsync/xobuilds-packed/

    log notice "[5/$STEPS] Refreshing XO builds available";
    (/usr/bin/xs-refresh-xobuilds 2>&1 ) | logger -p user.debug -t "xs-rsync[$$]"
    log notice "[6/$STEPS] Finished - XOs can now update.";

    rm -fr "$TMPDEST" "$TMPMANIF"

fi # end if we have xs-xobuilds/manifest.sha1