<commit_before># vim: set ts=4 sw=4 et:

chroot_init() {
    XBPSSRC_CF=$XBPS_MASTERDIR/etc/xbps/xbps-src.conf

    cat > $XBPSSRC_CF <<_EOF
# Generated configuration file by xbps-src, DO NOT EDIT!
XBPS_MASTERDIR=/
XBPS_CFLAGS="$XBPS_CFLAGS"
XBPS_CXXFLAGS="$XBPS_CXXFLAGS"
XBPS_CPPFLAGS="$XBPS_CPPFLAGS"
XBPS_LDFLAGS="$XBPS_LDFLAGS"
_EOF
    if [ -n "$XBPS_MAKEJOBS" ]; then
        echo "XBPS_MAKEJOBS=$XBPS_MAKEJOBS" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_HOSTDIR" ]; then
        echo "XBPS_HOSTDIR=/host" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_CCACHE" ]; then
        echo "XBPS_CCACHE=$XBPS_CCACHE" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_DISTCC" ]; then
        echo "XBPS_DISTCC=$XBPS_DISTCC" >> $XBPSSRC_CF
        echo "XBPS_DISTCC_HOSTS=\"${XBPS_DISTCC_HOSTS}\"" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_USE_GIT_REVS" ]; then
        echo "XBPS_USE_GIT_REVS=yes" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_DEBUG_PKGS" ]; then
        echo "XBPS_DEBUG_PKGS=yes" >> $XBPSSRC_CF
    fi

    echo "# End of configuration file." >> $XBPSSRC_CF

    # Create custom script to start the chroot bash shell.
    cat > $XBPS_MASTERDIR/bin/xbps-shell <<_EOF
#!/bin/sh

XBPS_SRC_VERSION="$XBPS_SRC_VERSION"

. /etc/xbps/xbps-src.conf

PATH=/xbps-packages:/usr/bin:/usr/sbin

exec env -i PATH="\$PATH" DISTCC_HOSTS="\$XBPS_DISTCC_HOSTS" DISTCC_DIR="/distcc" @@XARCH@@ \
    CCACHE_DIR="/ccache" IN_CHROOT=1 LANG=en_US.UTF-8 TERM=linux HOME="/tmp" \
    PS1="[\u@$XBPS_MASTERDIR \W]$ " /bin/bash +h
_EOF
    if [ -n "$XBPS_ARCH" ]; then
        sed -e "s,@@XARCH@@,XBPS_ARCH=${XBPS_ARCH},g" -i $XBPS_MASTERDIR/bin/xbps-shell
    else
        sed -e 's,@@XARCH@@,,g' -i $XBPS_MASTERDIR/bin/xbps-shell
    fi
    chmod 755 $XBPS_MASTERDIR/bin/xbps-shell

    cp -f /etc/resolv.conf $XBPS_MASTERDIR/etc

    # Update xbps alternative repository if set.
    mkdir -p $XBPS_MASTERDIR/etc/xbps/repo.d
    if [ -n "$XBPS_ALT_REPOSITORY" ]; then
        ( \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}"; \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}/nonfree"; \
            ) > $XBPS_MASTERDIR/etc/xbps/repo.d/00-alternative.conf
    else
        rm -f $XBPS_MASTERDIR/etc/xbps/repo.d/00-alternative.conf
    fi
}

