#!/bin/bash
case "$1" in
start)
export ramsize=`free -t -b|tr -s \  |grep Total|cut -f 2 -d :|cut -f 2 -d \  `
mkswap -L swap -s $ramsize -F /swapfile
swapon /swapfile
export offset=`filefrag -v /swapfile|head -n 4|tail -n 1|tr : \\\\n|head -n 3|tail -n 1|cut -f 1 -d .|sed "s|\ ||g"`
export paramlen=`cat /boot/grub/grub.cfg|grep resume_offset|head -n 1|wc -w|cut -f 1 -d \  `
if [ $paramlen -eq 0 ];then
sleep .01
else
export origoffset=`cat /boot/grub/grub.cfg|grep resume_offset|cut -f $paramlen- -d \  |cut -f 2 -d =`
for f in `echo $origoffset`;do
sed -i "s|resume_offset=$f|resume_offset=$offset|g" /boot/grub/grub.cfg
done
fi
;;
stop)
swapoff /swapfile
echo > /swapfile
rm /swapfile
;;
restart)
$0 stop
$0 start
;;
*)
echo usage: $0 start\|stop\|restart
exit 1
;;
esac
