#!/bin/bash

###########################
## Program: parlink.sh
##
## Description: Parsec Wrapper
## 
## MIT License
##
## Copyright (c) 2019 Justin Moore
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
###########################
# Colors
RED='\033[0;31m'
LRED="\033[1;31m"
BLUE="\033[0;34m"
LBLUE="\033[1;34m"
GREEN="\033[0;32m"
LGREEN="\033[1;32m"
YELLOW="\033[1;33m"
CYAN="\033[0;36m"
LCYAN="\033[1;36m"
PURPLE="\033[0;35m"
LPURPLE="\033[1;35m"
BWHITE="\e[1m"
NC='\033[0m' # No Color

# Global Vars
PARSEC_BIN="/usr/bin/parsecd"
PARSEC_ARGS="app_daemon=1"
PARSEC_URL="https://parsecgaming.com"
PARSEC_PROCESS=$(ps -A | grep -c parsecd)
AUTO_RESTART=true
###########################################
set_video_driver(){
	declare -a driver_name

	driver_name=(
	"iHD" 
	"i965" 
	"nouveau" 
	"vdpau" 
	"radeonsi" 
	"fglrx")

	
	CARD_ID="${1}"
	if [[ ${CARD_ID} = "nvidia" ]];then
		echo "Setting LIBVA_DRIVER_NAME to: ${driver_name[2]}"
		export LIBVA_DRIVER_NAME=${driver_name[2]}

	elif [[ ${CARD_ID} = "catalyst" ]];then
		echo "Setting LIBVA_DRIVER_NAME to: ${driver_name[5]}"
		export LIBVA_DRIVER_NAME=${driver_name[5]}

	elif [[ "${CARD_ID}" = "intel-modern" ]];then
		echo "Setting LIBVA_DRIVER_NAME to: ${driver_name[0]}"
		export LIBVA_DRIVER_NAME=${driver_name[0]}

	elif [[ "${CARD_ID}" = "intel-old" ]];then
		echo "Setting LIBVA_DRIVER_NAME to: ${driver_name[1]}"
		export LIBVA_DRIVER_NAME=${driver_name[1]}

	elif [[ "${CARD_ID}" = "radeon" ]];then
		echo "Setting LIBVA_DRIVER_NAME to: ${driver_name[4]}"
		export LIBVA_DRIVER_NAME=${driver_name[4]}

	elif [[ -z "${CARD_ID}" ]];then

		echo "Autodetecting Driver..."

	elif [[ "${CARD_ID}" = "-h" ]];then
		echo -e "${LRED}Accepted Options:\n
	intel-old  : (uses i965 driver)
	intel-modern : (uses iHD driver)
	nvidia : (uses vdpau driver)
	nouveau : (uses nouveau driver)
	radeon: (uses the radeonsi driver)
	catalyst: (uses fglx driver)${NC}"
	exit 1
	fi
}

start_parsec(){
	while [[ ${PARSEC_PROCESS} != "1" ]];do
		if [[ ! -f ${PARSEC_BIN} ]];then
			xdg-open ${PARSEC_URL}
		fi
		
		if [[ "${AUTO_RESTART}" = false  ]];then
			${PARSEC_BIN} ${PARSEC_ARGS}
			exit

		else
			${PARSEC_BIN} ${PARSEC_ARGS}
		fi
	done
}


case "${1}" in

	-c)
		PARSEC_ARGS="%u"

		if [[ ! -z "${2}" ]];then
			set_video_driver "${2}"
			start_parsec 
		else
			start_parsec 
		fi
	;;

	*)
		set_video_driver "${1}"
		start_parsec
	;;
esac


