#!/bin/sh

plugin="tobayer01 swapskin"
plugindir="/var/swap/titanskins/tobayer01"
pluginsize=2300
model=`cat /etc/model`
link=`readlink /var/swap`
buildgroup=`cat /etc/.buildgroup`

sync

echo "Checking your box type..."
if [ "$model" = "" ]; then
	echo ""
	echo "Sorry! This plugin is not available for your $model, because it will not work correctly!"
	echo "Aborting installation..."
	exit 1
else
	echo "Box type $model is OK."
fi

echo ""
echo "Checking swapstick..."
if [ ! -d /var/swap/titanskins/ ]; then
	echo "----------------------"
	echo "Swapstick not found..."
	echo "----------------------"
	exit 1
else
	echo "Swapstick is OK."
fi

if [ `df | grep /dev/mtdblock | grep mnt | sed 's/ \+/ /g' | cut -d ' ' -f4 | head -n1 | wc -l` -eq 1 ] && [ $link = "/mnt/swapextensions" ]; then
	SPACE=`df | grep /dev/mtdblock | grep mnt | sed 's/ \+/ /g' | cut -d ' ' -f4 | head -n1`
	FREE=`expr $SPACE - 100`
	echo ""
	echo "Checking free space..."
	echo "Package size $pluginsize kb"
	echo "Free space $FREE kb"
	if [ "$FREE" -lt "$pluginsize" ]; then
		echo "Sorry, not enough free space on your device!"
		exit 1
	else
		echo "OK"
	fi
else
	sync
fi

echo ""
echo "Checking OS..."
if [ `cat /etc/motd | grep $buildgroup | grep M | grep rev | wc -l` -eq 0 ]; then 	 	 
	echo "---------------------------"
	echo "Don't use this TPK package!"
	echo "---"
	echo "Only for $buildgroup image!"
	echo "---------------------------"
	exit 1
else
	echo "OS is OK."
fi

rm -rf $plugindir
echo ""
echo "Installing $plugin..."

exit 0
