#!/bin/bash
# easyvdr-hw.sh
# Hardewareerkennung 
# 0.0.1 maxx
#

#set -x
version=0.0.1
trenn="\n"
#trenn="|"
file=""
php="no"
rcstart="no"
debug="no"

function usage () {
   echo ""
   echo "easyvdr-hw.sh $version"
   echo ""
   echo "Options: -f, -p und -r bitte immer als erste Optionen benutzen! ( wegen meiner Schreibfaulheit ;) )"
   echo
   echo "        -f <Datei>, --file <Datei>  Datei als Eingabedatei für DVB nutzen (gut zum Testen)"
   echo "        -p,  --php                  Als Trennzeichen <br> benutzen"
   echo "        -r,  --rcstart              dmesg snapshot über /etc/init.d/RCStartAfterVDR.d/RCStartPersonal"
   echo "             --debug                Debug Ausgabe (für DVB Erkennung)"
   echo
   echo "        -a,  --audio                Anzeige der Audio Karte"
   echo "        -c,  --cpu                  Anzeige der CPU"
   echo "        -t,  --takt                 Anzeige der CPU-Taktung"
   echo "        -do, --dvbold               Anzeige der DVB / PVR Geräte (alte Methode)"
   echo "        -d,  --dvb                  Anzeige der DVB / PVR Geräte"
   echo "        -h,  --hd                   Anzeige der Festplatten"
   echo "        -o,  --cd                   Anzeige der opt. Laufwerke"
   echo "        -i,  --ip                   Anzeige der IP"
   echo "        -k,  --kernel               Anzeige der Kernel Version"
   echo "        -m,  --mem                  Anzeige der Speichergröße"
   echo "        -n,  --net                  Anzeige der Netzwerkkarte"
   echo "        -v,  --vga                  Anzeige der Grafikkarte"
   echo "        -h,  --help                 Hilfe anzeigen"
   echo
}

function GetParam() {
   while [ $# -gt 0 ]; do
      case "$1" in
         "--audio"	|"-a")	 ShowAudio;
                                 shift;;
         "--cpu"	|"-c")	 ShowCPU;
                                 shift;;
         "--takt"	|"-t")	 ShowCPUTakt;
                                 shift;;
         "--dvbold"	|"-do")	 ShowDVBOld;
                                 shift;;
         "--dvb"	|"-d")	 ShowDVB;
                                 shift;;
         "--hd"		|"-h")	 ShowHD;
                                 shift;;
         "--cd"		|"-o")	 ShowOptLW;
                                 shift;;
         "--kernel"	|"-k")	 ShowKernel;
                                 shift;;
         "--mem"	|"-m")	 ShowMem;
                                 shift;;
         "--net"	|"-n")	 ShowNet;
                                 shift;;
         "--ip"		|"-i")	 ShowIP;
                                 shift;;
         "--vga"	|"-v")	 ShowVGA;
                                 shift;;
         "--php"	|"-p")	 trenn="<br>";
				 php="yes"
                                 shift;;
         "--rcstart"	|"-r")	 rcstart="yes";
                                 shift;;
         "--file"	|"-f")	 shift;
				 file=$1;
				 echo "inputfile=$file" >&2;
                                 shift;;
         "--debug")              debug="yes";
                                 shift;;
         "--help"       |"-h")   usage;
                                 exit;;
         *)	                 echo "Parameter: $1 unbekannt"
                                 usage;
                                 exit;;
      esac
   done
}

function HwInfo() {
   if [ "x$file" = "x" ]; then
      hwinfo $*
   elif [ -f "$file" ]; then
      cat $file
   else
      echo "$file nicht vorhanden. Ende." >&2
      exit 1
   fi
}
function DMesg2RCStart() {
   if [ $(grep -w -e "^dmesg > /tmp/easyvdr_hw_dmesg0.log" /etc/init.d/RCStartAfterVDR.d/RCStartPersonal &>/dev/null; echo $?) = "1"  ]; then
      echo "dmesg > /tmp/easyvdr_hw_dmesg0.log  # für EasyPortal" >> /etc/init.d/RCStartAfterVDR.d/RCStartPersonal
   fi
}
function DMesg() {
   if [ "x$rcstart" = "xyes" ]; then
      DMesg2RCStart
   fi

   if [ ! -f /tmp/easyvdr_hw_dmesg0.log ]; then
      dmesg > /tmp/easyvdr_hw_dmesg0.log   
      cp /tmp/easyvdr_hw_dmesg0.log /tmp/easyvdr_hw_dmesg.log   
   else
      cp /tmp/easyvdr_hw_dmesg0.log /tmp/easyvdr_hw_dmesg.log   
      dmesg >> /tmp/easyvdr_hw_dmesg.log   
   fi

   if [ "x$file" = "x" ]; then
      cat /tmp/easyvdr_hw_dmesg.log
   elif [ -f "$file" ]; then
      cat $file
   else
      echo "$file nicht vorhanden. Ende." >&2
      exit 1
   fi
}

