#!/bin/sh
# /dev/shm setup for mount-devfs (called from execline up).
# Keep this as a real sh script: execlineb does not treat '...' as a
# quoted word, so /bin/sh -c '…if…' becomes $0=if and fails with
# "if: 1: Syntax error: Unterminated quoted string".

set -e

# Already a mountpoint: nothing to do.
if mountpoint -q /dev/shm 2>/dev/null; then
	exit 0
fi

# Symlink (typically /dev/shm -> /run/shm): mount the target.
if [ -L /dev/shm ]; then
	target=$(readlink -f /dev/shm 2>/dev/null || readlink /dev/shm)
	[ -n "$target" ] || exit 0
	if mountpoint -q "$target" 2>/dev/null; then
		exit 0
	fi
	mkdir -p "$target" 2>/dev/null || true
	mount -t tmpfs -o mode=1777,nosuid,nodev tmpfs "$target" 2>/dev/null || true
	exit 0
fi

# Plain path: ensure directory, then mount tmpfs (source name "tmpfs").
if [ ! -d /dev/shm ]; then
	rm -f /dev/shm 2>/dev/null || true
	mkdir -p /dev/shm 2>/dev/null || true
fi
[ -d /dev/shm ] || exit 0
mount -t tmpfs -o mode=1777,nosuid,nodev tmpfs /dev/shm 2>/dev/null || true
exit 0
