#!/bin/sh

# This crude quick fix workaround for
#
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=728096
#
# When using systemd (jessie/sid), you will need this (while the
# bug is not fixed) to make some (i.e., those who use /run/shm
# with /dev/shm a symlink to run like wheezy does) distribution
# chroots work.
#
# Symlinking this file from /etc/schroot/setup.d should suffice
# to enable.
#
# Note that this ONLY ACTS on directory based chroots (for other
# types, you still need to fix this manually in the affaected source
# chroot).
#
# Note that this actually _changes_ the source chroot, not only
# the snasphots -- so you might want to re-create your chroots
# once the workaround is no longer needed to clean things up.

set -e

. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
. "$SETUP_DATA_DIR/common-config"

# Only in setup-start
[ "${1}" = "setup-start" ] || exit 0

printf "Workaround for: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=728096\n"

# Skip conditions
[ "${CHROOT_TYPE}" = "directory" ] || { printf "I: Not a directory chroot, skipping...\n"; exit 0; }
printf "%s" "${CHROOT_NAME}" | grep -q "^mini-buildd" || { printf "I: Not a mini-buildd chroot, skipping...\n"; exit 0; }
[ -d /run/systemd/system ] || { printf "I: Not using systemd (/dev/shm), skipping ...\n"; exit 0; }
[ "$(/bin/readlink "${CHROOT_DIRECTORY}/dev/shm")" = "/run/shm" ] || { printf "I: Chroot does not/no longer symlink /dev/shm to /run/shm, skipping ...\n"; exit 0; }

# The actual workaround: Just make /dev/shm a directory.
printf "W: Fixing SHM symlink in source chroot (systemd?)...\n" >&2
rm -v "${CHROOT_DIRECTORY}/dev/shm"
mkdir -v "${CHROOT_DIRECTORY}/dev/shm"
