<commit_before>#!/bin/sh

set -e

DIR=/target

if [ -e "$DIR" ]; then
    if [ -d "$DIR" ]; then
        FILES=$(ls -1qAH -- "$DIR")
        if ! [ -z "$FILES" ]; then
        echo "$DIR is not an empty directory!"
        exit 1
        fi
    else
        echo "$DIR exists and is not a directory!"
    fi
fi

if ! [ -d "/home/belenios" ]; then
    echo "This script must be run from a root created with belenios-stage1.sh!"
    exit 1
fi

mkdir -p "$DIR"

cd /

echo "Debootstrapping..."

debootstrap --merged-usr --variant=minbase --include=systemd,dbus buster "$DIR/rootfs"
echo 'APT::Install-Recommends "false";' >> "$DIR/rootfs/etc/apt/apt.conf"

ln -sfT /usr/lib/systemd/resolv.conf "$DIR/rootfs/etc/resolv.conf"
echo belenios > "$DIR/rootfs/etc/hostname"

cat > "$DIR/rootfs/etc/hosts" <<EOF
127.0.0.1 localhost
127.0.1.1 belenios

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b ip6-allnodes
fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b ip6-allrouters
EOF

echo "Installing Debian prerequisites..."

chroot "$DIR/rootfs" sh -c "apt-get update -qq && apt-get upgrade -qq"
chroot "$DIR/rootfs" sh -c "debconf-set-selections" <<EOF
dma dma/mailname string belenios
dma dma/relayhost string localhost
EOF
chroot "$DIR/rootfs" sh -c "apt-get install -qq libgmp-dev libpcre3-dev libssl-dev libsqlite3-dev ca-certificates zip libncurses-dev zlib1g-dev libgd-securityimage-perl cracklib-runtime dma logrotate"
chroot "$DIR/rootfs" sh -c "apt-get clean"
chroot "$DIR/rootfs" useradd belenios

echo "Copying needed runtime files from opam root..."

for u in \
    bin/ocsigenserver.opt \
    lib/astring \
    lib/atdgen \
    lib/atdgen-runtime \
    lib/bigarray \
    lib/biniou \
    lib/bytes \
    lib/calendar \
    lib/camomile \
    lib/cryptokit \
    lib/csv \
    lib/domain-name \
    lib/dynlink \
    lib/easy-format \
    lib/eliom \
    lib/fileutils \
    lib/findlib \
    lib/findlib.conf \
    lib/fmt \
    lib/gettext \
    lib/gettext-camomile \
    lib/ipaddr \
    lib/js_of_ocaml \
    lib/lwt \
    lib/lwt_log \
    lib/lwt_react \
    lib/lwt_ssl \
    lib/macaddr \
    lib/mmap \
    lib/netstring \
    lib/netstring-pcre \
    lib/netsys \
    lib/ocplib-endian \
    lib/ocsigenserver \
    lib/pcre \
    lib/ppx_deriving \
    lib/re \
    lib/react \
    lib/reactiveData \
    lib/result \
    lib/seq \
    lib/sqlite3 \
    lib/ssl \
    lib/stdlib-shims \
    lib/str \
    lib/threads \
    lib/tyxml \
    lib/uchar \
    lib/unix \
    lib/uutf \
    lib/xml-light \
    lib/yojson \
    lib/zarith \
; do cp -a --parents home/belenios/.belenios/opam/4.11.1/$u "$DIR/rootfs"; done

echo "Copying needed runtime files from belenios source tree..."

cp -a --parents home/belenios/belenios/_run  home/belenios/opam-env.sh "$DIR/rootfs"

echo "Creating remaining runtime files and directories..."

cat > "$DIR/rootfs/etc/logrotate.d/belenios" <<EOF
/var/belenios/log/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 belenios belenios
        sharedscripts
        postrotate
                echo reopen_logs > /tmp/belenios/ocsigenserver_command
        endscript
}
EOF

cat > "$DIR/rootfs/home/belenios/belenios-env.sh" <<EOF
. /home/belenios/opam-env.sh<commit_msg>OCaml has been updated to 4.11.2<commit_after>#!/bin/sh

set -e

DIR=/target

