#!/bin/sh
#===============================================================================
#
#  suspend
#
#  Copyright (C) 2009 by Digi International Inc.
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2 as published by
#  the Free Software Foundation.
#
#
#  !Description: suspend system to RAM
#
#===============================================================================

scriptname="$(basename ${0})"
syspower="/sys/power/state"

usage() {
	printf "\nSuspend system to RAM memory\n"
	printf "\nUsage: ${scriptname} [OPTIONS]\n
	-h      Show this help
	\n"
}

resume_storage_devices() {
	# remode disk devices and run coldplug mdev to scan for nodes in
	# case any device was plugged while the system was suspended
	rm -f /dev/sd[a-z] /dev/mmcblk[0-9] && mdev -s

	# for partition nodes fire hotplug ADD event or umount file system
	# in case the real hardware is not present
	for i in /dev/sd[a-z][0-9]* /dev/mmcblk[0-9]p[0-9]*; do
		[ "${i}" = "/dev/sd[a-z][0-9]*" ] && continue
		[ "${i}" = "/dev/mmcblk[0-9]p[0-9]*" ] && continue

		dev_path=$(ls -d /sys/block/*/${i#/dev/} 2>/dev/null)
		if [ -f "${dev_path}/uevent" ]; then
			printf "add" > ${dev_path}/uevent
		else
			if grep -q "${i}[[:blank:]]" /proc/mounts; then
				mdir=$(sed -ne "s,${i}[[:blank:]]\+\([^[:blank:]]\+\)[[:blank:]].*,\1,g;T;p" /proc/mounts)
				umount "${mdir}"
				rmdir -- "${mdir}" 2>/dev/null
				rm -f "${i}"
			fi
		fi

	done
}

while getopts "h" c; do
	case "${c}" in
		h) usage; exit;;
	esac
done

if [ -f "${syspower}" ]; then
	printf "mem" > ${syspower}
	# Storage devices need some more care to resume properly
	resume_storage_devices
else
	printf "\n[ERROR] File ${syspower} not found\n\n"
fi
