SUPPORTED_DRIVERS="i915 i965 nouveau r200 radeon swrast"
SUPPORTED_GALLIUM="i915 nouveau r300 r600 svga swrast"

make_drivers_checklist() {

  # For each locale, extract name, title and charmap information
  for driver in $SUPPORTED_DRIVERS; do
      title=$driver

    echo "\"$driver\" \"$title\" \"on\" "
  done
}

make_gallium_drivers_checklist() {

  # For each locale, extract name, title and charmap information
  for driver in $SUPPORTED_GALLIUM; do
      title=$driver

    echo "\"$driver\" \"$title\" \"on\" "
  done
}

select_drivers() {
  BACKTITLE="MesaLib Configuration"
      TITLE="Driver Selection"
       HELP=""
  make_drivers_checklist | xargs \
  dialog --backtitle "$BACKTITLE" \
         --title     "$TITLE"     \
         --stdout                 \
         --separate-output        \
         --checklist "$HELP"      \
         0 0 0
}

select_gallium_drivers() {
  BACKTITLE="Gallium Configuration"
      TITLE="Gallium Driver Selection"
       HELP=""
  make_gallium_drivers_checklist | xargs \
  dialog --backtitle "$BACKTITLE" \
         --title     "$TITLE"     \
         --stdout                 \
         --separate-output        \
         --checklist "$HELP"      \
         0 0 0
}

if ! grep -q "MESADRIVER=" $MODULE_CONFIG; then
  MESADRIVER=`select_drivers`
  set_module_config MESADRIVER "$MESADRIVER"
fi

if [[ -z "$(get_module_config CONFIGURED)" ]]; then
  message "${MESSAGE_COLOR}Gallium is an alternative to DRI and needed to do 3D acceleration on"
  message "nouveau, radeon(-hd), ati or Intel GMA (Poulsbo/Cedar Trail). However, Gallium is not"
  message "performing on Intel GENX graphics (Any onboard Corei3/5/7, G25/33/45, pinetrail gfx"
  message ""
  message "Open Source Drivers:"
  message "If you build for Intel DRI-based hardware, disable Gallium, llvm and enable libva, xvmc."
  message "If you build for Intel CedarTrail or Poulsbo, enable Gallium, llvm, xvmc, vdpau."
  message "If you build for NVidia or Ati hardware, enable Gallium, llvm, xvmc, vdpau."
  message ""
  message "Binary / Proprietary Drivers:"
  message "If you build the NVIDIA binary driver, disable gallium, llvm and xvmc."
  message "If you build the fglrx binary driver, disable gallium, llvm, vdpau and xvmc."
  message "${DEFAULT_COLOR}"
fi

mquery GALLIUM "Enable Gallium llvm renderer?" y "--enable-gallium-llvm" "--disable-gallium-llvm"

# mquery doesn't return or set env's, so read out the result:
GALLIUM=`get_module_config GALLIUM`

if [[ $GALLIUM == y ]] ; then
  if ! grep -q "GALLIUMDRIVER=" $MODULE_CONFIG; then
    GALLIUMDRIVER=`select_gallium_drivers`
    set_module_config GALLIUMDRIVER "$GALLIUMDRIVER"
  fi
fi

mquery XA "Enable XA support?" y "--enable-xa" "--disable-xa"
if [[ $GALLIUM == y ]]; then
  mquery EGL "Enable EGL (for i9x5)?" y "--enable-egl --enable-gallium-egl" "--disable-egl --disable-gallium-egl"
else
  mquery EGL "Enable EGL (for i9x5)?" y "--enable-egl" "--disable-egl"
fi

if [[ $EGL == y ]]; then
  mquery OPENVG "Enable openVG?" y "--enable-openvg" "--disable-openvg"
fi

mquery TEXTURE_FLOAT "Enable patented texture-float? (if this patent is valid in your country choose N)" n "--enable-texture-float" "--disable-texture-float"
mquery ENABLE_GLX_TLS "Enable glx-tls? (lin -r xorg-server also)" y "--enable-glx-tls" "--disable-glx-tls"

set_module_config CONFIGURED y
