#!/bin/sh

KO_DIRS="/lib/modules/`uname -r` /usr/driver"
KO_NAMES="xvmalloc ramzswap"

DEV0_NODE=/dev/ramzswap0
DEV0_DISKSIZE=100000
DEV0_MEMLIMIT=50000

CTRL=/usr/sbin/rzscontrol

# Package was removed, but not purged (i.e. script exists but can't continue)
test -f /usr/sbin/rzscontrol || exit 0

check_device()
{
    if ! test -b $DEV0_NODE
    then
    # Ramzswap wan't compiled into kernel, try to locate modules
	for dir in $KO_DIRS
	do
	    for ko in $KO_NAMES
	    do
		_path="$dir/${ko}.ko"
		test -f "$_path" && insmod "$_path" >/dev/null 2>&1
	    done
	done

	if ! lsmod | egrep "`echo $KO_NAMES | sed -e 's/ /|/g'`"
	then
	    echo "$0: Unable to load compcache kernel modules.  Ignoring."
	    exit 1
	fi
    fi
}

enable_devs()
{
    # Don't setup ramzswap multiple times on the same device
    grep "$DEV0_NODE" /proc/swaps >/dev/null 2>&1 && return

    echo "$0: Setting up $DEV0_NODE with disksize $DEV0_DISKSIZE kb, memlimit $DEV0_MEMLIMIT kb"
    $CTRL $DEV0_NODE --disksize_kb=$DEV0_DISKSIZE --memlimit_kb=$DEV0_MEMLIMIT --init
    if test $? -ne 0
    then
	echo "$0: Unable to setup ramzswap device $DEV0_NODE"
	exit 1
    fi
    swapon $DEV0_NODE
}

disable_devs()
{
    grep "$DEV0_NODE" /proc/swaps >/dev/null 2>&1 && swapoff "$DEV0_NODE"
    $CTRL "$DEV0_NODE" --reset
}

show_stats()
{
    $CTRL "$DEV0_NODE" --stats
}

# Well, by default we run 'start' as rc.sysinit calls us without parameters during startup sequence ...
test -z "$1" && set -- "start"

case "$1" in
    start)
	check_device
	enable_devs
	;;
    
    stop)
	disable_devs
	;;

    status)
	show_stats
	;;

    *)
	echo "Usage: $0 {start|stop|status}"
	exit 1
	;;
esac


    


    
    
    
