#!/bin/sh
# 2010-04 TaZoC, Graphics Information from CL/GUI, based on report-video by Barry Kauler
#100928 Added /tmp/$USER/report-video-full with xorg.conf and Xorg log. Should work on any Woof-based system. -q or --quiet for no GUI.
#110417 L64 updates
#110615 Better driver detection
#110719 #110615 again
#110910 Driver detection fix for Xorg 1.9.5
#111020 #110615 again (thanks to Lobster)
#120714 0.8 Strip ':' and 'vendor=*' from driver
#120808 1.1 Accept -cl|--nogui. New gtkdialog GUI.
#120813 1.2 Report arch (uname -rsm)
#120902 1.3 Bugfix: incorrectly detected vesa, thanks to BarryK, 01micko

APPTITLE="Graphics Information"
APPVERSION="1.3"
APPNAME="report-video"
APPNAMECAP="Report Video"
ICON="/usr/share/icons/report-video.png"
ABOUT="$APPNAME $APPVERSION TaZoC lhpup.org license: GPL v3 or any later version"
FONTCOLOR="#00005C"
export USER=`id -un`
REPORTDIR="/tmp/${USER}"
REPORT_FILE="${REPORTDIR}/${APPNAME}"
SYSDATE="`date +'%y%m%e'`" # yymmdd
nl='
'
. /etc/DISTRO_SPECS

if [ "$USER" != "root" ]; then #120808
 exec sudo -A "$0" "$@" #support sudo for non-root user
fi

# I18N #120808 from BarryK's pmount
# set locale
# myLOCALE=$LANG # is enough for puppy. but this code is more general. 
for myLOCALE in C $(echo $LANGUAGE|cut -d':' -f1) $LC_ALL $LANG;do :;done	# ex. 	ja_JP.UTF-8
# search locale file
myLOCALE1=$(echo $myLOCALE|cut -d'.' -f1)		# ex.	ja_JP
myLOCALE2=$(echo $myLOCALE|cut -d'_' -f1)	# ex.	ja
LOCALEDIR=/usr/share/locale
LOCALEFILE=$(basename $0).mo
for D in C $myLOCALE2 $myLOCALE1 $myLOCALE
do
	F="$LOCALEDIR/$D/LC_MESSAGES/$LOCALEFILE"
	[ -f "$F" ] && . "$F" 
done
#[ "`echo "$LANG" | grep '^en_US'`" ] && export LANG=C #111214
# end of I18N

#120808 define gtkdialog (thanks to Radky)
if [ "`which gtkdialog4`" ]; then
	GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
	GTKDIALOG=gtkdialog3
else
	GTKDIALOG=gtkdialog
fi

case $@ in
*-V|*-version)
  echo -e "$ABOUT"; exit 0 ;; ### End script ###
  
*-h|*-help)
  echo -e "$ABOUT"
  echo "This script can display some graphics information."
  echo "The output is also recorded at $REPORT_FILE."
  echo -e "\nUsage: $APPNAME [-q | --quiet ] [-h | --help] [-V | --version]"
  exit 0 ;; ### End script ###
  
*-q|*-quiet|*-cl|*-nogui|'') ;; #120808

*) exec $0 -h
   exit 0 ;; ### End script ###
esac

