#!/bin/sh

LANG=en_US.UTF-8
MODUL=/sys/module/zswap
DEBUG=/sys/kernel/debug/zswap
SWTCH=$MODUL/parameters/enabled
SWAPS=`swapon | grep -cv zram`
ZRAMS=`swapon | grep -c  zram`

Protect(){
	echo "$1"
	exit 1
}

Show(){
	grep -R . $2 2>&1 | sed '/uevent/d;/\/enabled/d;s|_percent|, %|;s|.*/||;s|:|: |;s|_| |g'
}

case $1 in # switch zswap on/off
	1|Y|y|on)	[ $ZRAMS -gt 0 ] && Protect "can't start: zram-swap is already in use"
				[ $SWAPS -lt 2 ] && Protect "swap partition not found"
				echo Y >$SWTCH;;
	0|N|n|off)	echo N >$SWTCH
esac

case `cat $SWTCH` in # is zswap in use?
	N)	echo zswap disabled ;;
	Y)	Show Stats $DEBUG
		PAGE=`cat $DEBUG/stored_pages`
		POOL=`cat $DEBUG/pool_total_size`
		[ $POOL -gt 0 ] &&
			printf "compression ratio: %s\n" `echo "scale=2;4096*$PAGE/$POOL" | bc` ||
			printf "\nzswap is not in use\n"
esac
