#!/bin/sh
# ObexTool startup script for Debain Sarge, written by Hendrik Sattler 
# See also: http://www.stud.uni-karlsruhe.de/~ubq7/debian/
#
# I has been tested and works great with:
#   obexftp (0.10.7-3)
#   tablelist (4.0-1) 
#   obextool (>= 0.32-2 installed in /usr/share/obextool) 
# and obextool cofiguration dir /etc/obextool
# It autodetects IRDA, Bluetooth and serial (/dev/ttyS0) interfaces

XDIALOG=$(which Xdialog)
if [ -z "${OBEXCMD}" ]; then
    echo "Define OBEXCMD environment variable to disable this scan!"
    if [ ${XDIALOG} ]; then
      ${XDIALOG} --infobox "Scanning for IrDA devices" 0 0 1000 &
    fi
    echo "Scanning for Irda devices"
    if ( obexftp -i -l >/dev/null 2>&1 ); then
	if [ ${XDIALOG} ]; then
	  ${XDIALOG} --infobox "Found IrDA device" 0 0 3000 &
	fi
	echo "Found IrDA device"
	OBEXCMD_OPTIONS="-i"
    else
	if [ ${XDIALOG} ]; then
	  ${XDIALOG} --infobox "Scanning for bluetooth devices" 0 0 5000 &
	fi
        echo "Scanning for bluetooth devices"
	BTADDR=$(obexftp -b 2>&1| grep Browsing | cut -f 2 -d " ")
	if [ "${BTADDR}" ]; then
	  if [ ${XDIALOG} ]; then
	    ${XDIALOG} --infobox "Found bluetooth device ${BTADDR}" 0 0 3000 &
	  fi
	  echo "Found bluetooth device ${BTADDR}"
	  OBEXCMD_OPTIONS="-b ${BTADDR}"
	fi
    fi
fi
OBEXCMD_OPTIONS=${OBEXCMD_OPTIONS:=-t /dev/ttyS0}
OBEXCMD=${OBEXCMD:=/usr/bin/obexftp ${OBEXCMD_OPTIONS}}
export OBEXCMD
echo "Using obexftp command: ${OBEXCMD}"

OBEXTOOL_CFG=${OBEXTOOL_CFG:=/etc/obextool}
export OBEXTOOL_CFG
exec /usr/share/obextool/obextool.tk "$@"
