#!/bin/sh
# Copyright (C) 2006-2011  mariodebian at gmail
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

if [ -f /tmp/vga_vendor ]; then
  VENDOR=$(awk -F":" '{print $1}' /tmp/vga_vendor)
  MODEL=$(awk -F":" '{print $2}' /tmp/vga_vendor)
else
  #return /sys/bus/pci/devices/0000:06:05.0/class:0x030000
  VGA_DEVPATH=$(grep 0x030000 /sys/bus/pci/devices/*/class | awk -F"/class" '{print $1}')

  VENDOR=$(cat $VGA_DEVPATH/vendor | sed -e 's|0x||' | tr [a-z] [A-Z])
  MODEL=$(cat $VGA_DEVPATH/device | sed -e 's|0x||' | tr [a-z] [A-Z])

  #VENDOR=$(lspci -nn| grep VGA | grep -E -o '[0-9a-z]{4}\:[0-9a-z]{4}' | tr [a-z] [A-Z] | awk -F":" '{print $1}')
  echo "${VENDOR}:${MODEL}" > /tmp/vga_vendor
fi

if [ "$1" = "vendormodel" ]; then
  echo "${VENDOR}:${MODEL}"
  exit 0
fi

if [ "$1" != "" ]; then
  if [ "$(echo $1| tr [a-z] [A-Z])" = "$VENDOR" ]; then
    echo "1"
  else
    echo "0"
  fi
  exit 0
fi

echo $VENDOR