chroot_prepare() {
    local f=

    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init ]; then
        return 0
    elif [ ! -f $XBPS_MASTERDIR/bin/bash ]; then
        msg_error "Bootstrap not installed in $XBPS_MASTERDIR, can't continue.\n"
    fi

    # Create some required files.
    cp -f /etc/services $XBPS_MASTERDIR/etc
    [ -f /etc/localtime ] && cp -f /etc/localtime $XBPS_MASTERDIR/etc

    for f in dev sys proc host boot; do
        [ ! -d $XBPS_MASTERDIR/$f ] && mkdir -p $XBPS_MASTERDIR/$f
    done

    # Copy /etc/passwd and /etc/group from base-files.
    cp -f $XBPS_SRCPKGDIR/base-files/files/passwd $XBPS_MASTERDIR/etc
    echo "$(whoami):x:$(id -u):$(id -g):$(whoami) user:/tmp:/bin/xbps-shell" \
        >> $XBPS_MASTERDIR/etc/passwd
    cp -f $XBPS_SRCPKGDIR/base-files/files/group $XBPS_MASTERDIR/etc
    echo "$(whoami):x:$(id -g):" >> $XBPS_MASTERDIR/etc/group

    # Copy /etc/hosts from base-files.
    cp -f $XBPS_SRCPKGDIR/base-files/files/hosts $XBPS_MASTERDIR/etc

    echo "syslog=false" >> $XBPS_MASTERDIR/etc/xbps/xbps.conf
    rm -f $XBPS_MASTERDIR/usr/share/xbps/repo.d/*.conf

    # Prepare default locale: en_US.UTF-8.
    if [ -s ${XBPS_MASTERDIR}/etc/default/libc-locales ]; then
        echo 'en_US.UTF-8 UTF-8' >> ${XBPS_MASTERDIR}/etc/default/libc-locales
        $XBPS_RECONFIGURE_CMD -f glibc-locales
    fi

    touch -f $XBPS_MASTERDIR/.xbps_chroot_init
    [ -n "$1" ] && echo $1 >> $XBPS_MASTERDIR/.xbps_chroot_init

    return 0
}

chroot_sync_repos() {
    local f=

    # Copy xbps configuration files to the masterdir.
    if [ ! -f ${XBPS_MASTERDIR}/etc/xbps/xbps.conf ]; then
        install -Dm644 ${XBPS_COMMONDIR}/xbps-src/chroot/xbps.conf \
            ${XBPS_MASTERDIR}/etc/xbps/xbps.conf
    fi
    install -Dm644 ${XBPS_COMMONDIR}/xbps-src/chroot/repos-local.conf \
        ${XBPS_MASTERDIR}/etc/xbps/repo.d/10-local.conf
    install -Dm644 ${XBPS_COMMONDIR}/xbps-src/chroot/repos-remote.conf \
        ${XBPS_MASTERDIR}/etc/xbps/repo.d/20-remote.conf

    # if -N is set, comment out remote repositories from xbps.conf.
    if [ -n "$XBPS_SKIP_REMOTEREPOS" ]; then
        rm -f ${XBPS_MASTERDIR}/etc/xbps/repo.d/20-remote.conf
    else
        # Make sure to sync index for remote repositories.
        $CHROOT_CMD $XBPS_MASTERDIR /usr/sbin/xbps-install -S
        if [ -n "$XBPS_CROSS_BUILD" ]; then
            # Copy host keys to the target rootdir.
            if [ ! -d $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/var/db/xbps/keys ]; then
                mkdir -p $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/var/db/xbps/keys
            fi
            cp -a $XBPS_MASTERDIR/var/db/xbps/keys/*.plist \
                $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/var/db/xbps/keys
            rm -rf $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/etc/xbps/repo.d
            mkdir -p $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/etc/xbps/repo.d
            cp ${XBPS_MASTERDIR}/etc/xbps/repo.d/*.conf \
                $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/etc/xbps/repo.d
            env XBPS_TARGET_ARCH=$XBPS_TARGET_ARCH \
                $CHROOT_CMD $XBPS_MASTERDIR /usr/sbin/xbps-install \
                -r /usr/$XBPS_CROSS_TRIPLET -S
        fi
    fi

    return 0
}

chroot_handler() {
    local action="$1" pkg="$2" rv=0 arg= _envargs= _chargs=

    # Debian uses /run/shm instead...
    if [ -d /run/shm ]; then
        mkdir -p ${XBPS_MASTERDIR}/run/shm
        _chargs+=" -S /run/shm"
    elif [ -d /dev/shm ]; then
        mkdir -p ${XBPS_MASTERDIR}/dev/shm
        _chargs+=" -S /dev/shm"
    fi

    if [ -n "$XBPS_HOSTDIR" ]; then<commit_msg>xbps-src: fix previous correctly for local mode too.<commit_after># vim: set ts=4 sw=4 et:

chroot_init() {
    XBPSSRC_CF=$XBPS_MASTERDIR/etc/xbps/xbps-src.conf

    cat > $XBPSSRC_CF <<_EOF
# Generated configuration file by xbps-src, DO NOT EDIT!
XBPS_MASTERDIR=/
XBPS_CFLAGS="$XBPS_CFLAGS"
XBPS_CXXFLAGS="$XBPS_CXXFLAGS"
XBPS_CPPFLAGS="$XBPS_CPPFLAGS"
XBPS_LDFLAGS="$XBPS_LDFLAGS"
_EOF
    if [ -n "$XBPS_MAKEJOBS" ]; then
        echo "XBPS_MAKEJOBS=$XBPS_MAKEJOBS" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_HOSTDIR" ]; then
        echo "XBPS_HOSTDIR=/host" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_CCACHE" ]; then
        echo "XBPS_CCACHE=$XBPS_CCACHE" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_DISTCC" ]; then
        echo "XBPS_DISTCC=$XBPS_DISTCC" >> $XBPSSRC_CF
        echo "XBPS_DISTCC_HOSTS=\"${XBPS_DISTCC_HOSTS}\"" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_USE_GIT_REVS" ]; then
        echo "XBPS_USE_GIT_REVS=yes" >> $XBPSSRC_CF
    fi
    if [ -n "$XBPS_DEBUG_PKGS" ]; then
        echo "XBPS_DEBUG_PKGS=yes" >> $XBPSSRC_CF
    fi

    echo "# End of configuration file." >> $XBPSSRC_CF

    # Create custom script to start the chroot bash shell.
    cat > $XBPS_MASTERDIR/bin/xbps-shell <<_EOF
#!/bin/sh

XBPS_SRC_VERSION="$XBPS_SRC_VERSION"

. /etc/xbps/xbps-src.conf

PATH=/xbps-packages:/usr/bin:/usr/sbin

exec env -i PATH="\$PATH" DISTCC_HOSTS="\$XBPS_DISTCC_HOSTS" DISTCC_DIR="/distcc" @@XARCH@@ \
    CCACHE_DIR="/ccache" IN_CHROOT=1 LANG=en_US.UTF-8 TERM=linux HOME="/tmp" \
    PS1="[\u@$XBPS_MASTERDIR \W]$ " /bin/bash +h
_EOF
    if [ -n "$XBPS_ARCH" ]; then
        sed -e "s,@@XARCH@@,XBPS_ARCH=${XBPS_ARCH},g" -i $XBPS_MASTERDIR/bin/xbps-shell
    else
        sed -e 's,@@XARCH@@,,g' -i $XBPS_MASTERDIR/bin/xbps-shell
    fi
    chmod 755 $XBPS_MASTERDIR/bin/xbps-shell

    cp -f /etc/resolv.conf $XBPS_MASTERDIR/etc

    # Update xbps alternative repository if set.
    mkdir -p $XBPS_MASTERDIR/etc/xbps/repo.d
    if [ -n "$XBPS_ALT_REPOSITORY" ]; then
        ( \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}"; \
            echo "repository=/host/binpkgs/${XBPS_ALT_REPOSITORY}/nonfree"; \
            ) > $XBPS_MASTERDIR/etc/xbps/repo.d/00-alternative.conf
    else
        rm -f $XBPS_MASTERDIR/etc/xbps/repo.d/00-alternative.conf
    fi
}

chroot_prepare() {
    local f=

    if [ -f $XBPS_MASTERDIR/.xbps_chroot_init ]; then
        return 0
    elif [ ! -f $XBPS_MASTERDIR/bin/bash ]; then
        msg_error "Bootstrap not installed in $XBPS_MASTERDIR, can't continue.\n"
    fi

    # Create some required files.
    cp -f /etc/services $XBPS_MASTERDIR/etc
    [ -f /etc/localtime ] && cp -f /etc/localtime $XBPS_MASTERDIR/etc

    for f in dev sys proc host boot; do
        [ ! -d $XBPS_MASTERDIR/$f ] && mkdir -p $XBPS_MASTERDIR/$f
    done

    # Copy /etc/passwd and /etc/group from base-files.
    cp -f $XBPS_SRCPKGDIR/base-files/files/passwd $XBPS_MASTERDIR/etc
    echo "$(whoami):x:$(id -u):$(id -g):$(whoami) user:/tmp:/bin/xbps-shell" \
        >> $XBPS_MASTERDIR/etc/passwd
    cp -f $XBPS_SRCPKGDIR/base-files/files/group $XBPS_MASTERDIR/etc
    echo "$(whoami):x:$(id -g):" >> $XBPS_MASTERDIR/etc/group

    # Copy /etc/hosts from base-files.
    cp -f $XBPS_SRCPKGDIR/base-files/files/hosts $XBPS_MASTERDIR/etc

    echo "syslog=false" >> $XBPS_MASTERDIR/etc/xbps/xbps.conf
    rm -f $XBPS_MASTERDIR/usr/share/xbps/repo.d/*.conf

    # Prepare default locale: en_US.UTF-8.
    if [ -s ${XBPS_MASTERDIR}/etc/default/libc-locales ]; then
        echo 'en_US.UTF-8 UTF-8' >> ${XBPS_MASTERDIR}/etc/default/libc-locales
        $XBPS_RECONFIGURE_CMD -f glibc-locales
    fi

    touch -f $XBPS_MASTERDIR/.xbps_chroot_init
    [ -n "$1" ] && echo $1 >> $XBPS_MASTERDIR/.xbps_chroot_init

    return 0
}

chroot_sync_repos() {
    local f=

    # Copy xbps configuration files to the masterdir.
    if [ ! -f ${XBPS_MASTERDIR}/etc/xbps/xbps.conf ]; then
        install -Dm644 ${XBPS_COMMONDIR}/xbps-src/chroot/xbps.conf \
            ${XBPS_MASTERDIR}/etc/xbps/xbps.conf
    fi
    install -Dm644 ${XBPS_COMMONDIR}/xbps-src/chroot/repos-local.conf \
        ${XBPS_MASTERDIR}/etc/xbps/repo.d/10-local.conf
    install -Dm644 ${XBPS_COMMONDIR}/xbps-src/chroot/repos-remote.conf \
        ${XBPS_MASTERDIR}/etc/xbps/repo.d/20-remote.conf

    # if -N is set, comment out remote repositories from xbps.conf.
    if [ -n "$XBPS_SKIP_REMOTEREPOS" ]; then
        rm -f ${XBPS_MASTERDIR}/etc/xbps/repo.d/20-remote.conf
    else
        # Make sure to sync index for remote repositories.
        $CHROOT_CMD $XBPS_MASTERDIR /usr/sbin/xbps-install -S
        if [ -n "$XBPS_CROSS_BUILD" ]; then
            # Copy host keys to the target rootdir.
            if [ ! -d $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/var/db/xbps/keys ]; then
                mkdir -p $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/var/db/xbps/keys
            fi
            cp -a $XBPS_MASTERDIR/var/db/xbps/keys/*.plist \
                $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/var/db/xbps/keys
            env XBPS_TARGET_ARCH=$XBPS_TARGET_ARCH \
                $CHROOT_CMD $XBPS_MASTERDIR /usr/sbin/xbps-install \
                -r /usr/$XBPS_CROSS_TRIPLET -S
        fi
    fi

    # Copy host repos to the cross root.
    if [ -n "$XBPS_CROSS_BUILD" ]; then
        rm -rf $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/etc/xbps/repo.d
        mkdir -p $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/etc/xbps/repo.d
        cp ${XBPS_MASTERDIR}/etc/xbps/repo.d/*.conf \
            $XBPS_MASTERDIR/usr/$XBPS_CROSS_TRIPLET/etc/xbps/repo.d
    fi

    return 0
}

chroot_handler() {
    local action="$1" pkg="$2" rv=0 arg= _envargs= _chargs=

    # Debian uses /run/shm instead...
    if [ -d /run/shm ]; then
        mkdir -p ${XBPS_MASTERDIR}/run/shm
        _chargs+=" -S /run/shm"
    elif [ -d /dev/shm ]; then
        mkdir -p ${XBPS_MASTERDIR}/dev/shm
        _chargs+=" -S /dev/shm"
    fi

    if [ -n "$XBPS_HOSTDIR" ]; then
{'fraction_non_alphanumeric': 0.14138164578394852, 'fraction_numerical': 0.006434134778191669, 'mean_word_length': 2.590577507598784, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2470088', 'n_tokens_mistral': 5188, 'n_tokens_neox': 4963, 'n_words': 846}