#!/bin/sh

plugin_size=2300
plugin_short="tobayer01"
plugin_type="swapskin"
plugin_target="/var/swap/titanskins"

plugin_dir="$plugin_target/$plugin_short"
plugin="$plugin_short $plugin_type"

model=`cat /etc/model`
buildgroup=`cat /etc/.buildgroup`


### FUNCTIONS ###

RES_COL=60									# column number to place the status message
MOVE_TO_COL="echo -en \\033[${RES_COL}G"	# command to move to the configured column number

echo_success() {							# function to print the SUCCESS status
	$MOVE_TO_COL; echo -n "[  OK  ]"; echo
}

echo_failure() {
	$MOVE_TO_COL; echo -n "[FAILED]"; echo	# function to print the FAILED status message
}



### MAIN ###

echo -n "Checking box type..."
if [ "$model" = "" ]; then
	echo_failure
	echo
	echo "Sorry, $plugin is not available for the $model!"
	echo "Aborting installation..."
	exit 1
else
	echo_success
fi


echo -n "Checking installation directory..."
if [ ! -d $plugin_target ]; then
	echo_failure
	echo
	echo "Sorry, $plugin_target not found!"
	echo "Aborting installation..."
	exit 1
else
	echo_success
fi


sync
SPACE=`getfreespace $plugin_target`
FREE=`expr $SPACE - 100`
echo -n "Checking free space ($plugin_size in $FREE kB)..."
if [ "$FREE" -lt "$plugin_size" ]; then
	echo_failure
	echo
	echo "Sorry, not enough free space in $plugin_target!"
	echo "Aborting installation..."
	exit 1
else
	echo_success
fi


echo -n "Checking image type..."
if [ `cat /etc/version | grep $buildgroup | wc -l` -eq 0 ]; then
	echo_failure
	echo
	echo "Sorry, $plugin is not available for this image type!"
	echo "Aborting installation..."
	exit 1
else
	echo_success
fi


rm -rf $plugin_dir

echo -n "Installing $plugin..."
echo_success

exit 0
