#!/bin/sh

strip_vendor()
{
	if echo $HDD_MODEL | grep -qi "^$1"; then
		HDD_VENDOR=$2
		HDD_MODEL=`echo $HDD_MODEL | sed "s/^$1//i"`
	fi
}

detect_vendor()
{
	if echo $HDD_MODEL | grep -qi "$1"; then
		HDD_VENDOR=$2
	fi
}

hal_detect()
{
	for UDI in `hal-find-by-property --key storage.drive_type --string 'disk'`; do
		HDD_DEV=`hal-get-property --udi "$UDI" --key block.device`
		HDD_MODEL=`hal-get-property --udi "$UDI" --key info.product`
		HDD_VENDOR=`hal-get-property --udi "$UDI" --key storage.vendor`
		HDD_SERIAL=`smartctl -a $HDD_DEV | sed -ne '/^Serial number:/ s/^Serial number: *//p'`

		detect_vendor '^ST.*' 'Seagate'
		detect_vendor '^D...-.*' 'IBM'
		detect_vendor '^IBM.*' 'IBM'
		detect_vendor '^HITACHI.*' 'Hitachi'
		detect_vendor '^IC.*' 'Hitachi'
		detect_vendor '^HTS.*' 'Hitachi'
		detect_vendor '^HDS.*' 'Hitachi'
		detect_vendor '^FUJITSU.*' 'Fujitsu'
		detect_vendor '^MP.*' 'Fujitsu'
		detect_vendor '^TOSHIBA.*' 'Toshiba'
		detect_vendor '^MK.*' 'Toshiba'
		detect_vendor '^MAXTOR.*' 'Maxtor'
		detect_vendor '^Pioneer.*' 'Pioneer'
		detect_vendor '^PHILIPS.*' 'Philips'
		detect_vendor '^QUANTUM.*' 'Quantum'
		detect_vendor 'FIREBALL.*' 'Quantum'
		detect_vendor '^WDC.*' 'Western Digital'
		detect_vendor 'WD.*' 'Western Digital'

		strip_vendor 'Maxtor ' 'Maxtor'
		strip_vendor 'WDC ' 'Western Digital'
		strip_vendor 'Hitachi ' 'Hitachi'

		if [ -z "`echo "$HDD_VENDOR" | grep -i 'Linux'`" ] && [ -z "`echo "$HDD_VENDOR" | grep -i 'Adaptec'`" ] && [ -z "`echo "$HDD_VENDOR" | grep -i 'Intel'`" ]; then
			echo HDD "$HDD_VENDOR" "$HDD_MODEL" "$HDD_SERIAL" ' - lshw' >> $HOME/debug.log
			$SHARE_DIR/add-component HDD "$HDD_VENDOR" "$HDD_MODEL" "$HDD_SERIAL"
		fi	
	done
}

einarc_detect()
{
	einarc -l | cut -f1 | while read TYPE; do
		einarc -t  "$TYPE" physical list | while read L; do
			HDD_VENDOR=`echo "$L" | cut -f2 | cut -f1 -d' '`
			HDD_MODEL=`echo "$L" | cut -f2 | cut -f2 -d' '`
			HDD_SERIAL=`echo "$L" | cut -f4`
			echo HDD "$HDD_VENDOR" "$HDD_MODEL" "$HDD_SERIAL" ' - einarc' >> $HOME/debug.log
			$SHARE_DIR/add-component HDD "$HDD_VENDOR" "$HDD_MODEL" "$HDD_SERIAL"
		done
	done
}

hal_detect
einarc_detect
