# Copyright 2013 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# \file sandbox-hooks.conf
# Configuration appended to the sandbox.conf(5) file generated by pkg_comp(8).

shtk_import bool
shtk_import cli


# Verifies that the configuration variables that we expect are set.
#
# \post Terminates execution if any of the key configuration variables cannot be
# accessed.
_pkg_comp_validate_variables() {
    for var in DISTDIR PACKAGES PKGSRCDIR PKG_DEVELOPER SANDBOX_ROOT; do
        local value
        eval "value=\"\$${var}\""
        [ -n "${value}" ] || shtk_cli_error "Aborting; ${var} is not set"
    done
}


# Creates the pkg_comp-specific file system hierarchy within the sandbox.
post_create_hook() {
    _pkg_comp_validate_variables

    # It is all too easy to forget to create the directories outside of the
    # sandbox, which would then cause a rather obscure "mount" failure.
    # Therefore, create them here for simplicity.  In principle this is wrong,
    # because the sandbox creation should not modify the state outside of the
    # sandbox... but this behavior is less annoying.
    mkdir -p "${DISTDIR}"
    mkdir -p "${PACKAGES}"
    mkdir -p "${PBULK_LOG}"
    mkdir -p "${PBULK_PACKAGES}"
    mkdir -p "${PKGSRCDIR}"

    mkdir "${SANDBOX_ROOT}/pkg_comp"
    mkdir "${SANDBOX_ROOT}/pkg_comp/bulklog"
    mkdir "${SANDBOX_ROOT}/pkg_comp/distfiles"
    mkdir "${SANDBOX_ROOT}/pkg_comp/packages"
    mkdir "${SANDBOX_ROOT}/pkg_comp/packages/pbulk"
    mkdir "${SANDBOX_ROOT}/pkg_comp/packages/pkg"
    mkdir "${SANDBOX_ROOT}/pkg_comp/pkgsrc"

    # Invoke the user-provided hook, which is guaranteed to exist by the
    # contents of sandbox.conf.pre.
    pkg_comp_post_create_hook
}


# Mounts the pkg_comp-specific file systems within the sandbox.
post_mount_hook() {
    _pkg_comp_validate_variables

    sandbox_bindfs -o rw "${PBULK_LOG}" "${SANDBOX_ROOT}/pkg_comp/bulklog"
    sandbox_bindfs -o rw "${DISTDIR}" "${SANDBOX_ROOT}/pkg_comp/distfiles"
    sandbox_bindfs -o rw "${PACKAGES}" "${SANDBOX_ROOT}/pkg_comp/packages/pkg"
    sandbox_bindfs -o rw "${PBULK_PACKAGES}" \
        "${SANDBOX_ROOT}/pkg_comp/packages/pbulk"

    local mode=ro
    if shtk_bool_check "${PKG_DEVELOPER}"; then
        mode=rw
    fi
    sandbox_bindfs -o "${mode}" "${PKGSRCDIR}" "${SANDBOX_ROOT}/pkg_comp/pkgsrc"

    # Invoke the user-provided hook, which is guaranteed to exist by the
    # contents of sandbox.conf.pre.
    pkg_comp_post_mount_hook
}