if [ -e "$DIR" ]; then
    if [ -d "$DIR" ]; then
        FILES=$(ls -1qAH -- "$DIR")
        if ! [ -z "$FILES" ]; then
        echo "$DIR is not an empty directory!"
        exit 1
        fi
    else
        echo "$DIR exists and is not a directory!"
    fi
fi

if ! [ -d "/home/belenios" ]; then
    echo "This script must be run from a root created with belenios-stage1.sh!"
    exit 1
fi

mkdir -p "$DIR"

cd /

echo "Debootstrapping..."

debootstrap --merged-usr --variant=minbase --include=systemd,dbus buster "$DIR/rootfs"
echo 'APT::Install-Recommends "false";' >> "$DIR/rootfs/etc/apt/apt.conf"

ln -sfT /usr/lib/systemd/resolv.conf "$DIR/rootfs/etc/resolv.conf"
echo belenios > "$DIR/rootfs/etc/hostname"

cat > "$DIR/rootfs/etc/hosts" <<EOF
127.0.0.1 localhost
127.0.1.1 belenios

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b ip6-allnodes
fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b ip6-allrouters
EOF

echo "Installing Debian prerequisites..."

chroot "$DIR/rootfs" sh -c "apt-get update -qq && apt-get upgrade -qq"
chroot "$DIR/rootfs" sh -c "debconf-set-selections" <<EOF
dma dma/mailname string belenios
dma dma/relayhost string localhost
EOF
chroot "$DIR/rootfs" sh -c "apt-get install -qq libgmp-dev libpcre3-dev libssl-dev libsqlite3-dev ca-certificates zip libncurses-dev zlib1g-dev libgd-securityimage-perl cracklib-runtime dma logrotate"
chroot "$DIR/rootfs" sh -c "apt-get clean"
chroot "$DIR/rootfs" useradd belenios

echo "Copying needed runtime files from opam root..."

for u in \
    bin/ocsigenserver.opt \
    lib/astring \
    lib/atdgen \
    lib/atdgen-runtime \
    lib/bigarray \
    lib/biniou \
    lib/bytes \
    lib/calendar \
    lib/camomile \
    lib/cryptokit \
    lib/csv \
    lib/domain-name \
    lib/dynlink \
    lib/easy-format \
    lib/eliom \
    lib/fileutils \
    lib/findlib \
    lib/findlib.conf \
    lib/fmt \
    lib/gettext \
    lib/gettext-camomile \
    lib/ipaddr \
    lib/js_of_ocaml \
    lib/lwt \
    lib/lwt_log \
    lib/lwt_react \
    lib/lwt_ssl \
    lib/macaddr \
    lib/mmap \
    lib/netstring \
    lib/netstring-pcre \
    lib/netsys \
    lib/ocplib-endian \
    lib/ocsigenserver \
    lib/pcre \
    lib/ppx_deriving \
    lib/re \
    lib/react \
    lib/reactiveData \
    lib/result \
    lib/seq \
    lib/sqlite3 \
    lib/ssl \
    lib/stdlib-shims \
    lib/str \
    lib/threads \
    lib/tyxml \
    lib/uchar \
    lib/unix \
    lib/uutf \
    lib/xml-light \
    lib/yojson \
    lib/zarith \
; do cp -a --parents home/belenios/.belenios/opam/4.11.2/$u "$DIR/rootfs"; done

echo "Copying needed runtime files from belenios source tree..."

cp -a --parents home/belenios/belenios/_run  home/belenios/opam-env.sh "$DIR/rootfs"

echo "Creating remaining runtime files and directories..."

cat > "$DIR/rootfs/etc/logrotate.d/belenios" <<EOF
/var/belenios/log/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 belenios belenios
        sharedscripts
        postrotate
                echo reopen_logs > /tmp/belenios/ocsigenserver_command
        endscript
}
EOF

cat > "$DIR/rootfs/home/belenios/belenios-env.sh" <<EOF
. /home/belenios/opam-env.sh
{'fraction_non_alphanumeric': 0.12663819761448977, 'fraction_numerical': 0.022382565159770284, 'mean_word_length': 2.2875121006776378, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 4, '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': '943807', 'n_tokens_mistral': 2676, 'n_tokens_neox': 2564, 'n_words': 572}