#!/bin/dash
# Simple tool to receive files

APPTITLE="Bluetooth Receiver"
OBEXD_LOCATION="/usr/lib/obex /usr/lib64/obex /usr/libexec"
DOWNLOAD_DIR=$HOME/received-files # it can't be in Downloads folder because of autochown

### helpers 
dlg() {
	Xdialog --title "$APPTITLE" "$@"
}

infobox() {
	dlg --infobox "$*" 0 0 10000
}

msgbox() {
	dlg --msgbox "$*" 0 0 
}

infoboxbg() {
	infobox "$*" &
}

# returns OBEXD
find_obexd() {
	type obexd > /dev/null && OBEXD=obexd && return
	for p in $OBEXD_LOCATION; do
		[ -x $p/obexd ] && OBEXD=$p/obexd && return
	done
	infoboxbg "Cannot find obexd, exiting."
	exit 1
}

### main
[ -z "$DISPLAY" ] && echo "This is a GUI program, run it under X." && exit 1
find_obexd
! [ -e $DOWNLOAD_DIR ] && mkdir -p $DOWNLOAD_DIR
! [ -e $DOWNLOAD_DIR ] && infoboxbg "Cannot create $DOWNLOAD_DIR" && exit 1
$OBEXD -a -n -r $DOWNLOAD_DIR &
XPID=$!
rox $DOWNLOAD_DIR
msgbox "Bluetooth-receiver is running, a Rox window where downloaded files 
will be kept has been opened. When done, click OK to terminate the receiver. 
Received files will be kept even after you terminate this application."
kill $XPID

