#!/bin/sh

# autodetect video hardware and create xorg.conf file
# - getting video driver from lspci, using table /usr/share/alterator-x11/videocards
#   fallback to fbdev, fallback to vesa
# - getting monitor parameters and resolution from ddc
# - setting some default resolution
# - setting all serial ports as mice
#   (other mouse types will be found by libXiconfig)

usage(){
cat <<EOF
  usage:
   x11_autosetup [-h] [-d <xdriver>] [-r <xres>]  [<file>]
  <xdriver> - video driver (default "auto")
  <xres>    - resolution (default "auto")
  <file>    - xorg.conf file for output (default /etc/X11/xorg.conf.auto)
EOF
> /dev/stderr
}

. shell-regexp
. shell-error
. shell-getopt

PATH="/usr/lib/alterator-x11:$PATH"
xdriver=auto
xdriver_data=
xres=auto
xdepth=
monitor=

verbose=1

while getoptex "d: r: h" "$@"; do
  case "$OPTOPT" in
    "d") xdriver=$OPTARG ;;
    "r") xres=$OPTARG ;;
    "h") usage; exit ;;
    "--") break ;;
  esac
done

shift $(($OPTIND-1))

XORG_TEMPLATE="/usr/share/alterator-x11/xorg.conf"
XORG_CONF=${XORG_CONF:-"$1"}
XORG_CONF=${XORG_CONF:-"/etc/X11/xorg.conf.auto"}

verbose "Creating new config file $XORG_CONF from $XORG_TEMPLATE"
cp -f "$XORG_TEMPLATE" "$XORG_CONF"
if [ "$?" = 1 ]; then exit 1; fi

SETDRV="/usr/sbin/x11presetdrv"
verbose "Executing $SETDRV"
[ -x "$SETDRV" ] && "$SETDRV"

# cleaning ddcdump cache file!
ddcclean

##################################################
# setting up video driver

if [ "$xdriver" = "auto" ]; then
  # try to find driver name via lspci
  xdriver="$(vcscan | cut -f1 | vcdrv_sorted)"
  verbose "Video driver(s) found using lspci: "
  for d in $xdriver; do verbose " * $d";done

  #if empty - setting up vesa
  if [ -z "$xdriver" ]; then
    verbose "  fallback to \"vesa\" driver"
    xdriver="vesa"
  fi
else
  verbose "Using explicitly defined video driver: $xdriver"
  if [ "$xdriver" = "fbdev" ]; then
    xres=$(fbresolution)
    if [ -n "$xres" ]; then
      verbose "  setting resolution $xres for \"fbdev\" driver"
    else
      verbose "  Error: can't get fbdev resolution!"
      xres=auto
    fi
  fi
fi

verbose "Use video driver: " $xdriver
video_setup "$xdriver" "$XORG_CONF"

# finding xdepth
xdepth="$(echo "$xdriver" | vcinfo | sed -nr 's,^xdepth[[:space:]]+,,p')"
verbose "  xdepth: $xdepth"

[ -n "$xdepth" ] && xconf -C "$xdepth" "$XORG_CONF" "$XORG_CONF"

##################################################
# setting up monitor

monitor="$(monscan)"

verbose "Monitor \"$monitor\" found"

[ -n "$monitor" ] && monitor_setup "$monitor" "$XORG_CONF"

##################################################
# setting up resolution

# resolution is already set for fbdev!
if [ "$xres" = "auto" ]; then
  ddc_size=$(ddcsize)
  verbose "  monitor size: $ddc_size cm"

  # FIXME: this is geared towards CRT, TFTs should just get native resolution!
  [ -n "$ddc_size" ] && xres="$(ddcreslist|ddcresbest "${ddc_size% *}" "${ddc_size#* }")"
  [ -z "$xres" ]     && xres="auto"
  verbose "  resolution: $xres"
else
  verbose "Using explicitly defined resolution: $xres"
fi

[ -n "$xres" ] && resolution_setup "$xres" "$XORG_CONF"

##################################################
# setting up mouse

# With libXiconfig we don't need configuration for
# ps and usb mice.

# For serial mice we set up all serial devices

setup_xorg_serial(){
  local device="$1";shift
  printf 'Identifier "%s"\n' "$device"
  printf 'Driver "%s"\n' "mouse"
  printf 'Option "Device" "%s"\n' "$device"
  printf 'Option "Protocol" "%s"\n' "microsoft"
}

for udi in $(hal-find-by-capability --capability serial); do
  mouse_dev=$(hal-get-property --udi $udi --key serial.device)
  setup_xorg_serial "$mouse_dev" | mouseconf -a -c "$XORG_CONF"
  verbose "Setting up serial device $mouse_dev as mouse"
done

##################################################

# cleaning ddcdump cache file!
ddcclean

SETGL="/usr/bin/x11setupdrv"
verbose "Executing $SETGL"
[ -x "$SETGL" ] && "$SETGL" "$XORG_CONF"
