#!/bin/bash
#
# SD/SDHC memory card read/write test.
#
# file name : sd_rw_test_v006.sh
# version   : 0.0.6
# create    : Feb.  9th, 2010
# update    : Feb. 15th, 2010
# author    : Fumiyoshi Sasaki
#
# Update history
# Ver0.0.1    1st. Version.
# Ver0.0.2    Ver0.0.1 bug fix.
# Ver0.0.2-2  change Movie Player execute method.
# Ver0.0.2-3  add Movie Player execute recover.
#             add trick for taskbar icon remove
#             in SL-C860.
# Ver0.0.3    fixed version.
# Ver0.0.4    add simple benchmark test.
#             file write/read time measured.
# Ver0.0.5    It was corrected that the execution result
#             of built-in command "time" was able to be
#             redirected to a standard output.
# Ver0.0.6    Change for bucchi SL-C750/SL-C760/SL-C860
#             SD/SDHC memory card driver Ver2.0
#
SD_DRIVER_FILE_NAME="/lib/modules/2.4.18-rmk7-pxa3-embedix/kernel/drivers/block/sharp_mmcsd_m.o"
SOURCE_DIR="/home/zaurus/Documents/Movie_Files"
DESTINATION_DIR="/mnt/card/Documents/Movie_Files"
TEST_FILE_NAME="sfine.asf"
TEST_FILE_SIZE=`ls -l "$SOURCE_DIR/$TEST_FILE_NAME" | tr -s ' ' | cut -d ' ' -f 5`
TEST_FILE_MD5SUM_VALUE=`md5sum "$SOURCE_DIR/$TEST_FILE_NAME" | cut -d ' ' -f 1`

echo "SL-C750/SL-C760/SL-C860 SD/SDHC driver and memory card information"
echo "=================================================================="
DATE=`date +"%Y/%m/%d %H:%M:%S"`
echo "DATE    : $DATE"
PRODUCT=`cat /proc/deviceinfo/product`
echo "PRODUCT : $PRODUCT"
echo
echo "SD memory card driver information"
echo "------------------------------------------------------------------"
SD_DRIVER_FILE_SIZE=`ls -l "$SD_DRIVER_FILE_NAME" | tr -s ' ' | cut -d ' ' -f 5`
if [ -L "$SD_DRIVER_FILE_NAME" ]; then
  echo "SD memory card driver           : use SHARP pure driver"
elif [ ! -e "$SD_DRIVER_FILE_NAME" ]; then
  echo "SD memory card driver           : use unknown SD memory card driver" 
elif [ "$SD_DRIVER_FILE_SIZE" -eq 48185 ]; then
  echo "SD memory card driver           : use bucchi SL-C750/SL-C760/SL-C860 SD/SDHC memory card driver Ver1.0" 
elif [ "$SD_DRIVER_FILE_SIZE" -eq 50018 ]; then
  echo "SD memory card driver           : use bucchi SL-C750/SL-C760/SL-C860 SD/SDHC memory card driver Ver2.0" 
fi
echo "SD memory card driver file size : $SD_DRIVER_FILE_SIZE"
echo
echo "ls -l /lib/modules/2.4.18-rmk7-pxa3-embedix/kernel/drivers/block/sharp_mmcsd_m.o"
echo "------------------------------------------------------------------"
ls -l /lib/modules/2.4.18-rmk7-pxa3-embedix/kernel/drivers/block/sharp_mmcsd_m.o
echo
echo "cat /proc/driver/mmcsd/00/* result"
echo "------------------------------------------------------------------"
echo -n "  detect      : "
cat /proc/driver/mmcsd/00/detect
echo -n "  manufacture : "
cat /proc/driver/mmcsd/00/manufacture
echo -n "  product     : "
cat /proc/driver/mmcsd/00/product
echo -n "  type        : "
cat /proc/driver/mmcsd/00/type
echo

echo "SL-C750/SL-C760/SL-C860 SD/SDHC driver memory card read/write test"
echo "=================================================================="
echo
echo "make directory test"
echo "------------------------------------------------------------------"
echo "mkdir $DESTINATION_DIR"
mkdir $DESTINATION_DIR
echo "mkdir command result status : $?"
if [ "$?" -eq 0 -a -d "$DESTINATION_DIR" ]; then
  echo "make directory test => succes"
else
  echo "make directory test => fail"
  echo "make directory test stoped."
  exit 1
fi
ls -l $DESTINATION_DIR

echo
echo "write file test"
echo "------------------------------------------------------------------"

if
  echo "time cp $SOURCE_DIR/$TEST_FILE_NAME $DESTINATION_DIR"
  (time cp $SOURCE_DIR/$TEST_FILE_NAME $DESTINATION_DIR) 2>&1
  CP_COMMAND_RESULT_STATUS=$?
  echo
  echo "cp command result status : $CP_COMMAND_RESULT_STATUS"
  [ ! "$CP_COMMAND_RESULT_STATUS" -eq 0 ]
then
  echo "write file test => fail - cp command failed."
  echo "write file test stoped."
  exit 1
elif [ ! -e "$DESTINATION_DIR/$TEST_FILE_NAME" ]
then
  echo "write file test => fail - test file is not found."
  echo "write file test stoped."
  exit 1
