#!/usr/bin/openrc-run

description="Activates swap files"

. /opt/etc/functions.openrc

extra_stopped_commands="init"

SWAP_FILES=${SWAP_FILES-""}

depend()
{
	need localmount
	provide ${SVCNAME}
}

init()
{
	for SF in ${SWAP_FILES}; do
		update_me
		if ! [ -f ${SF} ]; then
			eerror "${ME}: ${SF} is not regular file."
			continue
		fi
		einfo "${ME}: preparing swap space ${SF}"
		mkswap ${SF}
		chown root:root ${SF}
		chmod 0600 ${SF}
	done
}

start()
{
	ebegin "Starting \"${SVCNAME}\""
	eindent
	for SF in ${SWAP_FILES}; do
		update_me
		if ! [ -f ${SF} ]; then
			eerror "${ME}: ${SF} is not regular file."
			continue
		fi
		einfo "${ME}: activating swap space ${SF}"
		swapon ${SF}
	done
	eoutdent
	eend $?
}

stop()
{
	ebegin "Stopping \"${SVCNAME}\""
	for SF in ${SWAP_FILES}; do
		swapoff ${SF}
	done
	eend 0
	return 0
}
