# Build script for the filesystem layout (Dragora).
#
# Copyright (c) 2014-2018, 2022 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 are to satisfy "19-symlinks", the final
# steps are done from "recipes/data/hierarchyfs".

echo "Making minimal directory layout on 'rootdir' ..."

mkdir -p -- \
 "${rootdir}/etc"      \
 "${rootdir}/root"     \
 "${rootdir}/tmp"      \
 "${rootdir}/usr/bin"  \
 "${rootdir}/var/log"
(
    cd -- "${rootdir}"
    chmod 0750 root; chmod 1777 tmp
    if test ! -L bin
    then
        rm -f bin
        ln -s -f usr/bin bin
    fi
)

# 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}
        if test ! -L lib
        then
            rm -f lib
            ln -s -f lib${libSuffix} lib
        fi
    )

    # Make symlink of compatibility for / at $rootdir
    cd -- "$rootdir"

    echo "Handling lib<equal> for ${rootdir}/lib${libSuffix} ..."
    if test ! -L "lib${libSuffix}"
    then
        rm -f lib${libSuffix}
        ln -s -f usr/lib${libSuffix} lib${libSuffix}
    fi

    echo "Handling lib<equal> for ${rootdir}/lib ..."
    if test ! -L lib
    then
        rm -f lib
        ln -s -f lib${libSuffix} lib
    fi

    echo "Making compatibility for ${rootdir}/tools/lib${libSuffix} ..."
    cd tools
    mkdir -p lib${libSuffix}
    if test ! -L lib
    then
        rm -f lib
        ln -s -f lib${libSuffix} lib
    fi
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