###### Function call scripts ######
MainDialogFunc() { #120808

export TOPMSG="The report below was saved to <span color='"$FONTCOLOR"'><b>$REPORT_FILE</b></span> ${nl}and archived with <span color='"$FONTCOLOR"'>/etc/X11/xorg.conf</span>, <span color='"$FONTCOLOR"'>/var/log/Xorg.0.log</span> ${nl}as <span color='"$FONTCOLOR"'><b>${REPORT_FILE}-full.gz</b></span>"

export MAIN_DIALOG="<window title=\"$APPNAMECAP $APPTITLE $APPVERSION\" icon-name=\"$APPNAME\" allow-shrink=\"true\" width-request=\"680\" height-request=\"442\">
 <vbox>
  <hbox width-request=\"572\" height-request=\"394\">
   <vbox scrollable=\"true\" space-fill=\"false\" space-expand=\"true\">
    <hbox>
     <pixmap>
      <width>96</width><height>96</height>
      <input file>$ICON</input>
     </pixmap>
     <text width-request=\"4\"><label>\"\"</label></text>
     <text width-request=\"536\" use-markup=\"true\"><label>\"${TOPMSG}\"</label>
     </text>
    </hbox>

    <edit editable=\"false\">
     <height>282</height>
     <default>\"$(echo; echo -e "$MSG" | sed -e 's/\(^.*$\)/  \1/g' -e '/^  $/d' -e '/^$/N;/\n$/D')\"</default>
    </edit>

    <text height-request=\"1\"><label>\"\"</label></text>
   </vbox>
  </hbox>
  <hbox height-request=\"36\" space-fill=\"false\" space-expand=\"true\">
   <button width-request=\"124\" height-request=\"36\" tooltip-text=\"Open in File Manager\">
    <input file stock=\"gtk-open\"></input>
    <label>\"Reports \"</label>
    <action>rox -d ${REPORTDIR} &</action>
   </button>
   <button width-request=\"124\" height-request=\"36\" tooltip-text=\"$(gettext ROX-Filer 'Cancel')\">
    <input file stock=\"gtk-quit\"></input>
    <label>\"$(gettext ROX-Filer 'Close') \"</label>
    <action type=\"exit\">Cancel</action>
   </button>
  </hbox>
 </vbox>
 <action signal=\"hide\">exit:Exit</action>
</window>
 "
 RET=$($GTKDIALOG --program=MAIN_DIALOG --center)
 #gxmessage "$RET" # debug
 eval "$RET"
 unset MAIN_DIALOG
 [ "$(echo "$EXIT" | grep "Cancel\|abort")" ] && exit

} ## End MainDialogFunc

mkdir -p "/tmp/$USER/" #100928 might want to run as non-root
XORGCONF_PRESENT="yes"
XORGCONF="\n=== /etc/X11/xorg.conf ==="; XORGLOG="\n=== /var/log/Xorg.0.log ===\n"
[ ! -e /etc/X11/xorg.conf ] && XORGCONF="=== /etc/X11/xorg.conf not present ===\n" && XORGCONF_PRESENT="no"
[ ! -e /var/log/Xorg.0.log ] && XORGLOG="=== /var/log/Xorg.0.log not present ===\n"

DDCPROBE="$(cat /tmp/ddcprobe.txt 2>/dev/null || ddcprobe)" #120808
LSPCI="$(lspci | grep VGA | tr '\n' '|' | sed 's%|%  %g' | sed -e 's%0[0-9]:0%%g')" #120808
if [ -n "$(echo $LSPCI | grep VGA)" ]; then #120808
 GRAPHICSCHIP="  ${LSPCI}
$(echo "$DDCPROBE" | grep -E '^oem: |^product: ' | sed 's%^%  %g')"
else
 GRAPHICSCHIP="$(echo "$DDCPROBE" | grep -E '^vendor: |^oem: |^product: ' | sed 's%^%  %g')"
fi

[ -s /var/log/Xorg.0.log ] && CHIPSET="`cat /var/log/Xorg.0.log | grep 'Chipset:' | cut -f 2 -d '"'`" #'geany
[ -z "$GRAPHICSCHIP" ] && GRAPHICSCHIP="$CHIPSET" #110417
[ "$GRAPHICSCHIP" = "" ] && GRAPHICSCHIP="unknown"

# TaZoC Some old driver tag cleanup, may not be necessary anymore
if [ -s /etc/X11/xorg.conf ] && [ -n "`grep ' #card0driver' /etc/X11/xorg.conf`" ]; then #110719
 sed -i -e "s/^ #card0driver$//" /etc/X11/xorg.conf #TaZoC
 sed -i -e "s/#card0driver #card0driver/#card0driver/" /etc/X11/xorg.conf #TaZoC
 sed -i -e  '/./,/^$/!d' /etc/X11/xorg.conf #TaZoC removes consecutive blank lines
fi

XSERVER="`readlink /usr/bin/X`"
[ "$XSERVER" = "Xvesa" ] && VIDEODRIVER="Xvesa" #110417
# Driver detection, first try xorg.conf...
[ -z "$VIDEODRIVER" ] && VIDEODRIVER="`grep '^[^#]*Driver ' /etc/X11/xorg.conf | grep -i -v -E 'mouse|kbd|synaptics'`" #111020 #120902
#120902 Still maybe more than one, prefer #card0driver... or first one 
[ -n "$VIDEODRIVER" ] && VIDEODRIVER="$(echo "$VIDEODRIVER" | grep '#card0driver' || echo "$VIDEODRIVER" | head -1)"

