# Build script for the filesystem layout (Dragora).
#
# Copyright (c) 2014-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# These steps here are to satisfy "13-symlinks", the complete
# steps are done from "recipes/config/hierarchyfs".

echo "Creating minimal structure of directories for 'rootdir' ..."

mkdir -p -- \
 "${rootdir}/tmp" "${rootdir}/usr/bin" "${rootdir}/etc" "${rootdir}/var/log"
(
    cd -- "${rootdir}"
    chmod 1777 tmp
    ln -sf usr/bin bin
)

# Handle lib<equal> directory for the multi directory support
if test -n "$libSuffix"
then
    echo "Handling lib<equal> for ${rootdir}/usr/lib${libSuffix} ..."
    (
        cd -- "${rootdir}"/usr
        mkdir -p lib${libSuffix}
        ln -s -f lib${libSuffix} lib
    )

    # Make symlink of compatibility for / at $rootdir
    cd -- "$rootdir"
    rm -f lib
    ln -s usr/lib${libSuffix} lib

    echo "Making compatibility for ${rootdir}/tools/lib${libSuffix} ..."
    cd tools
    mkdir -p lib${libSuffix}
    ln -s -f lib${libSuffix} lib
else
    mkdir -p -- "${rootdir}/lib" "${rootdir}/tools/lib" "${rootdir}/usr/lib"
fi

# Save the value of 'libSuffix'

if test -n "$libSuffix"
then
    mkdir -p /tools/etc
    echo "$libSuffix" > /tools/etc/stage1_is-multi-directory
fi

