#!/bin/bash

set -e 

PROGNAME=$0

die() {
    echo "$PROGNAME: $*" >&2
    exit 1
}

usage() {
    if [ "$*" != "" ] ; then
        echo "Error: $*"
    fi

    cat << EOF
Usage: $PROGNAME -t 123
Custom Script usage title here
Options:
-h, --help                         display this usage message and exit
-t, --title						   custom app test title
-f, --first						   select first found device
EOF
    exit 1
}


title=""
first=0

while [ $# -gt 0 ] ; do
    case "$1" in
    -h|--help)
        usage
        ;;

    -t|--title)
        title="$2"
        shift
        ;;
	-f|--first)
		first=1
		;;

    -*)
        usage "Unknown option '$1'"
        ;;
    *)
        usage "Too many arguments"
      ;;
    esac
    shift
done

devices=($(advl))


if [[ -z $devices ]]; then
	exit 1
fi

if [[ $first == "1" ]]; then
    echo ${devices[0]}
    exit 0
fi


count=0
for device in "$devices"; do
	((++count))
	echo "$count) $device" >&2
done

read -p "Enter a device number:- " number
echo ${device[((--number))]}
exit 0

