#!/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-switch.log
prime_settings=/etc/prime-discrete
prime_power=/proc/acpi/bbswitch
force_prime=/etc/force-prime
prime_supported=/usr/bin/prime-supported

# Remove any previous logs
rm -f $LOG

# Check hardware support here
supported="`$prime_supported`"
if [ -z "$supported" ]; then
    echo "Sorry but your hardware configuration is not supported" \
         >> $LOG 2>&1
    exit 0
fi

if lsmod | grep nouveau > /dev/null; then
    # Exit if nouveau is in use
    echo "Sorry but nvidia-prime does not work with the nouveau driver" \
         >> $LOG 2>&1
    exit 0
fi

if [ ! -e $prime_settings ]; then
    echo "No settings for prime can be found in $prime_settings" \
         >> $LOG 2>&1
    exit 0
fi

if ! lsmod | grep bbswitch > /dev/null; then
    opts="`/sbin/get-quirk-options`"
    # Try to load bbswitch
    /sbin/modprobe bbswitch load_state=-1 unload_state=1 "$opts" || true
fi

if [ ! -e $prime_power ]; then
    # If we get here there is no bbswitch module
    echo "No bbswitch can be found" \
         >> $LOG 2>&1
    exit 0
fi

nvidia_status="`cat $prime_power | cut -d " " -f2`"
action="`cat $prime_settings`"
# We support both uppercase and lowercase
action="$(echo "$action" | /usr/bin/tr '[:upper:]' '[:lower:]')"

# Always set up xorg.conf
if [ "$action" = "on" ]; then
    echo "Configuring xorg.conf" \
     >> $LOG 2>&1
    /sbin/prime-xconfig on
    echo "Configuring alternatives" \
     >> $LOG 2>&1
    # Make sure the alternatives are in sync
    /usr/bin/prime-select nvidia
elif [ "$action" = "off" ]; then
    echo "Moving xorg.conf away" \
     >> $LOG 2>&1
    /sbin/prime-xconfig off
    echo "Configuring alternatives" \
     >> $LOG 2>&1
    # Make sure the alternatives are in sync
    /usr/bin/prime-select intel
fi

# Do we need to call bbswitch?
if [ "$action" = "$nvidia_status" ]; then
    echo "No action required to get PRIME to work" \
         >> $LOG 2>&1
    exit 0
fi

if [ "$action" = "on" ]; then
    echo "Enabling the NVIDIA card" \
         >> $LOG 2>&1
    echo "ON" > /proc/acpi/bbswitch
    /sbin/modprobe nvidia || true
    exit 0
elif [ "$action" = "off" ]; then
    echo "Disabling the NVIDIA card" \
         >> $LOG 2>&1
    /sbin/rmmod nvidia || true
    echo "OFF" > /proc/acpi/bbswitch
    exit 0
else
    echo "\"$action\" is not a valid action for PRIME" \
         >> $LOG 2>&1
    exit 0
fi

