#!/bin/sh
#
# Copyright 2013 Canonical Ltd.
#
# Author: Alberto Milone <alberto.milone@canonical.com>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
LOG=/var/log/prime-supported.log
force_prime=/etc/force-prime

if [ -n "$1" ]; then
    LOG="$1"
fi

# Remove any previous logs
# as long as the log is an actual
# file. In some cases we may pass
# it /dev/stdout which is a link
if [ -f $LOG ]; then
    rm -f $LOG
fi

# We only support laptops by default,
# you can override this check by creating
# the /etc/force-prime file
if [ -f "$force_prime" ]; then
    echo "Force mode enabled. Skipping laptop check" >> $LOG 2>&1
else
    if [ -n "`ls -A /sys/class/power_supply/`" ] && \
       [ -d "/proc/acpi/button/lid" ]; then
        echo "Laptop detected" >> $LOG 2>&1
    else
        echo "Sorry but we only support laptops" >> $LOG 2>&1
        exit 0
    fi
fi

# We only support intel + nvidia
cards="$(lspci -n)"
# the different classes as per pci.ids
# Syntax:
# C class   class_name
#   subclass    subclass_name       <-- single tab
#       prog-if  prog-if_name   <-- two tabs
#C 03  Display controller
#    00  VGA compatible controller
#        00  VGA controller
#        01  8514 controller
#    01  XGA compatible controller
#    02  3D controller
#    80  Display controller
gfx_300="$(echo "$cards" | grep 300)"
gfx_301="$(echo "$cards" | grep 301)"
gfx_302="$(echo "$cards" | grep 302)"
gfx_380="$(echo "$cards" | grep 380)"

has_nvidia=false
has_intel=false

for gfx_class in "$gfx_300" "$gfx_301" "$gfx_302" "$gfx_380"; do
    if echo "$gfx_class" | egrep -q "8086"; then
      # Intel detected
      has_intel=true
    fi
    if echo "$gfx_class" | egrep -q "10de"; then
      # Nvidia detected
      has_nvidia=true
    fi
done

if ! $has_intel || ! $has_nvidia; then
    echo "Sorry we only support Intel+Nvidia" \
         >> $LOG 2>&1
    exit 0
fi

echo "yes"
