#!/bin/bash
# Copyright (C) 2003 Juergen "George" Sawinski
#                    Rene Rebe
# All rights reserved.
# Partially copied from/rewritten based on hotplug.
# Partially copied from/rewritten based on ROCK Linux build scripts.
# See README file for further explanation and
# LICENCING TERMS (GPL v2 or later).

. /etc/rockplug/functions

usage() {
	cat <<EOF
/sbin/rockscan  <system> [options]
Copyright (C) 2003 Juergen "George" Sawinski
                   Rene Rebe

System: pci,usb,isapnp,scsi,ieee1394,net

Options:
    --help               This.
    -h|--human           List devices in a human readable format.
    -d|--device device   List information about device (see README)
    -b|--bus path        List information about bus (see README)
EOF
}

output() {
    echo $* | sed -e 's, ,\\ ,g' | tr -d "'"
}

pci_list_human() {
  pci_convert_vars
  pci_desc
  pci_map_modules

  echo "${PCI_SLOT_NAME}:"
  echo -e "\tClass:   $PCI_CLASS  $PCI_CLASS_CLASS_D, $PCI_CLASS_SUBCLASS_D [$PCI_CLASS_PROGIF_D]"
  echo -e "\tDevice:  $PCI_ID $PCI_VENDOR_D ($PCI_DEVICE_D)"
  echo -e "\tSubsys:  $PCI_SUBSYS_ID $PCI_SUBSYS_DEVICE_D ($PCI_SUBSYS_VENDOR_D)"
  
  MODULES="" ; SCRIPTS="" ; pci_map_modules
  echo -e "\tMatched modules: $MODULES"
  echo -e "\tMatched scripts: $SCRIPTS"
}

pci_list() {
  MODULES="" ; SCRIPTS=""

  pci_convert_vars
  pci_desc
  pci_map_modules

  echo $PCI_SLOT_NAME $PCI_ID `output $PCI_VENDOR_D` `output $PCI_DEVICE_D` $PCI_SUBSYS_ID `output $PCI_SUBSYS_VENDOR_D` `output $PCI_SUBSYS_DEVICE_D` $PCI_CLASS `output $PCI_CLASS_CLASS_D` `output $PCI_CLASS_SUBCLASS_D` `output $PCI_CLASS_PROGIF_D` `output $MODULES` `output $SCRIPTS`
}

usb_list_human() {
  usb_convert_vars
  usb_desc

  echo "$DEVICE:"
  echo -e "\tProduct:   $PRODUCT $USB_DEVICE_D ($USB_VENDOR_D)"
  echo -e "\tInterface: $INTERFACE $USB_INTERFACE_CLASS_D, $USB_INTERFACE_SUBCLASS_D, $USB_INTERFACE_PROTOCOL_D"

  echo -e "\tType:      $TYPE $TYPE_D" #@FIXME TYPE_D not defined
  
  MODULES="" ; SCRIPTS="" ; usb_map_modules
  echo -e "\tMatched modules: $MODULES"
  echo -e "\tMatched scripts: $SCRIPTS"
}

usb_list() {
  usb_convert_vars
  usb_desc
  MODULES="" ; SCRIPTS="" ; usb_map_modules

  echo $DEVICE $PRODUCT `output $USB_VENDOR_D` `output $USB_DEVICE_D` $INTERFACE `output $USB_INTERFACE_CLASS_D` `output $USB_INTERFACE_SUBCLASS_D` `output $USB_INTERFACE_PROTOCOL_D` $TYPE '-' '-' '-' `output $MODULES` `output $SCRIPTS`
}

isapnp_list_human() {
  :
}

scsi_list_human() {
  :
}

net_list_human() {
  :
}

system=""
show=""
while [ "$1" ]; do
  case "$1" in
    pci|usb|isapnp|scsi|net)
      system="$1"
      ;;
    --help)
      usage
      exit
      ;;
    -h|--human)
      show="human"
      ;;
    -d|--device)
      shift ; device="$1"
      ;;
    -b|--bus)
      shift ; bus="$1"
      ;;
    *)
      usage
      exit -1
      ;;
  esac
  shift
done

if [ -n "$device" ]; then
  case "$system" in
    pci)
      bus="/proc/bus/pci/`echo $device | tr ":" "/"`"
      ;;
    usb)
      bus="/proc/bus/usb/`echo $device | tr ":" "/"`"
      ;;
  esac
fi

[ -f /etc/conf/$system ] && . /etc/conf/$system
if [ -f /etc/rockplug/$system.functions ]; then
  . /etc/rockplug/$system.functions
  if eval "[ \"\$${system}_autodetect\" ]"; then
    if [ x$show = xhuman ]; then
      if [ -z "$bus" ]; then
	eval "${system}_hwscan ${system}_list_human"
      else
	[ -z "$bus" ] && exit
	eval "${system}_scan_slot $bus"
	eval "${system}_list_human"
      fi
    else
      if [ -z "$bus" ]; then
	eval "${system}_hwscan ${system}_list"
      else
	[ -z "$bus" ] && exit
	eval "${system}_scan_slot $bus"
	eval "${system}_list"
      fi
    fi
  fi
fi
