#!/bin/sh
#
# $1 = aktion [showInfoDirect | swapalloff | swapfileoff | swapramoff | swappartoff]

. /var/etc/autostart/start-config
model=`cat /etc/model`

aktion=$1

if [ "$debug" == 'high' ]; then                      
	set -x
fi

startSwapinfo() {
	if  [ ! "$debug" == 'off' ]; then
		echo "SWAP mount swapfile ($retstr)"
	fi
	if [ "$aktion" == "showInfoDirect" ]; then
		infobox 3 SWAP "SWAP: $retstr" &
	else
		echo "SWAP: $retstr" >> /tmp/.infobox
	fi
	echo "SWAP: $retstr" > /dev/vfd
	touch /tmp/.swapready
}

startCompcache() {
	if grep -q /dev/ramzswap0 /proc/swaps; then return; fi
	if  [ ! "$debug" = 'off' ]; then
		echo $CMD ["$INPUT"] start startCompcache
	fi
	/sbin/insmod /lib/modules/lzo1x_compress.ko
	/sbin/insmod /lib/modules/lzo1x_decompress.ko
	/sbin/insmod /lib/modules/ramzswap.ko disksize_kb=32768
	mknod /dev/ramzswap0 b 253 0 2>/dev/null
	swapon /dev/ramzswap0
	if [ $? -eq 0 ]; then
		retstr="RAM"
		#echo 60 > /proc/sys/vm/swappiness
	else
		retstr="ERROR"
	fi
	startSwapinfo
}

startStopcompcache() {
	if grep -q /dev/ramzswap0 /proc/swaps; then
		if  [ ! "$debug" = 'off' ]; then
			echo $CMD ["$INPUT"] start startStopcompcache
		fi
		swapoff /dev/ramzswap0
		/sbin/rmmod /lib/modules/ramzswap.ko
		/sbin/rmmod /lib/modules/lzo1x_decompress.ko
		/sbin/rmmod /lib/modules/lzo1x_compress.ko
	fi
}

startUmountswap() {
	if grep -q /var/swap/swapfile /proc/swaps; then
		if [ ! "$debug" == 'off' ]; then                      
			echo $CMD ["$INPUT"] start startUmountswap
		fi
		swapoff /var/swap/swapfile
		#echo 60 > /proc/sys/vm/swappiness
	fi
}

startUmountswappart() {
	if [ -e /tmp/.swappart ]; then
		swappart=`cat /tmp/.swappart`
		swappartdev=`echo $swappart | cut -d"#" -f1`
		if grep -q "/dev/$swappartdev" /proc/swaps; then
			if [ ! "$debug" == 'off' ]; then                      
				echo $CMD ["$INPUT"] start startUmountswappart
			fi
			swapoff "/dev/$swappartdev"
		fi
	fi
}

startCreateswappart() {
	if  [ ! "$debug" == 'off' ]; then                      
		echo $CMD ["$INPUT"] start startCreateswappart
	fi
	mkswap "/dev/$1"
	if [ $? -eq 0 ]; then
		echo "SWAPPART" > /tmp/swaplabel
		dd if=/tmp/swaplabel of="/dev/$1" seek=1052 bs=1 count=8
		rm -f /tmp/swaplabel
		echo "$1#swap" > /tmp/.swappart
	fi
}

startCreateswap() {
	if  [ ! "$debug" == 'off' ]; then                      
		echo $CMD ["$INPUT"] start startCreateswap
	fi
	size10=$(($1/10))
	rest=$(($1-($size10*10)))
	rm -f /var/swap/swapfile; sync
	dd if=/dev/zero of=/var/swap/swapfile.tmp bs=1024 count=$size10; sync
	for i in 1 2 3 4 5 6 7 8 9 10; do
		cat /var/swap/swapfile.tmp >> /var/swap/swapfile; sync
		killall -4 infobox
		s=$(($s+9)); echo "SWAPFILE ($s P)" > /dev/vfd
	done
	dd if=/dev/zero of=/var/swap/swapfile.tmp bs=1024 count=$rest; sync
	cat /var/swap/swapfile.tmp >> /var/swap/swapfile; sync
	rm /var/swap/swapfile.tmp; sync
	killall -4 infobox
	s=$(($s+9)); echo "SWAPFILE ($s P)" > /dev/vfd
	mkswap /var/swap/swapfile
	killall -4 infobox
	s=$(($s+1)); echo "SWAPFILE ($s P)" > /dev/vfd
	sync
}

