#!/bin/sh
#note, this is the original report-video script, builtin to Woof.
#TaZoc has written a more advanced version, available as a pet pkg (sys-info).
#121128 BK complete rewrite.
#130127 internationalised.
#130207 don570: a translation was missing.
#130214 remove scanpci. add "vga" test.
#130215 a module may get loaded, then attempted loaded again, then unloaded, 1st load still there.

export TEXTDOMAIN=report-video
export OUTPUT_CHARSET=UTF-8

. /etc/DISTRO_SPECS

GRAPHICSCHIP="`lspci | grep -i -E 'graphic|video|display|vga' | cut -f 2- -d ' '`" #130214
if [ "$GRAPHICSCHIP" = "" ];then
 if [ -f /tmp/ddcprobe.txt ];then
  GRAPHICSCHIP="`cat /tmp/ddcprobe.txt | grep -E '^oem: |^product: '`"
 else
  GRAPHICSCHIP="`ddcprobe | grep -E '^oem: |^product: '`"
 fi
fi
[ -z "$GRAPHICSCHIP" ] && GRAPHICSCHIP=" unknown"

XSERVER="$(readlink /usr/bin/X)"

echo "$(gettext 'VIDEO REPORT:') ${DISTRO_NAME}, $(gettext 'version') ${DISTRO_VERSION}" > /tmp/report-video

echo "
$(gettext 'Chip description:')
$GRAPHICSCHIP" >> /tmp/report-video

if [ "$XSERVER" = "Xorg" ];then
 if [ -f /etc/X11/xorg.conf ];then
  sPTN='/^Section "Screen"/,/^$/p' #this is a multi-line block find expression.
  reqVIDEODEPTH="$(sed -n "$sPTN" /etc/X11/xorg.conf | tr '\t' ' ' | tr -s ' ' | grep '^[^#][ \t]*Depth ' | cut -f 3 -d ' ')"
  reqVIDEORES="$(sed -n "$sPTN" /etc/X11/xorg.conf | tr '\t' ' ' | tr -s ' ' | grep '^[^#][ \t]*Modes .*#screen0modes' | cut -f 2 -d '"')" #'geany.
  echo "
Requested by /etc/X11/xorg.conf:" >> /tmp/report-video
  [ "$reqVIDEORES" ] && echo "  $(gettext 'Resolution (widthxheight, in pixels): ')${reqVIDEORES}" >> /tmp/report-video
  [ "$reqVIDEODEPTH" ] && echo "  $(gettext 'Depth (bits, or planes):              ')${reqVIDEODEPTH}"  >> /tmp/report-video
  REQMODS="$(grep '^[^#][ \t]*Load ' /etc/X11/xorg.conf | cut -f 2 -d '"' | tr '\n' ' ')" #'geany
  [ "$REQMODS" ] &&   echo "  $(gettext 'Modules requested to be loaded:       ')${REQMODS}" >> /tmp/report-video
  DISABLEMODS="$(grep '^[^#][ \t]*Disable ' /etc/X11/xorg.conf | cut -f 2 -d '"' | tr '\n' ' ')" #'geany
  [ "$DISABLEMODS" ] &&   echo "  $(gettext 'Modules requested to not be loaded:   ')${DISABLEMODS}" >> /tmp/report-video
  #this section is normally commented out...
  sPTN='/^Section "Device"/,/^$/p' #this is a multi-line block find expression.
  reqDRIVER="$(sed -n "$sPTN" /etc/X11/xorg.conf | tr '\t' ' ' | tr -s ' ' | grep '^[^#][ \t]*Driver ' | cut -f 2 -d '"' | tr '\n' ' ')" #'geany
  [ "$reqDRIVER" ] && echo "  $(gettext 'Drivers requested to be loaded:       ')${reqDRIVER}" >> /tmp/report-video
 else
  echo "
$(gettext 'NOTE: there is no /etc/X11/xorg.conf configuration file.')" >> /tmp/report-video
 fi
fi

VIDEODRIVER=""; ALLMODS=""
if [ "$XSERVER" = "Xorg" ];then
 echo "
$(gettext 'Probing Xorg startup log file (/var/log/Xorg.0.log):')" >> /tmp/report-video
 LOADED="$(grep 'Loading .*/xorg/modules/drivers/.*_drv.so' /var/log/Xorg.0.log | rev | cut -f 1 -d '/' | rev | cut -f 1 -d '_' | tr '\n' ' ')"
 for ALOADED in $LOADED
 do
  aPTN="UnloadModule: \"$ALOADED\""
  bPTN="LoadModule: \"$ALOADED\""
  #130215 Xorg loaded noauveau, then tried to load it again, failed, then unloaded, so count them...
  CNTload=`grep "$bPTN" /var/log/Xorg.0.log | wc -l`
  CNTunload=`grep "$aPTN" /var/log/Xorg.0.log | wc -l`
  [ $CNTload -eq 0 ] && continue
  [ $CNTunload -ge $CNTload ] && continue
  VIDEODRIVER="$ALOADED"
  break
 done
 [ "$VIDEODRIVER" ] && echo "  $(gettext 'Driver loaded (and currently in use):') ${VIDEODRIVER}" >> /tmp/report-video
 ALLLOADED="$(grep 'LoadModule: ' /var/log/Xorg.0.log | rev | cut -f 1 -d '/' | rev | cut -f 2 -d '"' | sort -u | tr '\n' ' ')" #'geany
 for ALOADED in $ALLLOADED
 do
  [ "$ALOADED" = "$VIDEODRIVER" ] && continue
  aPTN="UnloadModule: \"$ALOADED\""
  bPTN="LoadModule: \"$ALOADED\""
  #130215 Xorg loaded noauveau, then tried to load it again, failed, then unloaded, so count them...
  CNTload=`grep "$bPTN" /var/log/Xorg.0.log | wc -l`
  CNTunload=`grep "$aPTN" /var/log/Xorg.0.log | wc -l`
  [ $CNTload -eq 0 ] && continue
  [ $CNTunload -ge $CNTload ] && continue
  ALLMODS="${ALLMODS} ${ALOADED}"
 done
 echo "  $(gettext 'Loaded modules:')${ALLMODS}" >> /tmp/report-video
fi

if [ "$XSERVER" = "Xorg" ];then
 echo "
$(gettext 'Actual rendering on monitor:')" >> /tmp/report-video
 if [ "`which xdpyinfo`" ]; then
  XDPYINFO="$(xdpyinfo)"
  VIDEORES="$(echo "$XDPYINFO" | grep ' dimensions: ' | sed -e 's%dimensions: %%' | tr -s ' ' | head -n 1)"
  echo "  $(gettext 'Resolution: ')$VIDEORES" >> /tmp/report-video
  VIDEODEPTH="$(echo "$XDPYINFO" | grep 'depth of root window:' | sed -e 's%depth of root window:%%' | tr -s ' ' | head -n 1)"
  echo "  $(gettext 'Depth:      ')$VIDEODEPTH" >> /tmp/report-video
 else
  if [ "`which xrandr`" ]; then
   XRANDR="$(xrandr | grep '*' | head -n 1)"
   echo "  $(gettext 'Resolution:') $(echo "$XRANDR" | tr -s ' ' | cut -f 2 -d ' ')" >> /tmp/report-video
  else
   echo "  $(gettext 'unknown')" >> /tmp/report-video
  fi
 fi
fi

cat /tmp/report-video
echo
echo "$(gettext '...the above also recorded in /tmp/report-video')" #130207

