#!/bin/sh
#(c) Copyright 2006 Barry Kauler www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v3.01 BK 11oct07: bugfix, crash when choose usb button.
#BK v3.98: improve create mono font in status log.

MODITEMS=""
for ONEMOD in `lsmod | grep -v '^Module ' | cut -f 1 -d ' ' | tr '\n' ' '`
do
 MODITEMS="$MODITEMS<item>$ONEMOD</item>"
done

KERNVER="`uname -r`"
#PCINUMS="`lspci -n | tr -s " " | cut -f 4 -d " " | tr "\n" " "`"
PCIMODINFO="`cat /lib/modules/modules.pcimap.$KERNVER | tr -s " " | cut -f 1-3 -d " "`"

echo -n "" > /tmp/chipnummod.txt
if [ "`which scanpci`" = "" ];then
 echo "PCI scan utility scanpci not available. This must be a
cutdown Puppy (scanpci is part of the Xorg package).
Simplified information only is displayed here:
"  >> /tmp/chipnummod.txt
 elspci -l  >> /tmp/chipnummod.txt
else
 scanpci |
 while read ONELINE
 do
  VENDOR="`echo -n "$ONELINE" | grep '^pci' | grep -o 'vendor .*' | cut -f 2 -d ' ' | cut -f 2 -d 'x'`"
  CHIPNO="`echo -n "$ONELINE" | grep '^pci' | grep -o 'device .*' | cut -f 2 -d ' ' | cut -f 2 -d 'x'`"
  if [ "$VENDOR" ];then
   read NEXTLINE
   CHIPDESC="`echo -n "$NEXTLINE" | cut -f 2 -d '[' | cut -f 2 -d ']'`"
   LVENDOR="0x0000$VENDOR"
   LCHIPNO="0x0000$CHIPNO"
   APATTERN="$LVENDOR $LCHIPNO"
   FNDMOD="`echo "$PCIMODINFO" | grep "$APATTERN" | head -n 1`"
   MODULE="unknown"
   [ ! "$FNDMOD" = "" ] && MODULE="`echo -n "$FNDMOD" | cut -f 1 -d " "`"
   if [ "$MODULE" = "unknown" ];then
    #find out if it is a usb module...
    UPATTERN=" ${VENDOR}:${CHIPNO} "
    [ ! "`elspci -l | grep -i "$UPATTERN" | grep '0C0300'`" = "" ] && MODULE="uhci-hcd"
    [ ! "`elspci -l | grep -i "$UPATTERN" | grep '0C0310'`" = "" ] && MODULE="ohci-hcd"
    [ ! "`elspci -l | grep -i "$UPATTERN" | grep '0C0320'`" = "" ] && MODULE="ehci-hcd"
   fi
   echo "DESCRIPTION: $CHIPDESC" >> /tmp/chipnummod.txt
   echo "VENDOR: $VENDOR  DEVICE: $CHIPNO  KERNEL MODULE: $MODULE" >> /tmp/chipnummod.txt
   echo  >> /tmp/chipnummod.txt
  fi
 done
fi

[ ! -s /tmp/chipnummod.txt ] && echo "No PCI interfaces" > /tmp/chipnummod.txt #v3.01

#     <action>/tmp/formatfunc.sh 30</action>

#v2.17 usb devices...
USBSECTION=""
echo -n "" > /tmp/usbchipnummod.txt
if [ -e /proc/bus/usb ];then

 USBMODINFO="`cat /lib/modules/modules.usbmap.$KERNVER | tr -s " " | cut -f 1,3,4 -d " " | tr [A-Z] [a-z]`"
 #...returns module-name vendor-id product-id

 #format so paragraphs can be separated...
 #DELPARAMS="`cat /proc/bus/usb/devices | tr -s ' ' | sed -e 's%^$%|||||%g'`"


  USBNUMS="`cat /proc/bus/usb/devices | grep '^P: ' | tr -s ' ' | cut -f 2-3 -d ' ' | tr ' ' '|' | tr [A-Z] [a-z] | tr '\n' ' '`"
  for ONENUM in $USBNUMS
  do
   VENDOR="`echo -n "$ONENUM" | cut -f 1 -d '|' | cut -f 2 -d '='`"
   PRODUCT="`echo -n "$ONENUM" | cut -f 2 -d '|' | cut -f 2 -d '='`"
   ONEUSB="0x${VENDOR} 0x${PRODUCT}"
   
   [ "$ONEUSB" = "0x0000 0x0000" ] && continue
   
   YESPARA=no
   USBMODULE=""
   rm -f /tmp/usbmodulexxx
   echo -n "" > /tmp/usbdevdescr
   cat /proc/bus/usb/devices | tr -s ' ' |
   while read ONELINE
   do
    zPATTERN="Vendor=${VENDOR} ProdID=${PRODUCT}"
    [ "`echo -n "$ONELINE" | grep "$zPATTERN"`" != "" ] && YESPARA=yes
    if [ "$YESPARA" = "yes" ];then
     xDRIVER="`echo -n "$ONELINE" | grep '^I:' | grep -o ' Driver=.*' | cut -f 2 -d '='`"
     [ "$xDRIVER" != "" ] && echo -n "$xDRIVER" > /tmp/usbmodulexxx
     xDESCR="`echo -n "$ONELINE" | grep '^S:' | cut -f 2-20 -d ' '`"
     [ "$xDESCR" != "" ] && echo "$xDESCR" >> /tmp/usbdevdescr
     [ "$ONELINE" = "" ] && break
    fi
   done
   [ -f /tmp/usbmodulexxx ] && USBMODULE="`cat /tmp/usbmodulexxx`"
   
   [ "$USBMODULE" = "" ] && USBMODULE="`echo "$USBMODINFO" | grep "$ONEUSB" | cut -f 1 -d ' ' | head -n 1`"

   #echo "DESCRIPTION: $CHIPDESC" >> /tmp/usbchipnummod.txt
   cat /tmp/usbdevdescr >> /tmp/usbchipnummod.txt
   echo "VendorID=$VENDOR  ProductID=$PRODUCT  KERNEL-MODULE=$USBMODULE" >> /tmp/usbchipnummod.txt
   echo  >> /tmp/usbchipnummod.txt
   
  done