#... if no luck, then try Xorg log
[ -z "$VIDEODRIVER" ] && VIDEODRIVER="`grep 'Chipset:' /var/log/Xorg.0.log | head -1`" #110910 #120714
[ -z "$VIDEODRIVER" ] && VIDEODRIVER="`grep -i ' vendor' /var/log/Xorg.0.log | grep -v 'X.Org\|glx' | sed 's%drm%%'`"
[ -z "$VIDEODRIVER" ] && VIDEODRIVER="`grep 'ScreenInit' /var/log/Xorg.0.log`" #110417
[ -z "$VIDEODRIVER" ] && VIDEODRIVER="`grep 'Total Memory' /var/log/Xorg.0.log`"  #110417
[ -z "$VIDEODRIVER" ] && VIDEODRIVER="`grep ' Configured Monitor' /var/log/Xorg.0.log`"  #110615
 
[ -n "$VIDEODRIVER" ] && VIDEODRIVER="`echo $VIDEODRIVER | sed -e 's% #card0driver%%' -e 's%^.*(..) %%' -e 's%Driver %%' -e 's%Module %%' -e 's%vendor=.*%%' | cut -f1 -d'(' | tr 'A-Z' 'a-z' | tr -d ' ' | tr -d ':' | tr -d '"'`" #'geany #111020 #120714

[ "$VIDEODRIVER" = "" ] && VIDEODRIVER="unknown"

if [ "$XSERVER" = "Xorg" ] && [ "`ps -eo comm | grep ^X`" ]; then #110417
 if [ `which xdpyinfo` ]; then
  RANDR="`xdpyinfo | grep -E "version:|dimensions|depth of"`"
 else
  [ `which xrandr` ] && RANDR="`xrandr | grep '*'`"
 fi
fi

if [ -r /etc/LHPVERSION ]; then #100928 Lighthouse
VERSION="`cat /etc/LHPVERSION | tr -d '\n'`" # ex: 5.00-F
else VERSION="`echo $DISTRO_VERSION`" #100928 Generic Pup ex: 500
fi

#Video mode used by Xorg: $VIDEORES  $VIDEODEPTH
echo -e "$APPNAMECAP $APPVERSION - `date +'%a %-e %b %Y'` on $DISTRO_NAME $VERSION - `uname -rsm`
 
Chip description:
$GRAPHICSCHIP
 
X Server: $XSERVER  Driver used: $VIDEODRIVER

$RANDR \n" > "${REPORT_FILE}" #TaZoC #120807

echo -e "$XORGCONF" | cat "${REPORT_FILE}" - /etc/X11/xorg.conf >"${REPORT_FILE}-full" #100928 TaZoC
echo -e "$XORGLOG" | cat - /var/log/Xorg.0.log >> "${REPORT_FILE}-full" #100928 TaZoC

sed -i '/./,/^$/!d' "${REPORT_FILE}-full" # TaZoC strip consecutive blank lines, allows 0 blanks at top, 1 at EOF
cat "${REPORT_FILE}-full" | gzip -f > "${REPORT_FILE}-full.gz" #100928 TaZoC

cat "${REPORT_FILE}" #TaZoC
echo
echo "   ...the above also recorded in /tmp/${USER}/ as report-video," 
echo "and archived with xorg.conf and Xorg.0.log as report-video-full.gz" 

MSG="`cat "${REPORT_FILE}"`"
# ...the above also recorded in /tmp/${USER}/ as report-video, and  
# gzipped with xorg.conf and Xorg.0.log as report-video-full.gz  
#"
if [ "$(ps -eo comm | grep ^X)" -a -z "$(echo "$1" | grep -E '\-q|\-quiet|\-cl|\-nogui')" ]; then #120808 X is running
 MainDialogFunc ## function call
   #Xdialog --title "$APPTITLE $APPVERSION" --wmclass "$APPNAME" --icon "$ICON" \
           --fill --msgbox "$MSG" 0 0
fi
