#!/bin/bash

# List block devices that are not removable,
# if multiple devices are found, the user will be invited to choose one.
# If no devices are found, the program exit with error code 1

outfile=${1:-/dev/stdout}
. /usr/sbin/dialog-helper

target_array=()
for i in /sys/block/*/device; do
	[ -d "$i/slaves" ] && continue
	dev=$(echo $i | cut -d'/' -f-4)
	outdev=$(echo $i | cut -d'/' -f4)
	grep -q 1 "$dev/removable"
	if [ "$?" = "1" ]; then
		 target_array+=("/dev/$outdev")
	fi
done

if (( "${#target_array[@]}" == "1" )); then
	TARGET="${target_array[0]}"
elif (( "${#target_array[@]}" > "1" )); then
	dialog_helper --no-items --menu "Installation destinaion device:" 10 40 3 $( for i in "${target_array[@]}"; do echo "$i"; done )
	TARGET="$DIALOGRES" 
else
	dialog --msgbox "No devices could be found, no installation possible" 10 40
	exit 1
fi

cat << EOC > $outfile
$TARGET
EOC

exit 0