[ ! -s /tmp/usbchipnummod.txt ] && echo "No plugged-in USB devices" > /tmp/usbchipnummod.txt #v3.01

  USBSECTION="
  <frame USBデバイス>
  <hbox>
   <text><label>USBはPCIインターフェースの一部ですが、他のプラグインUSBデバイスの情報を知るにはこのボタンをクリックして下さい:</label></text>
   <vbox>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/usb16.xpm</input>
     <action>cp -f /tmp/usbchipnummod.txt /tmp/hwproberesult.txt</action>
     <action>refresh:INFO</action>
    </button>
   </vbox>
  </hbox>
  </frame>
"

fi

MAINDIALOG="
<wtitle>Puppy hardware-interfaces information</wtitle>
<hbox>
 <vbox>
  
  <frame カーネルモジュール>
  <hbox>
   <text><label>起動時, パピーはどんなカーネルドライバ（モジュール）をロードするか決める為にハードウェアインターフェースを調べてロードします。ロードしたモジュールの情報についてはこのボタンをクリックして下さい:</label></text>
   <vbox>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/module16.xpm</input>
     <action>eval /tmp/hwprobelsmod</action>
     <action>refresh:INFO</action>
    </button>
   </vbox>
   <vbox>
    <text><label>ロードした各モジュールの詳細:</label></text>
    <combobox>
     <variable>MODCOMBO</variable>
      $MODITEMS
    </combobox>
   </vbox>
   <vbox>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/module16.xpm</input>
     <action>eval /tmp/hwprobemodinfo \$MODCOMBO</action>
     <action>refresh:INFO</action>
    </button>
   </vbox>
  </hbox>
  </frame>

  <frame PCIインターフェース>
  <hbox>
   <text><label>PC内の大部分のハードウェアインターフェースは、PCIデバイスです。全てのPCIインターフェースをスキャンするには、このボタンをクリックして下さい:</label></text>
   <vbox>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/Card.xpm</input>
     <action>cp -f /tmp/chipnummod.txt /tmp/hwproberesult.txt</action>
     <action>refresh:INFO</action>
    </button>
   </vbox>
  </hbox>
  </frame>

  ${USBSECTION}
  
  <frame 結果>
   <edit>
    <variable>INFO</variable>
    <input file>/tmp/hwproberesult.txt</input>
    <width>450</width>
    <height>300</height>
   </edit>
  </frame>
  <hbox>     
   <button>
    <label>終了</label>
    <action>EXIT=EXIT</action>
   </button>
  </hbox>
 </vbox>
</hbox>
"

#  <frame Other interfaces>
#  <hbox>
#   <text><label>Information about USB interfaces:</label></text>
#   <vbox>
#    <button>
#     <input file>/usr/local/lib/X11/mini-icons/usb16.xpm</input>
#     <action>usbview &</action>
#    </button>
#   </vbox>
#  </hbox>
#  </frame>


if [ "`which scanpci`" = "" ];then
 echo 'echo "This must be a cutdown Puppy that does not have the scanpci utility
(which is part of the Xorg package). So, cannot give a detailed
report on the PCI bus. Instead can display this summary only. The
most useful information here is the vendor:chip numbers (column-4):
" >/tmp/hwproberesult.txt
lspci -n >>/tmp/hwproberesult.txt'  >/tmp/hwprobescanpci
else
 #echo '#!/bin/sh' >/tmp/hwprobescanpci.sh
 echo 'scanpci >/tmp/hwproberesult.txt' >/tmp/hwprobescanpci
fi
chmod 755 /tmp/hwprobescanpci

echo 'lsmod >/tmp/hwproberesult.txt' >/tmp/hwprobelsmod
chmod 755 /tmp/hwprobelsmod
echo 'modinfo $1 > /tmp/hwproberesult.txt 2>&1' >/tmp/hwprobemodinfo
chmod 755 /tmp/hwprobemodinfo

#v3.98 improved...
##i don't know enough about themes to do this any better...
#[ -f /etc/gtk-2.0/gtkrc ] && cp -f /etc/gtk-2.0/gtkrc /tmp/gtkrc-pupscan
#cp -f /etc/gtk-2.0/gtkrcMONOSPACED-APPEND /etc/gtk-2.0/gtkrc
#echo '#!/bin/sh
# sleep 2
# if [ -f /tmp/gtkrc-pupscan ];then
#  cp -f /tmp/gtkrc-pupscan /etc/gtk-2.0/gtkrc
# else
#  rm /etc/gtk-2.0/gtkrc
# fi
#' > /tmp/delaygtkrc.sh
#chmod 755 /tmp/delaygtkrc.sh
#/tmp/delaygtkrc.sh &
echo 'style "specialmono"
{
  font_name="Mono 12"
}

class "GtkText*" style "specialmono"' > /tmp/gtkrc_mono
export GTK2_RC_FILES=/tmp/gtkrc_mono:/root/.gtkrc-2.0


RETSTRING="`echo "$MAINDIALOG" | gtkdialog2 --stdin`"
