#!/bin/sh
#
# livecd-tmpfs	Remount enough tmpfs into /tmp for development
#
# chkconfig: 345 96 04
# description:	aimed at hasher/mkimage livecd users
# config: /etc/sysconfig/livecd-tmpfs

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/livecd-tmpfs
RETVAL=0

# defaults
MNT=/tmp
RAM=0
SWAP=0
NEED=0

# it's assumed that he who configures knows what he's doing
# (no further safety net deployed)
SourceIfNotEmpty /etc/sysconfig/livecd-tmpfs

tmpfs_size()
{
	# the task is specific enough that we ask for gigs not megs
	df -T "$MNT" 2>/dev/null \
	| awk '/^[a-zA-Z_0-9/-]+[[:space:]]+tmpfs/ { print int($3/1048576); }'
}

ram_size()
{
	# there's no round() in awk, only int(); also vram/kernelernel
	awk '/^MemTotal/ { print int($2/1048576+0.5) }' /proc/meminfo
}

swap_size()
{
	# offset by a rough size of a legacy "cylinder" just in case
	awk '/^SwapTotal/ { print int(($2+8192)/1048576) }' /proc/meminfo
}

guess_need()
{
	# test if not overridden in config
	if [ "$RAM" = 0 ]; then
		echo -n "Checking for RAM size: "
		RAM="$(ram_size)"
		# at least 512m is practically needed for hasher tmpfs
		case "$RAM" in
			0) echo -n "insufficient"; echo_failure; echo; exit 1;;
			1) echo -n "barely enough"; echo_passed;;
			*) echo -n "${RAM}g"; echo_success;;
		esac
		echo
	fi
	if [ "$RAM" -lt 4 -a "$SWAP" = 0 ]; then
		echo -n "Checking for swap size: "
		SWAP="$(swap_size)"
		# we don't rely on swap that bad but...
		case "$SWAP" in
			0) echo -n "none"; echo_passed;;
			1) echo -n "barely enough"; echo_passed;;
			*) echo -n "${SWAP}g"; echo_success;;
		esac
		echo
	fi
	# NB: no memory must be set aside for the processes:
	#     tmpfs will allocate as needed
	VM="$(($RAM+$SWAP))"
	# lower-memory systems might employ swap (if available)
	if [ "$RAM" -lt 8 ]; then
		NEED="$VM"
	else
		NEED="$RAM"
	fi
}

# TODO: swap support/heuristics
start()
{
	# check for tmpfs (mounted/size)
	echo -n "Checking for tmpfs in $MNT: "
	TMPFS="$(tmpfs_size)"
	# we should only change fs size, not type; at least a gig is needed
	case $TMPFS in
		"") echo -n "none"; echo_failure; echo; exit 1;;
		 0) echo -n "not enough"; echo_failure;;
		 1) echo -n "barely enough"; echo_passed;;
		 *) echo -n "${TMPFS}g"; echo_success;;
	esac
	echo
	# might be specified in config
	if [ "$NEED" = 0 ]; then guess_need; fi
	# things might be set up just fine already
	if [ "$NEED" -gt "$TMPFS" ]; then
		echo -n "Adjusting $MNT size to ${NEED}g: "
		mount -o remount,size="$NEED"g "$MNT" \
		&& echo_success \
		|| echo_failure
		echo
	else
		echo -n "No need to adjust $MNT size to ${NEED}g" \
			"(it's ${TMPFS}g already)"
		echo_success
		echo
	fi
	RETVAL=$?
	return $RETVAL
}

stop()
{
	mount -o remount "$MNT"
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		restart
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		if [ -d "$MNT" ]; then
			df -Th "$MNT"
		fi
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
