#!/bin/sh
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: oneshot; invoke with: sv once mountdevsubfs.sh

exec 2>&1

ebegin() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
eend() { r=${1:-0}; shift || true; [ "$r" -eq 0 ] || [ "${VERBOSE:-}" = no ] || echo "$*" >&2; return "$r"; }
einfo() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
ewarn() { echo "$@" >&2; }
eerror() { echo "$@" >&2; }

/usr/lib/runit-services/stage1/sv once mountkernfs.sh || exit 1

TTYGRP=5
TTYMODE=620
[ -f /etc/default/devpts ] && . /etc/default/devpts
KERNEL="$(uname -s)"
. /lib/init/vars.sh
. /lib/init/tmpfs.sh
. /lib/init/mount-functions.sh

trylink() {
	test -h "/dev/$1" || test -d "/dev/$1" || ln -s -- "$2" "/dev/$1" || {
		ewarn "unable to ensure link /dev/$1 -> $2"
		ls -lad "/dev/$1"
	} >&2
}

MNTMODE=mount_noupdate
trylink fd /proc/self/fd
trylink stdin fd/0
trylink stdout fd/1
trylink stderr fd/2
mount_shm "$MNTMODE"
if [ "$KERNEL" = Linux ]; then
	if [ ! -d /dev/pts ]; then
		mkdir --mode=755 /dev/pts
		[ -x /sbin/restorecon ] && /sbin/restorecon /dev/pts
	fi
	domount "$MNTMODE" devpts "" /dev/pts devpts "-onoexec,nosuid,gid=$TTYGRP,mode=$TTYMODE"
fi
exit 0