function CutClock() {
   awk '{
	x=$0
	if (substr(x,1,1) == "[" && substr(x,14,1) == "]" )
	   printf "%s\n", substr(x,16);
	else   
	   printf "%s\n", x;
   }'
}

function ShowDVB() {
   DMesg | grep -e DVB -e PVR -e EM8[43]00 | CutClock | sort | uniq |
   awk -v TRENN="$trenn" -v DEBUG=$debug 'FS="['\''()<>:]"{
	i=1;
	split($0,s);
	if (DEBUG == "yes") {
	   printf "s[1]=%s<\n", s[1];
	   printf "s[2]=%s<\n", s[2];
	   printf "s[3]=%s<\n", s[3];
	   printf "s[4]=%s<\n", s[4];
	   printf "s[5]=%s<\n", s[5];
	   printf "s[6]=%s<\n", s[6];
	}
	if (s[i+1] == " registering new adapter " || substr(s[i+1],0,19) == " Initialized card #" || s[i+1] == " Initialized card #" || s[i] == "b2c2-flexcop" && s[i+1] == " initialization of ") {
		if (s[i+2] != "FlexCop Digital TV device") {
			if (++n>1)
				printf TRENN;
			printf s[i+2];
		}
	}
	else {
		if (s[3] == " V4L in-tree version\r") {
			if (++n>1)
				printf TRENN;
			printf s[2];
		}
		else if  (s[1] == "em8300" || s[1] == "em8400") {
			if (++n>1)
				printf TRENN;
			printf ("RealMagiq / Sigma %s", s[1]);
		}
	}
   }'
   if [ x"$php" = "xno" ]; then
      echo
   fi
}

function ShowDVBOld() {
   HwInfo | grep -e DVB -e PVR | sort | uniq |
   awk -v TRENN="$trenn" 'FS="['\''()<>:]"{
	split($0,s);
	#printf "s[4]=%s,s[5]=%s,s[6]=%s\n", s[4],s[5],s[6];
	if (s[4] == " registering new adapter " || s[4] == " Initialized card #0" || s[3] == "b2c2-flexcop" && s[4] == " initialization of ") {
		if (s[5] != "FlexCop Digital TV device") {
			if (++n>1)
				printf TRENN;
			printf s[5];
		}
	}
   }'
   if [ x"$php" = "xno" ]; then
      echo
   fi
}

function ShowHD() {
   hwinfo --disk | grep "Model:" |
   awk -v TRENN="$trenn" 'FS="[\"]"{
      split($0,s);
      if (++n>1)
         printf TRENN;
      printf s[2];
   }'
   if [ x"$php" = "xno" ]; then
      echo
   fi
}

function ShowOptLW() {
   hwinfo --cdrom | grep "Model:" |
   awk -v TRENN="$trenn" 'FS="[\"]"{
      split($0,s);
      if (++n>1)
         printf TRENN;
      printf s[2];
   }'
   if [ x"$php" = "xno" ]; then
      echo
   fi
}

function ShowNet() {
   hwinfo --netcard | grep "Model:" |
   awk 'FS="[\"]"{
      split($0,s);
      if (++n==1)
         printf "%s", s[2]
   }'
   if [ x"$php" = "xno" ]; then
      echo
   fi
}

function ShowAudio() {
   hwinfo --sound | grep "Model:" |
   awk 'FS="[\"]"{
      split($0,s);
      if (++n==1)
         printf "%s", s[2]
   }'
   if [ x"$php" = "xno" ]; then
      echo
   fi
}


function ShowIP() {
   (/sbin/ifconfig eth0; /sbin/ifconfig eth1; /sbin/ifconfig wlan0) 2>/dev/null | grep "inet Adresse" |
   awk '{ print $2 }' | awk -F: '
   {
      if (++n>1)
         printf TRENN;
      print $2
   }'
}
function ShowCPU() {
   less /proc/cpuinfo | grep 'model name' | awk -F: '{ print $2 }'
}
function ShowCPUTakt() {
   hwinfo --cpu | grep Clock: | awk -F: '{ print $2 }'
}
function ShowMem() {
   less /proc/meminfo | grep 'MemTotal:' | awk -F: '{ print $2 }'
}
function ShowKernel() {
   uname -r
}
function ShowVGA() {
   lspci | grep VGA | awk -F: '{ print $3 }'
}
#function ShowAudio() {
#   lspci | grep 'Multimedia audio' | awk -F: '{ print $3 }'
#}
GetParam $*

