#!/bin/sh -efu

. shell-getopt

show=""
drv_db="/usr/share/alterator-x11/drivers"

print_help(){
cat <<EOF

Show parameters for xorg driver using own database

Usage: $0 [<options>] <driver>

Options:
  -h                    -- show this help message
  -s (desc|depth|dlist) -- show only driver description,
                           default depth or depth list

Default output format (without -s option) is compatible with
xconf utility

EOF
}

# helper for selection specified field from parameter string
select_field(){
  local var=$1
  shift $2
  eval $var=\"$2\"
}

while getopts "hs:" "$@"; do
  case $OPTOPT in
    h)
      print_help
      exit 1
    ;;
    s)
      show="$OPTARG"
    ;;
  esac
done

shift $(($OPTIND-1))

drv=${1:-}

if [ -z "$drv" ]; then
  print_help >&2
  exit 1
fi

while read params; do
  [ -n "${params%#*}" ] || continue

  eval select_field d 1 $params
  [ "$d" = "$drv" ] || continue

  eval select_field desc   2 $params
  eval select_field depth  3 $params
  eval select_field dlist  4 $params

  case "$show" in
    desc)
      echo "$desc"
    ;;
    depth)
      echo "$depth"
    ;;
    dlist)
      echo "$dlist"
    ;;
    "")
      echo -e "xdriver\t$drv"
      echo -e "xdrivername\t$desc"
      echo -e "xdepth\t$depth"
      echo -e "xdlist\t$dlist"
    ;;
    *)
      echo "Error: unknown parameter to -s option" >&2
      exit 1
    ;;
  esac


done < "$drv_db"
