#!/bin/sh

set -eu

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

mount_if_needed() {
  target=$1
  shift

  mountpoint -q "${target}" || mount "$@" "${target}"
}

mount_if_needed /proc -n -t proc proc
mount_if_needed /sys -n -t sysfs sysfs
mount_if_needed /dev -n -t devtmpfs devtmpfs

# devtmpfs supplies device nodes, but not the descriptor aliases used by
# shells, process substitution, and the Steam Runtime logger.
ln -snf /proc/self/fd /dev/fd
ln -snf /proc/self/fd/0 /dev/stdin
ln -snf /proc/self/fd/1 /dev/stdout
ln -snf /proc/self/fd/2 /dev/stderr

mkdir -p /dev/pts /dev/shm /run
mount_if_needed /dev/pts -n -t devpts -o gid=5,mode=620 devpts
mount_if_needed /dev/shm -n -t tmpfs -o nodev,nosuid,mode=1777 tmpfs
mount_if_needed /run -n -t tmpfs -o nodev,nosuid,mode=0755 tmpfs

mkdir -p /run/dbus /run/lock /run/udev /run/user
chmod 0755 /run/dbus /run/udev /run/user
chmod 1777 /run/lock
