#!/bin/bash
#
# recordum_usb_name
#
# v0.1 2013-03-22 RE
#
# translates a /dev/sdX to its model name
# usefull for usb block devices

MYSELF=$( basename $0 )

script_help() {
    cat <<EOF
${MYSELF} help:

This script shows a possibel model name for block devices. It needs root privileges!

Usage: ${MYSELF} <DEVICE>

where:
    DEVICE ... /dev/sdX -- sda, hdb, sdc, ...

EOF
}

if [[ $# -lt 1 ]] || [[ $# -gt 1 ]]
then
    echo "incorrect number of commandline parameters!"
    script_help
    exit 1
fi

if [[ $UID -gt 0 ]]
then
    echo "$MYSELF needs to be run as superuser. You are $USER."
    script_help
    exit 2
fi

DEVICE=$1

# find out if udev knows a name - this also tests for usb device
NAME=$( /sbin/udevadm info --name=${DEVICE} --query=name ) || exit 3
# here we find out if the usb device is a block device
cd /sys/block/${NAME}/device || exit 4
# here if a model exists
MODEL=$( cat model ) || exit 5
VENDOR=$( cat vendor ) || exit 6

# success tell me about it
echo ${VENDOR}"|"${MODEL}
exit 0
