#!/bin/dash
# originally written by 01micko, http://murga-linux.com/puppy/viewtopic.php?p=839972#839972
# re-written by jamesbond 2015
# refreshed by step 2022
# License: MIT License
#

export TEXTDOMAIN=fatdog
. gettext.sh

### config
APPTITLE="$(gettext "MTP Browser")"

### helper
if true; then
# with yad {{{
dlg() {
	yad --center --buttons-layout=center --borders=10 \
		--title "$APPTITLE" \
		--window-icon=/usr/share/pixmaps/midi-icons/connect48.png \
		"$@"
}

msg() {
	dlg --button=gtk-ok \
		--image=/usr/share/pixmaps/midi-icons/connect48.png \
		--text="$*"
}

# $1-message [$2-timeout]
info() {
	dlg --button=gtk-ok --timeout="${2:-10}" \
		${2:+--timeout-indicator=bottom} \
		--image=/usr/share/pixmaps/midi-icons/connect48.png \
		--text="$1"
}

# $1-message $2@-choices
get_choice() {
	local message="$1"
	shift
	dlg --geometry=300x200 \
		--button=gtk-ok:0 --button=gtk-cancel:1 \
		--list --no-headers --column= \
		--separator= "$@"
}
# }}}

else
# with Xdialog {{{
dlg() {
	Xdialog --stdout --title "$APPTITLE" "$@"
}

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

# $1-message [$2-timeout]
info() {
	dlg --infobox "$1" 0 0 $((${2:-10} *1000))
}

# $1-message $2@-choices
get_choice() {
	local message="'$1'" p
	shift
	for p; do shift; set -- "$@" "'$p'" "'$p'"; done
	eval dlg --no-tags --menubox "$message" 15 60 3 $*
}
#}}}

fi

# in: $1-devices $2-selected out: $devno
count() {
	devno=1
	for p in $1; do
		[ "$p" = "$2" ] && return
		: $((devno=devno+1))
	done
	devno=""
}

# $1-intro-message
mount_device() {
msg "$1

$(gettext "Please plug-in your device now.")
$(gettext "Click OK when ready.")"

DEVICES=$(simple-mtpfs-detect | awk '$1 ~ /[0-9]+:/ { print $4 }')
if [ -z "$DEVICES" ]; then
	info "$(gettext "No MTP devices found.")"
	return 1
fi

MODELS=""
for p in $DEVICES; do
	MODELS="$MODELS $(udevadm info --query=property --name=/dev/bus/usb/${p%:*}/${p#*:} |
	        sed '/ID_MODEL=/!d; s/ID_MODEL=//' | tr "'\\/ .;:?*[](){}\t" _ )"
done

if dev="$(get_choice "$(gettext "Choose device to connect")" $MODELS)"
then
	count "$MODELS" $dev
	if [ -z $devno ]; then
		info "$(gettext "Unknown error.")"
		return 1
	fi

	mntpoint=$HOME/mtp_$dev
	echo $mntpoint
	mkdir -p $mntpoint
	simple-mtpfs --device $devno $mntpoint
	if mountpoint $mntpoint > /dev/null; then
		rox -d $mntpoint
		rox -x $mntpoint
		return 0
	else
		return 10 # mount failed
	fi
else
	return 20 # cancelled
fi
}

### main

intro="$(gettext "The Media Transfer Protocol (MTP) allows transferring media files to and from portable devices.")"
retry=0 max_retry=2

while true; do
	mount_device "$intro"
	case $? in

	0) # mounted
		info "\
$(gettext "Don't forget to umount it before disconnecting your device.")

$(eval_gettext 'You can do that by going to your $HOME folder, find the mtp_* folder
with a green dot on it, right-click on it and then choose "User Unmount".

Or from terminal you can type:
"fusermount -u $mntpoint"')
"
		exit 0
		;;

	10) # mount failed
		: $((retry=$retry +  1))
		intro="$(gettext "Could not retrieve device storage.")"

		if [ $retry -le $max_retry ]; then
			intro="$intro
$(gettext "For android phones make sure the screen is unlocked and, when prompted, allow access to phone data.")

$(retry=$retry max_retry=$max_retry eval_gettext 'Retry ${retry} of ${max_retry}.')"
		else
			info "$intro
$(gettext "No more retries.")"
			exit 1
		fi
		;;

	20) # cancelled
		info "$(gettext "Cancelled. Nothing was changed.")"
		exit 0
		;;

	*)
		exit 1
		;;
	esac
done
