#!/bin/sh
# swapon.sh
# Starts and/or stops swap fiel usage.

if [ -d /mnt/card1/swap ]; then
    SWAPDIR=/mnt/card1/swap
elif [ -d /media/card1/swap ]; then
    SWAPDIR=/media/card1/swap
elif [ -d /mnt/card/swap ]; then
    SWAPDIR=/mnt/card/swap
elif [ -d /media/card/swap ]; then
    SWAPDIR=/media/card/swap
fi

case "$1" in

    start)
	echo "Starting swap:"

    # swap partitions (assumed to be hertofore unmounred filesystems)
    for FN in /dev/mmcda[1-9]*; do
        mount | grep $FN >/dev/null 2>&1 && continue
        (set -x; swapon $FN)
    done

    # swapfiles
    if [ -d "$SWAPDIR" ]; then
        for FN in $SWAPDIR/swapfile.*; do
            [ ! -s $FN ] && continue
            (set -x; swapon $FN)
        done
    fi
	;;

    stop)
	echo "Stopping swap:"

    # swap partitions
    for FN in `grep "^/dev/" /proc/swaps | awk '{print $1}'`; do
        (set -x; swapoff $FN)
    done

    # swapfiles
    if [ -d "$SWAPDIR" ]; then
        for FN in $SWAPDIR/swapfile.*; do
            [ ! -s $FN ] && continue
            (set -x; swapoff $FN)
        done
    fi
	;;

    restart)
	$0 stop
	sleep 1
	$0 start
	;;

    *)
	echo "Usage: $0 (start|stop|restart)"
	exit 1
	;;

esac

