Normal
  5   ˆl–äY>”Êi?u8 ƒp@¸ )‹Fÿ�þB6ãYÇ û/ŠA óÂƒqÖ�ÁÀ¿ [    #!/usr/bin/env bash
#
# Copyright the Collabora Online contributors.
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# set -x

test $# -eq 2 || { echo "Usage: $0 <chroot template directory for system libs to create> <Collabora Office installation directory>"; exit 1; }

# No provision for spaces or other weird characters in pathnames. So sue me.

# First parameter is the pathname where this script will create the "systemplate" tree
CHROOT=$1

# Second parameter is the instdir directory of the Collabora Office installation to be used
INSTDIR=$2

test -d "$INSTDIR" || { echo "$0: No such directory: $INSTDIR"; exit 1; }

mkdir -p $CHROOT || exit 1

# Resolve the real paths, in case they are relative and/or symlinked.
# INSTDIR_LOGICAL will contain the logical path, if there are symlinks,
# while INSTDIR is the physical one. Both will most likely be the same,
# except on systems that have symlinks in the path. We must create
# both paths (if they are different) inside the jail, hence we need both.
CHROOT=`cd $CHROOT && pwd`
INSTDIR_LOGICAL=`cd $INSTDIR && pwd -L`
INSTDIR=`cd $INSTDIR && pwd -P`

if [ ! `uname -s` = "FreeBSD" ]; then
    CP=cp
    REALPATH=realpath
    LOCALBASE=usr/
else
    CP=gcp
    REALPATH=grealpath
    LOCALBASE=usr/local/
fi

# Detect C library
if ldd --version 2>&1 | grep -qi musl; then
    LIBC=musl
else
    LIBC=glibc
fi

cd / || exit 1

# Refresh fontconfig's cache before copying it below, so fonts installed or
# volume-mounted since the last cache build (cool#4555) are already in it,
# instead of only becoming visible in the jail after a separate manual
# fc-cache run.
fc-cache 2>/dev/null

(
# Produce a list of file names, one per line, that will be copied
# into the template tree of system files for the chroot jails.

# First essential files and shared objects
if [ "$LIBC" != "musl" ]; then
    find etc/ld.so.* \
         lib/ld-* lib64/ld-* \
         lib/libnss_* lib64/libnss_* lib/*/libnss_* \
         lib/libresolv* lib64/libresolv* lib/*/libresolv* \
         var/cache/fontconfig \
         etc/fonts \
         -type f 2>/dev/null

    find etc/fonts \
         lib/ld-* lib64/ld-* \
         lib/libnss_* lib64/libnss_* lib/*/libnss_* \
         lib/libresolv* lib64/libresolv* lib/*/libresolv* \
        -type l 2>/dev/null
else
    # musl doesn't use libnss, libresolv, or glibc locale files
    find var/cache/fontconfig \
         etc/fonts \
         -type f 2>/dev/null

    find etc/fonts \
        -type l 2>/dev/null
fi

# Find the first of these that exist to fulfill ssltls
# via openssl requirements
find etc/pki/tls/certs/ca-bundle.crt \
     etc/pki/tls/certs/ca-bundle.trust.crt \
     etc/ssl/certs/ca-certificates.crt \
     var/lib/ca-certificates/ca-bundle.pem \
	-type l,f -print -quit 2>/dev/null

) |

# Can't use -l because then symlinks won't be handled well enough.
# This will now copy the file a symlink points to, but whatever.
cpio -p -d -L $CHROOT

# Link the dynamic files, replacing any existing.
rm -f $CHROOT/etc/copied
mkdir -p $CHROOT/etc/
ETC_FILES="hosts resolv.conf passwd group host.conf"
if [ "$LIBC" != "musl" ]; then
    ETC_FILES="$ETC_FILES nsswitch.conf"
fi
for file in $ETC_FILES
do
    # echo "Linking/Copying /etc/$file"
    # Prefer hard-linking, fallback to just copying (do *not* use symlinking because that would be relative to the jail).
    # When copying, we must make sure that we copy the source and not a symlink. Otherwise, the source won't be accessible from the jail.
    # In addition, we flag that at least one file is copied by creating the 'copied' file, so that we do check for updates.
    ln -f `${REALPATH} /etc/$file` $CHROOT/etc/$file 2> /dev/null || (${CP} --dereference --preserve=all /etc/$file $CHROOT/etc/$file && touch $CHROOT/etc/copied) || echo "$0: Failed to link or copy /etc