#!/bin/sh
#####################################################################
#
# saveweb 2
#
# Shell script to save current web files to flash memory
#
# 2008/1/24
#
# Usage: saveweb 2
#
##################################################################### 

# Get ramdisk free space
RAMDISK_FREE_SPACE=`/bin/df -k | /bin/grep /dev/ram0 | /bin/awk 'NR==1 {print $4}'`

if [ "$RAMDISK_FREE_SPACE" = "" ]; then
	RAMDISK_FREE_SPACE=`/bin/df -k | /bin/grep rootfs | /bin/awk 'NR==1 {print $4}'`
fi

if [ "$RAMDISK_FREE_SPACE" = "" ]; then
	echo "Can't get the ramdisk free space."
	exit 0
fi

if [ $RAMDISK_FREE_SPACE -lt 0 ]; then
	echo "Ramdisk free space is not enough! (${RAMDISK_FREE_SPACE} kB)"
	exit 0
fi

echo
echo "Ramdisk free space : ${RAMDISK_FREE_SPACE} kB"

# Get the disk usage of /web/*
WEB_DISK_USAGE=`/usr/bin/du -k /web | /bin/grep -v /web/ | /bin/awk 'NR==1 {print $1}'`

echo "/web/* disk usage  : ${WEB_DISK_USAGE} kB"
echo

# Check ramdisk free space is enough
echo -n "Checking .... "
RFS_FREE=`expr $RAMDISK_FREE_SPACE '*' 10`
WEB_WR_DU=`expr $WEB_DISK_USAGE '*' 13`

# echo "RFS_FREE $RFS_FREE"
# echo "WEB_WR_DU $WEB_WR_DU"

if [ $RFS_FREE -lt $WEB_WR_DU ]; then
	echo "[Fail]"
	echo "  -  Ramdisk free space may not be enaugh."
	exit 0
fi
echo "[ OK ]"

echo -n "Working ... "

# Update a image on flash
#echo "Saving current web files to flash memory"
/usr/sbin/fuc -sl wi

echo "[Done]"


#
# Print saved wi size
WI_SIZE=`/usr/sbin/fuc -d | /bin/grep "03 " | /bin/awk 'NR==1 {print $3}'`

echo
echo "Saved wi size : $WI_SIZE"
echo

