#!/bin/bash
# STEAM DE - Script for starting Steam at login
# Copyright (C) 2012  Thomaz de Oliveira dos Reis <thor27@gmail.com>
# Modified, 2019, Thomas Castleman <contact@draugeros.ml>
#
# 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.

#set -Ee

## Global vars
export XFWM4_PID
export STEAM_DE_PID=$$
export WM_NAME

## Define logging function
/bin/echo "Start Steam Session - $(/bin/date)" > /tmp/steam-de."$UID".log
INFO () {
        /bin/echo "INFO: $*" | /usr/bin/tee -a /tmp/steam-de."$UID".log;
}

WARN () {
        /bin/echo "WARN: $*" | /usr/bin/tee -a /tmp/steam-de."$UID".log;
}

ERRO () {
        /bin/echo "ERRO: $*" | /usr/bin/tee -a /tmp/steam-de."$UID".log; exit 1;
}

cmd_exist () {
        command -v "$1" &> /dev/null;
}

get_path () {
        command -v "$1"
}

wait_steam_shutdown () {
        "$STEAM" -shutdown &
        count=0
        while [ -d /proc/"$OLD_STEAM_PID" ]; do
            /bin/sleep 0.1
            ((count++==100)) && break
        done
}

steam_stopped () {
    if [ -f "$HOME"/.steampid ]; then
            read OLD_STEAM_PID < "$HOME"/.steampid
            if [ ! -d /proc/"$OLD_STEAM_PID" ]; then
                unset OLD_STEAM_PID
                /bin/rm -f "$HOME"/.steampid
            else
                wait_steam_shutdown $1
            fi
    fi
    PIDS_TO_KILL=(
        $OLD_STEAM_PID
        $(/bin/pidof steam)
        $(/bin/pidof steam.sh)
        $(/bin/pidof MainThrd)
    )

    ((${#PIDS_TO_KILL[@]} == 0)) && return

    # Stop all running steam process
    INFO "Pids to /bin/kill: ${PIDS_TO_KILL[*]}"
    for pid in "${PIDS_TO_KILL[@]}"; do
            while ((PID > 1)) && [ -d "/proc/$PID" ]; do
                    /bin/kill "$pid" &> /dev/null && /bin/sleep 1
                    /bin/kill -s 9 $pid &> /dev/null
            done
    done
}

disable_joysticks () {
    /usr/bin/find /dev/input -name "js*" | \
    while read -r dev; do
        # Get the name of the joystick model
        name="$(/sbin/udevadm info --query=property --name=$dev | /bin/grep ID_VENDOR_ENC | /usr/bin/cut -c15- -) &(/sbin/udevadm info --query=property --name=$dev | /bin/grep ID_MODEL_ENC | /usr/bin/cut -c14- -)"
        # Escape \ sequences
        name="`/bin/echo -e "$name"`"
        # Disable the joystick
        /usr/bin/xinput --list | /bin/grep "$name" | /bin/grep -Pwo "id=[0-9]*" | /bin/grep -Pwo "[0-9]*" | /usr/bin/xargs /usr/bin/xinput --disable
    done
}

kill_OpenBox () {
        openbox --exit
}

kill_XFWM4 () {
        [ -z "$XFWM4_PID" ] && return
        [ ! -d "/proc/$XFWM4_PID" ] && return
        /bin/kill "$XFWM4_PID" &> /dev/null && /bin/sleep 1
        /bin/kill -s 9 "$XFWM4_PID" &> /dev/null
}

steam_de_exit () {
        INFO "Steam-DE cooling"
        steam_stopped
        case $WM_NAME in
                xfwm4) kill_XFWM4 ;;
                openbox) kill_OpenBox ;;
        esac
        INFO "Steam-DE stopped"
        exit 0
}

## Main
INFO "Initialization"
# Vars to switch logic
# if supported things found
export STEAM STEAM_NATIVE
export OPTIRUN PRIMUSRUN
export NVIDIA_SETTINGS
export GNOME_SETTINGS_DAEMON
export PREFIX=""
export PROGRAM=""
export STEAM_ARGS="-tenfoot -enableremotecontrol"

if cmd_exist steam; then
        STEAM="$(get_path steam)"
        INFO "Check: steam found"
else
        ERRO "Steam not found in PATH"
fi

if cmd_exist xfwm4; then
        WM_NAME=xfwm4
        STATUS="found"
else
        STATUS="missing"
fi

#       Checking xfwm4 status
INFO "Check: xfwm4 $STATUS"
if cmd_exist openbox; then
        WM_NAME=openbox
        STATUS="found"
else
        STATUS="missing"
fi

#       checking openbox status
INFO "Check: openbox $STATUS"
if [ -z "$WM_NAME" ]; then
        ERRO "Can't find supported WM in PATH"
fi

if cmd_exist steam-native; then
        STEAM_NATIVE="$(get_path steam-native)"
        STATUS="found"
else
        STATUS="missing"
fi

#       checking steam-native status
INFO "Check: steam-native $STATUS"
if cmd_exist optirun; then
        OPTIRUN="$(get_path optirun)"
        STATUS="found"
else
        STATUS="missing"
fi

#checking optirun status
INFO "Check: optirun $STATUS"
if cmd_exist primusrun; then
        PRIMUSRUN="$(get_path primusrun)"
        STATUS="found"
else
        STATUS="missing"
fi

#check primusrun status
INFO "Check: primusrun $STATUS"
if cmd_exist /usr/bin/nvidia-settings; then
        NVIDIA_SETTINGS="$(get_path /usr/bin/nvidia-settings)"
        STATUS="found"
else
        STATUS="missing"
fi

#check /usr/bin/nvidia-settings status
INFO "Check: /usr/bin/nvidia-settings $STATUS"
if cmd_exist gnome-settings-daemon; then
        GNOME_SETTINGS_DAEMON="$(get_path gnome-settings-daemon)"
        STATUS="found"
else
        STATUS="missing"
fi

#check gnome-settings-daemon status
INFO "Check: gnome-settings-daemon $STATUS"
if [ ! -z "$STEAM_NATIVE" ]; then
        INFO "Use steam-native instead of steam"
        PROGRAM="$STEAM_NATIVE"
else
        INFO "Use steam instead of steam-native"
        PROGRAM="$STEAM"
fi

trap steam_de_exit SIGINT SIGTERM EXIT

if [ ! -z "$OPTIRUN" ] || [ ! -z "$PRIMUSRUN" ]; then
        if [ ! -z "$PRIMUSRUN" ]; then
                INFO "Use primusrun instead of optirun"
                export vblank_mode=0
                PREFIX="$OPTIRUN -b primus"
        else
                INFO "Use optirun instead of primusrun"
                PREFIX="$OPTIRUN"
        fi
fi

# Disable /usr/bin/xinput for joysticks
if cmd_exist /usr/bin/xinput; then
        INFO "Disable /usr/bin/xinput for joysticks"
        disable_joysticks
fi

case $WM_NAME in
        xfwm4)
                INFO "Start XFWM4 Window manager"
                /usr/bin/xfwm4 --replace --daemon --compositor=off
                XFWM4_PID="$(/bin/pidof xfwm4)"
        ;;
        openbox)
                INFO "Start OpenBox Window manager"
                openbox --replace &
        ;;
esac


if [ ! -z "$NVIDIA_SETTINGS" ] || [ ! -z "$GNOME_SETTINGS_DAEMON" ]; then
        INFO "Start basic video settings process"
        if [ ! -z "$NVIDIA_SETTINGS" ]; then
                INFO "Run /usr/bin/nvidia-settings"
                /usr/bin/nvidia-settings -l
        fi
        if [ ! -z "$GNOME_SETTINGS_DAEMON" ]; then
                INFO "Run gnome-settings-daemon"
                gnome-settings-daemon &
        fi
fi

INFO "Ensure that steam is not running"
steam_stopped

export SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS=0
INFO "Disable: DPMS"
/usr/bin/xset -dpms

INFO "Disable: screen blanking"
/usr/bin/xset s off

INFO "Start Steam Manager"
/usr/bin/python3 /usr/share/steam-de/steamde.py 2>&1 | /usr/bin/tee -a /tmp/steam-de.$UID.log

exit 0
