#!/bin/bash

#
# (C) 2010 Western Digital Technologies, Inc. All rights reserved.

#
#Add/delete metadata into the DLNA CDS when external storage devices are added/removed
#
#Usage: Here's what's expected to be input to the script 
#   --action=<add/remove>
#   --mount-path=<mount_path> for example; /var/media/FreeAgent_GoFlex_Drive
#   --read-only=<true/false> 
#   --size=<size of volume in MB> for example, 500105.248768


TWONKY_IP="`cat /tmp/twonky_server`"

#TO DO (1) Chenge debug "echos" to the logger util;ity (2) use getopt

# validate the action
if [ "$1" != "--add" ] -o [ "$1" != "--remove" ]; 
then
    exit 1
fi

rpc_action=$1
rpc_action=${rpc_action:9} #strip first nine characters, "--action="

mount_path=$2
mount_path=${mount_path:13} #strip first 13 characters, "--mount-path="

if [ $rpc_action == "add" ];
then
    volume_size=$4
    volume_size=${volume_size:7} #strip first seven characters, "--size="

    #determine whether to report mediatype as flash or hardrive
    dot_position=`expr "$volume_size" : '[0-9]*\.'`
    volume_size_in_MB=${volume_size:0:$dot_position-1} 
    if [ $volume_size_in_MB -gt "262144" ]; #size below 262144 MB will be considered as a flash drive
    then
	media_type="harddrive";
    else
	media_type="flash";
    fi
fi

case "$rpc_action" in
'add')
	curl "http://${TWONKY_IP}/rpc/drive_added?path=${mount_path}&mediatype=${media_type}"
	;;
'remove')
	curl "http://${TWONKY_IP}/rpc/drive_removed?path=${mount_path}"
	;;
*)
	exit 1
esac

exit 0
