#!/bin/sh

#. /etc/sysconfig/i18n
HLEVEL=${1:-disk}
HMODE=${2:-shutdown}

if [ -n "$DISPLAY" -a $HLEVEL = "mem" ]; then
  SETVT=7
  chvt 1
fi

clear
if [ -d /sys/devices/system/cpu/cpu0/cpufreq ]; then
  echo Setting CPU to full speed...
  cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
fi
echo "Hibernating ($HMODE, $HLEVEL) at `date` ..."
echo ----------------------------
if [ $HLEVEL = "mem" ]; then
  vbetool vbestate save > /tmp/vbestate
fi
sleep 1
echo $HMODE >/sys/power/disk
echo $HLEVEL >/sys/power/state
# suspends here
if [ $HLEVEL = "mem" ]; then
  vbetool post
  vbetool vbestate restore < /tmp/vbestate
fi
#setfont $SYSFONT
clear
echo Returned from Hibernation at `date` ...
echo Restoring system time from h/w clock...
clock --hctosys
echo Time adjusted. Now `date`.
if [ -d /sys/devices/system/cpu/cpu0/cpufreq ]; then
  echo Setting CPU back to sane on-demand scaling...
  cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
fi
if [ -z "$DISPLAY" ]; then
  stty rows 25
fi
echo All done. Welcome back!
echo

if [ $HLEVEL = "mem" ]; then
  chvt 2
  if [ -n "$SETVT" ]; then
    chvt $SETVT
  else
    chvt 1
  fi
fi
