#!/bin/bash

usage() {
    cat <<EOF
Usage: $PROG --mode|--reset|--help
  --mode powersaving|performance
    powersaving: shutdown dGPU, only use iGPU
    performance: use dGPU for rendering, iGPU and dGPU for display
  --reset
    reset the script changes
  --ispx
    echo "true" if the platform can enable PX, otherwise echo "false"
EOF
}

powersaving_conf() {
    cat <<EOF
Section "ServerFlags"
        Option  "AutoAddGPU" "off"
EndSection

Section "Device"
        Identifier "Intel"
        Driver "intel"
EndSection

Section "Files"
    ModulePath "/usr/lib/xorg/modules"
EndSection
EOF
}

performance_conf() {
    cat <<EOF
Section "Device"
        Identifier "Amd"
        Driver "amdgpu"
        Option "ShareFD" "on"
EndSection

Section "Device"
        Identifier "Intel"
        Driver "modesetting"
        Option "RenderPath" "amdgpu"
        Option "RenderDriver" "amdgpu"
EndSection

Section "Screen"
        Identifier "Screen0"
        Device "Intel"
        GPUDevice "Amd"
EndSection
EOF
}

ld_so_conf() {
    cat <<EOF
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/mesa
/usr/lib/x86_64-linux-gnu/mesa-egl

/lib/i386-linux-gnu
/usr/lib/i386-linux-gnu
/usr/lib/i386-linux-gnu/mesa
/usr/lib/i386-linux-gnu/mesa-egl
EOF
}

nautilus_autostart_delay_conf() {
    cat <<EOF
[Desktop Entry]
Type=Application
Name=Files
Exec=nautilus -n
OnlyShowIn=GNOME;Unity;
AutostartCondition=GSettings org.gnome.desktop.background show-desktop-icons
NoDisplay=true
X-GNOME-Autostart-Delay=6
X-Ubuntu-Gettext-Domain=nautilus
EOF
}

nautilus_autostart_default_conf() {
    cat <<EOF
[Desktop Entry]
Type=Application
Name=Files
Exec=nautilus -n
OnlyShowIn=GNOME;Unity;
AutostartCondition=GSettings org.gnome.desktop.background show-desktop-icons
NoDisplay=true
X-Ubuntu-Gettext-Domain=nautilus
EOF
}

set_mode() {
    if [ x$1 = xpowersaving ]; then
	mkdir -p $X11_CONFDIR
	powersaving_conf > $X11_CONFDIR/$X11_CONFFILE
	ld_so_conf > $LD_SO_CONFDIR/$LD_SO_CONFFILE
	if [ -f $XDG_AUTOSTART_CONF_DIR/$NAUTILUS_AUTOSTART_CONF ]; then
	    rm -f $XDG_AUTOSTART_CONF_DIR/$NAUTILUS_AUTOSTART_CONF
	fi
	nautilus_autostart_default_conf > $XDG_AUTOSTART_CONF_DIR/$NAUTILUS_AUTOSTART_CONF
    elif [ x$1 = xperformance ]; then
	mkdir -p $X11_CONFDIR
	performance_conf > $X11_CONFDIR/$X11_CONFFILE
	rm -f $LD_SO_CONFDIR/$LD_SO_CONFFILE
	if [ -f $XDG_AUTOSTART_CONF_DIR/$NAUTILUS_AUTOSTART_CONF ]; then
	    rm -f $XDG_AUTOSTART_CONF_DIR/$NAUTILUS_AUTOSTART_CONF
	fi
	nautilus_autostart_delay_conf > $XDG_AUTOSTART_CONF_DIR/$NAUTILUS_AUTOSTART_CONF
    else
	usage
	exit
    fi

    ldconfig

    echo "Switch to $1 mode success, please re-login to make settings take effect"
}

reset() {
    rm -f $LD_SO_CONFDIR/$LD_SO_CONFFILE $X11_CONFDIR/$X11_CONFFILE
    ldconfig

    echo "Reset success, please re-login to make settings take effect"
}

ispx() {
    if lspci | grep 'VGA' | grep Intel 1>/dev/null 2>/dev/null && \
       lspci | grep 'Display controller\|VGA' | grep AMD 1>/dev/null 2>/dev/null; then
	echo true
    else
	echo false
    fi
}

need_root() {
    if [ "$EUID" -ne 0 ]; then
	echo "Please run as root"
	exit 1
    fi
}

X11_CONFDIR="/etc/X11/xorg.conf.d"
X11_CONFFILE="01-amdgpu-pro-px.conf"
LD_SO_CONFDIR="/etc/ld.so.conf.d"
LD_SO_CONFFILE="10-amdgpu-pro-px.conf"
XDG_AUTOSTART_CONF_DIR="/etc/xdg/autostart"
NAUTILUS_AUTOSTART_CONF="nautilus-autostart.desktop"

PROG=${0##*/}
case "$1" in
    --help)
	usage; exit 0;;
    --mode)
	need_root
	set_mode $2
	exit $?
	;;
    --reset)
	need_root
	reset
	exit $?
	;;
    --ispx)
	ispx
	exit $?
	;;
    *)    usage >&2; exit 1;;
esac

