#!/usr/bin/env sh

set -e

# Don't allow spaces in arguments intentionally
case "$1" in
    *\ *)
        echo "Spaces aren't allowed in arguments: '$1'" > /dev/stderr
        exit 0;;
esac

OVERLAY=$1

if [ -z $OVERLAY ]; then
    echo "Usage: $0 <dtb-overlay-file>"
    exit 0
fi

if [ ! -r $OVERLAY ]; then
    echo "Failed to read '$OVERLAY'" > /dev/stderr
    exit 1
fi

NAME=`basename $OVERLAY .dtb`
PREFIX=/sys/kernel/config/device-tree/overlays/$NAME
modprobe dtbocfg

# Don't use '-p' here: want to show user if overlay is already applied.
mkdir $PREFIX
cat $OVERLAY > $PREFIX/dtbo
echo 1 > $PREFIX/status
