#!/sbin/openrc-run

depend() {
	need localmount
	after swapfile
	before bootmisc
}

start() {
	source /usr/share/misc/source_deviceinfo

	if [ "$deviceinfo_tmp_as_tmpfs_size" = "0" ]; then
		return 0
	fi

	_tmpfs_size="50%"
	if [ -n "$deviceinfo_tmp_as_tmpfs_size" ]; then
		_tmpfs_size="$deviceinfo_tmp_as_tmpfs_size"
	fi

	_mem_size_mb="$(LC_ALL=C free -m | awk '/^Mem:/{print $2}')"
	# Mount if asked to, or if more than default memory,
	# but never if already mounted
	if [ -n "$deviceinfo_tmp_as_tmpfs_size" ] || \
	   [ $_mem_size_mb -ge 2048 ] && \
	   ! mountinfo -q -f tmpfs /tmp ; then
		ebegin "Mounting /tmp as tmpfs"
		mkdir -p /tmp || { eend 1 "Failed to create /tmp" ; return 1 ; }
		# mount params taken from Debian buster
		mount -t tmpfs -o mode=1777,strictatime,nosuid,nodev,size="$_tmpfs_size",nr_inodes=400k tmpfs /tmp
		eend $?
	fi
}