startMountswappart() {
	if grep -q "/dev/$1" /proc/swaps; then return; fi
	swapon "/dev/$1"
	if [ $? -ne 0 ]; then
		startCreateswappart "$1"
		sync
		swapon "/dev/$1"
	fi
	if [ $? -eq 0 ]; then
		retstr="PARTITION"
		startStopcompcache
		#echo 100 > /proc/sys/vm/swappiness
	else
		startMountswap
		return
	fi
	startSwapinfo
}

startMountswap() {
	if grep -q /var/swap/swapfile /proc/swaps; then return; fi
	swapon /var/swap/swapfile
	if [ $? -ne 0 ]; then
		mkswap /var/swap/swapfile
		sync
		swapon /var/swap/swapfile
	fi
	if [ $? -eq 0 ]; then
		retstr="FILE"
		startStopcompcache
		#echo 100 > /proc/sys/vm/swappiness
	else
		startCompcache
		return
	fi
	startSwapinfo
}

startSwappart() {
	if  [ ! "$debug" == 'off' ]; then                      
		echo $CMD ["$INPUT"] start startSwappart
	fi
	swappart=`cat /tmp/.swappart`
	swappartdev=`echo $swappart | cut -d"#" -f1`
	swapparttype=`echo $swappart | cut -d"#" -f2`
	if [ "$swapparttype" != "swap" ]; then
		infobox 15009 SWAP "create Swappart on /dev/$swappartdev" &
		startUmountswappart
		startCreateswappart "$swappartdev"
		killall infobox
	fi
	startMountswappart "$swappartdev"
}

startSwapfile() {
	if [ ! "$debug" == 'off' ]; then                      
		echo $CMD ["$INPUT"] start startSwapfile
	fi
	size=`grep swapsize /var/etc/autostart/start-config | sed "s/swapsize=//g"`
	if [ ! -e /var/swap/swapfile ]; then
		if [ ! "$debug" == 'off' ]; then
			echo SWAP create $size MB swapfile
		fi
		echo "CREATE SWAPFILE" > /dev/vfd
		infobox 15009 SWAP "create Swapfile $size MB /var/swap/swapfile" &
		startUmountswap
		startCreateswap $size
		killall infobox
		startMountswap
	else
		swap=`ls -l /var/swap/swapfile 2>/dev/null | cut -c 30-42`
		if [ -z "$swap" ]; then swap=0; fi
		swap=$(($swap/1024))
		if  [ "$swap" = "$size" ]; then
		(
			startUmountswap
			startMountswap
		)&
		else
			echo "CHANGE SWAPFILE" > /dev/vfd
			infobox 15009 SWAP "change Swapfile $swap > $size MB /var/swap/swapfile" &
			startUmountswap
			startCreateswap $size
			killall infobox
			startMountswap
		fi
	fi
}

if [ ! -e /etc/.usbimage ]; then
	if [ "$model" = "ufs922" ]; then
		exit
	fi
fi	

if [ "$aktion" == "swapalloff" ]; then
	startUmountswappart
	startUmountswap
	startStopcompcache
	exit
fi

if [ "$aktion" == "swapfileoff" ]; then
	startUmountswap
	exit
fi

if [ "$aktion" == "swapramoff" ]; then
	startStopcompcache
	exit
fi

if [ "$aktion" == "swappartoff" ]; then
	startUmountswappart
	exit
fi

if [ "$swap" = 'y' ]; then
	if [ -e /tmp/.swappart ]; then
		startSwappart
	elif grep -q /var/swap /proc/mounts; then
		startSwapfile
	elif [ -e /etc/.usbimage ] && [ "$model" = "ufs910" ]; then
		startSwapfile
	elif [ -e /etc/.BAImage ]; then
		startSwapfile	
	else
		(
			startUmountswappart
			startUmountswap
			startCompcache
		)&
	fi
else
	(
		startUmountswappart
		startUmountswap
		startCompcache
	)&
fi