#!/bin/sh

APP_DIR_SWAP_FILE=/opt/tplink/EAPController/data/swapfile
swap_size=`uci -c /etc/profile.d  get profile.@eapcontroller[0].swap_size`
[ -z "$swap_size" ] && {
	swap_size="512"
}
APP_DIR_SWAP_SIZE=$(($swap_size*1024*1024))


if [ -f ${APP_DIR_SWAP_FILE} ]; then
	# check file size
	filesize=`ls -l ${APP_DIR_SWAP_FILE} | awk '{print $5}'`
	if [ $filesize -ge ${APP_DIR_SWAP_SIZE} ]; then
		# swapfile size is valid
		mkswap ${APP_DIR_SWAP_FILE} && swapon ${APP_DIR_SWAP_FILE}
		exit 0
	fi
fi

# swapfile not exist or file size is invalid
# create swapfile, use Background process
dd if=/dev/zero of=${APP_DIR_SWAP_FILE} bs=1M count=$swap_size
sync

if [ -f ${APP_DIR_SWAP_FILE} ]; then
	mkswap ${APP_DIR_SWAP_FILE} && swapon ${APP_DIR_SWAP_FILE}
fi

exit 0
