FROM localhost/base

RUN mkdir -p /usr/share/webapp /usr/share/database /usr/share/regular /usr/share/recursive

RUN <<END
    set -euxo pipefail

    mkdir -p /usr/share/layers
    mkdir -p /usr/share/layers/dir1
    mkdir -p /usr/share/layers/dir2
    mkdir -p /usr/share/layers/dir3
    mkdir -p /usr/share/layers/dir4

    # the root layers directory
    setfattr -n user.component -v "root" /usr/share/layers
    echo "foo" > /usr/share/layers/fileA
    echo "foo" > /usr/share/layers/targetA
    ln -s /usr/share/layers/targetA /usr/share/layers/linkA
    echo "foo" > /usr/share/layers/targetB
    ln -s /usr/share/layers/targetB /usr/share/layers/broken-linkB
    rm /usr/share/layers/targetB
    ln -s /usr/share/layers/dir1/targetB /usr/share/layers/linkB

    # dir1 has user.component set
    setfattr -n user.component -v "dir1" /usr/share/layers/dir1
    echo "foo" > /usr/share/layers/dir1/targetA
    ln -s /usr/share/layers/dir1/targetA /usr/share/layers/dir1/linkA
    # link to a file in dir2
    echo "foo" > /usr/share/layers/dir2/targetB
    ln -s /usr/share/layers/dir2/targetB /usr/share/layers/dir1/linkB
    # linked to from root
    echo "foo" > /usr/share/layers/dir1/targetB
    mkdir -p /usr/share/layers/dir1/dirA
    echo "foo" > /usr/share/layers/dir1/dirA/fileA

    # dir2 has no user.component set so it will be included in the root layer
    echo "foo" > /usr/share/layers/dir2/fileA
    echo "foo" > /usr/share/layers/dir2/targetA
    ln -s /usr/share/layers/dir2/targetA /usr/share/layers/dir2/linkA
    mkdir -p /usr/share/layers/dir2/dirA
    echo "foo" > /usr/share/layers/dir2/dirA/fileA

    # dir3 has no user.component set but some files in dir3 have a user.component
    echo "foo" > /usr/share/layers/dir3/fileA
    # fileB has a user.component
    echo "foo" > /usr/share/layers/dir3/fileB
    setfattr -n user.component -v "dir3fileB" /usr/share/layers/dir3/fileB

    # dir4 has a user.component set but some files have a user.component
    setfattr -n user.component -v "dir4" /usr/share/layers/dir4
    echo "foo" > /usr/share/layers/dir4/fileA
    echo "foo" > /usr/share/layers/dir4/fileB
    setfattr -n user.component -v "dir4fileB" /usr/share/layers/dir4/fileB

END
