#!/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

echo_success() {							# function to print the OK status
	printf "%-${RES_COL}s%s\n" "$1" "[  OK  ]"
}

echo_failure() {							# function to print the FAILED status
	printf "%-${RES_COL}s%s\n" "$1" "[FAILED]"
}



### MAIN ###

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


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


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


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


rm -rf $plugin_dir

msg="Installing $plugin..."
echo_success "$msg"

exit 0
