#!/bin/bash -e
# Copyright (c) 2009 Alon Swartz <alon@turnkeylinux.org> - all rights reserved

fatal() {
    echo "fatal: $@" 1>&2
    exit 1
}

usage() {
cat<<EOF
Syntax: $(basename $0) rootfs-dir overlay-dir
Applies the files from overlay-dir on top of rootfs-dir.
Any file in overlay-dir that is also in rootfs-dir will be overwritten.

Environment variables:

    TKLPATCH_DEBUG       Turn on debugging.

Example overlay structure:

    # overwrite message of the day
    patch-dir/overlay/etc/issue 

EOF
    exit 1
}

if [[ "$#" != "2" ]]; then
    usage
fi

[ -n "$TKLPATCH_DEBUG" ] && set -x

rootfs=$1
overlay=$2

[ -d $rootfs ] || fatal "no such directory: $rootfs"
[ -d $overlay ] || fatal "no such directory: $overlay"

echo "# applying overlay $overlay"
cp -TdR $overlay $rootfs
