#!/bin/sh # varables DEBUG=${DEBUG:-""} if [ ! -z $DEBUG ];then dbg_msg="Check /var/log/messages for errors." else dbg_msg="Enable *DEBUG* for error log." fi device_addr=$1 own_name=$(basename $0) dir=$(mkdir)/mnt/$device_addr browser=${browser:-"xdg-open"} count=0 # Check for kdialog if which kdialog 1>/dev/null 2>/dev/null; then messagetype="kdialog" else messagetype="gtkdialog" export p_file="progress.tmp" export m_file="message.tmp" touch $p_file touch $m_file echo "0" > $p_file fi #gtkdialog abstraction gtkbox_interactive() { export messagebox=' progress p_file Done. ' } gtkmessage() { I=$IFS; IFS="" for STATEMENT in $(gtkdialog --center --program=messagebox); do eval $STATEMENT done IFS=$I } #gtkdialog progress counter if [ "$messagetype" = "gtkdialog" ]; then progress(){ while : do sleep 0.3 percentage=$(cat $p_file) text=$(cat $m_file) echo $percentage echo $text if [ $percentage = 100 ]; then rm *.tmp exit 0 fi done } message(){ echo $MSG_TXT > $m_file } fi export -f progress # End gtkdialog abstraction # mount function mount_device () { i=0 LIMIT=5 while [ $i -lt $LIMIT ] do let count=$count+17 echo "$count" > $p_file printf "$i " [ ! -z $DEBUG ] && logger -i -t $own_name "Perform $i try to mount device $device_addr to $dir using obexfs" [ ! -z $DEBUG ] && logger -i -t $own_name "Execute: obexfs -b $device_addr -B 10 $dir" out=$(obexfs -b $device_addr $dir 2>&1) [ ! -z $DEBUG ] && logger -i -t $own_name "$out" mounts=$(grep $dir /proc/mounts | wc -l) [ ! -z $DEBUG ] && logger -i -t $own_name "Found $mounts mounted $dir inside /proc/mounts" if [ $mounts -ne 0 ] ;then MSG_TXT="Successfully mounted $device_addr to $dir" if [ $messagetype == "kdialog" ]; then kdialog --passivepopup "$MSG_TXT" 2 elif [ $messagetype == "gtkdialog" ]; then message $"$MSG_TXT" echo "99" > $p_file sleep 2 echo "100" > $p_file fi [ ! -z $DEBUG ] && logger -i -t $own_name "Open $dir with $browser" out=$($browser $dir 2>&1) [ ! -z $DEBUG ] && logger -i -t $own_name "$out" exit 0 else logger -i -t $own_name "Failed to mount $device_addr to $dir from try $i " fi sleep 1 ((i += 1)) done if [ $mounts -eq 0 ] ;then MSG_TXT="Failed to mount $device_addr to $dir. $dbg_msg" if [ $messagetype == "kdialog" ]; then kdialog --passivepopup "$MSG_TXT" 4 elif [ $messagetype == "gtkdialog" ]; then message $"$MSG_TXT" echo "99" > $p_file sleep 4 echo "100" > $p_file fi fi } # unmount function umount_device () { mounts=$(grep $dir /proc/mounts | wc -l) if [ $mounts -ne 0 ] ;then [ ! -z $DEBUG ] && logger -i -t $own_name "Perform umount of $dir if it's already mounted." out=$(fusermount -uz $dir 2>&1) [ ! -z $DEBUG ] && logger -i -t $own_name "$out" mounts=$(cat /proc/mounts |grep $dir|wc -l) if [ $mounts -eq 0 ] ;then [ ! -z $DEBUG ] && logger -i -t $own_name "Successfully unmounted $dir." else logger -i -t $own_name "$dir is already mounted. Failed to umount $dir.$out. Please perform its umount manually." MSG_TXT="Failed to umount $dir - $dbg_msg" if [ $messagetype == "kdialog" ]; then kdialog --passivepopup "$MSG_TXT" 4 elif [ $messagetype == "gtkdialog" ]; then message $"$MSG_TXT" echo "99" > $p_file sleep 4 count=5 fi exit fi else [ ! -z $DEBUG ] && logger -i -t $own_name "$dir is not mounted - no need to perform umount..." fi } # mkdir function create_dir () { [ ! -z $DEBUG ] && logger -i -t $own_name "Create directory $dir to mount the device by bluetooth..." out=$(mkdir -p $dir 2>&1) [ ! -z $DEBUG ] && logger -i -t $own_name "$out" } #main MSG_TXT="Mount device is in progress... Please wait..." if [ $messagetype == "kdialog" ]; then kdialog --passivepopup "$MSG_TXT" 4 elif [ $messagetype == "gtkdialog" ]; then message $"$MSG_TXT" gtkbox_interactive gtkmessage& fi create_dir umount_device mount_device