elif
  WRITE_FILE_SIZE=`ls -l "$DESTINATION_DIR/$TEST_FILE_NAME" | tr -s ' ' | cut -d ' ' -f 5`
  [ "$TEST_FILE_SIZE" -ne "$WRITE_FILE_SIZE" ]
then
  echo "write file test => fail - test file size is not corresponding."
  echo "write file test stoped."
  exit 1
elif
  WRITE_FILE_MD5SUM_VALUE=`md5sum "$SOURCE_DIR/$TEST_FILE_NAME" | cut -d ' ' -f 1`
  [ "$TEST_FILE_MD5SUM_VALUE" != "$WRITE_FILE_MD5SUM_VALUE" ]
then
  echo "write file test => fail - test file md5sum values is not corresponding."
  echo "write file test stoped."
  exit 1
elif
  echo
  echo "cmp -s $SOURCE_DIR/$TEST_FILE_NAME $DESTINATION_DIR/$TEST_FILE_NAME"
  cmp -s $SOURCE_DIR/$TEST_FILE_NAME $DESTINATION_DIR/$TEST_FILE_NAME
  CMP_COMMAND_RESULT_STATUS=$?
  echo "cmp command result status : $CMP_COMMAND_RESULT_STATUS"
  [ ! "$CMP_COMMAND_RESULT_STATUS" -eq 0 ]
then
  echo "write file test => fail - test file are not corr  echo "write file test stoped."
  exit 1
esponding."
else
  echo
  echo "test file size          : $TEST_FILE_SIZE"
  echo "write file size         : $WRITE_FILE_SIZE"
  echo "test file md5sum value  : $TEST_FILE_MD5SUM_VALUE"
  echo "write file md5sum value : $WRITE_FILE_MD5SUM_VALUE"
  echo "write file test => succes"
fi

echo
echo "ls -l $DESTINATION_DIR/$TEST_FILE_NAME"
echo "------------------------------------------------------------------"
ls -l $DESTINATION_DIR/$TEST_FILE_NAME

echo
echo "read file test - benchmark test by dd command"
echo "------------------------------------------------------------------"
echo "time dd if=$DESTINATION_DIR/$TEST_FILE_NAME of=/dev/null bs=512 count=6298"
(time dd if=$DESTINATION_DIR/$TEST_FILE_NAME of=/dev/null bs=512 count=6298) 2>&1

echo
echo "read file test - movie play by SHARP Movie Player"
echo "------------------------------------------------------------------"
echo "start movie play by SHARP Movie Player."
# qcop QPE/Application/movieplayer "setDocument(QString)" $DESTINATION_DIR/$TEST_FILE_NAME
# qcop QPE/System "execute(QString,QString)" movieplayer $DESTINATION_DIR/$TEST_FILE_NAME

qcop QPE/System "execute(QString)" movieplayer 2> /dev/null
sleep 5
qcop QPE/Application/movieplayer "setDocument(QString)" $DESTINATION_DIR/$TEST_FILE_NAME 2> /dev/null

#
# recover Movie Player execute
#
MOVIE_PLAYER_PROCESS=`ps -C movieplayer | grep movieplayer`
echo "Movie Player process : $MOVIE_PLAYER_PROCESS"
if [ -z "$MOVIE_PLAYER_PROCESS" ]
then
  echo "recover Movie Player execute"
  qcop QPE/System "execute(QString)" movieplayer 2> /dev/null
  sleep 5
  qcop QPE/Application/movieplayer "setDocument(QString)" $DESTINATION_DIR/$TEST_FILE_NAME 2> /dev/null
fi

sleep 45
echo "stop movie play."
qcop QPE/Application/movieplayer "quit()" 2> /dev/null

#
# trick for taskbar icon remove in SL-C860.
#
if [ "$PRODUCT" = "SL-C860" ]; then
  qcop QPE/System "execute(QString)" movieplayer 2> /dev/null
  sleep 3
  qcop QPE/Application/movieplayer "setDocument(QString)" $DESTINATION_DIR/$TEST_FILE_NAME 2> /dev/null
fi

qcop QPE/TaskBar "message(QString)" "terminated Movie Player." 2> /dev/null
echo "SHARP Movie Player terminated."

echo
echo "remove file test"
echo "------------------------------------------------------------------"
echo "rm $DESTINATION_DIR/$TEST_FILE_NAME"
rm $DESTINATION_DIR/$TEST_FILE_NAME
echo "rm command result status : $?"
if [ -e "$DESTINATION_DIR/$TEST_FILE_NAME" ]; then
  echo "remove file test => fail"
  echo "remove file test stoped."
  exit 1
else
  echo "remove file test => succes" 
fi

echo
echo "remove directry test"
echo "------------------------------------------------------------------"
echo "rmdir $DESTINATION_DIR"
rmdir $DESTINATION_DIR
echo "rmdir command result status : $?"
if [ -d "$DESTINATION_DIR" ]; then
  echo "remove directry test => fail"
  echo "remove directry test stoped."
  exit 1
else
  echo "remove directry test => succes" 
fi

exit 0

