#!/bin/bash

cd /

while :
do

# Set version of the Regata OS
version=19.1.62

kmsg=$(grep -r LANG /home/*/.config/plasma-localerc)
echo $kmsg

if [[ $kmsg == *"pt"* ]]; then

	kmsg2=$(grep -r Variant=Versão /etc/xdg/kcm-about-distrorc)
	echo $kmsg2

	if [[ $kmsg2 == *"Variant=Versão"* ]]; then
		sed -i 's/$kmsg/Variant=Versão $version/' /etc/xdg/kcm-about-distrorc
	else
		echo Variant=Versão $version >> /etc/xdg/kcm-about-distrorc
	fi

	kmsg2=$(grep -r Variant=Version /etc/xdg/kcm-about-distrorc)
	echo $kmsg2

	if [[ $kmsg2 == *"Variant=Version"* ]]; then
		sed -i 's/$kmsg//' /etc/xdg/kcm-about-distrorc
	fi

else

	kmsg2=$(grep -r Variant=Version /etc/xdg/kcm-about-distrorc)
	echo $kmsg2

	if [[ $kmsg2 == *"Variant=Version"* ]]; then
		sed -i 's/$kmsg/Variant=Version $version/' /etc/xdg/kcm-about-distrorc
	else
		echo Variant=Version $version >> /etc/xdg/kcm-about-distrorc
	fi

	kmsg2=$(grep -r Variant=Versão /etc/xdg/kcm-about-distrorc)
	echo $kmsg2

	if [[ $kmsg2 == *"Variant=Versão"* ]]; then
		sed -i 's/$kmsg//' /etc/xdg/kcm-about-distrorc
	fi

fi

# Fix repos
#Google
#wget --no-check-certificate -O /usr/share/regataos/linux_signing_key.pub https://dl.google.com/linux/linux_signing_key.pub
#if test -e /usr/share/regataos/linux_signing_key.pub ; then
#  zypper clean -a
#  rpm --import /usr/share/regataos/linux_signing_key.pub
#  zypper --gpg-auto-import-keys ref
#fi

# Fix regataosprime executable
if test ! -e /opt/magma/regataosprime ; then
	cp -f /opt/magma/magma /opt/magma/regataosprime
fi
ln -s /opt/magma/regataosprime /usr/bin/regataosprime

# Fix Kworker consuming a lot of CPU
echo "disable" > "/sys/firmware/acpi/interrupts/gpe06"

# Fix Insync and possibly other programs
if test ! -e /etc/pki/tls/certs/ca-bundle.crt ; then
	mkdir -p /etc/pki/tls/certs
	ln -s /etc/ssl/ca-bundle.pem /etc/pki/tls/certs/ca-bundle.crt
fi

# Fix pulseaudio
fix_pulse=$(cat /etc/pulse/system.pa)
if [[ $fix_pulse != *"load-module module-allow-passthrough"* ]]; then
	echo "load-module module-allow-passthrough" >> /etc/pulse/system.pa
fi

# Detect hybrid graphics
#Detect "device 2" and configure xorg
device=$(inxi -G | egrep -i "Card-2|Device-2")

if [[ $device == *"AMD"* ]]; then
	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    rm -f /etc/X11/xorg.conf.d/20-intel.conf
    rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf

elif [[ $device == *"ATI"* ]]; then
	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    rm -f /etc/X11/xorg.conf.d/20-intel.conf
    rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf

elif [[ $device == *"NVIDIA"* ]]; then
	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    rm -f /etc/X11/xorg.conf.d/20-intel.conf
    rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf

elif [[ $device == *"GeForce"* ]]; then
	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    rm -f /etc/X11/xorg.conf.d/20-intel.conf
    rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf

elif [[ $device == *"Intel"* ]]; then
	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    rm -f /etc/X11/xorg.conf.d/20-intel.conf
    rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf

else

	#Detect driver and configure xorg
	driver=$(lshw -class display)

	if [[ $driver == *"driver=intel"* ]]; then
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /etc/X11/xorg.conf.d/20-intel.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /usr/share/X11/xorg.conf.d/20-intel.conf

	elif [[ $driver == *"driver=i915"* ]]; then
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /etc/X11/xorg.conf.d/20-intel.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /usr/share/X11/xorg.conf.d/20-intel.conf

	elif [[ $driver == *"driver=i965"* ]]; then
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /etc/X11/xorg.conf.d/20-intel.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /usr/share/X11/xorg.conf.d/20-intel.conf

	elif [[ $driver == *"driver=iris"* ]]; then
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /etc/X11/xorg.conf.d/20-intel.conf
    	cp -f /usr/share/regataos/gpu/20-intel.conf /usr/share/X11/xorg.conf.d/20-intel.conf

	elif [[ $driver == *"driver=nvidia"* ]]; then
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-intel.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    	cp -f /usr/share/regataos/gpu/20-nvidia.conf /etc/X11/xorg.conf.d/20-nvidia.conf
    	cp -f /usr/share/regataos/gpu/20-nvidia.conf /usr/share/X11/xorg.conf.d/20-nvidia.conf

	elif [[ $driver == *"driver=amdgpu"* ]]; then
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-intel.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    	rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf
    	cp -f /usr/share/regataos/gpu/20-amdgpu.conf /etc/X11/xorg.conf.d/20-amdgpu.conf
    	cp -f /usr/share/regataos/gpu/20-amdgpu.conf /usr/share/X11/xorg.conf.d/20-amdgpu.conf
	else
    	rm -f /etc/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-amdgpu.conf
    	rm -f /etc/X11/xorg.conf.d/20-radeon.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-radeon.conf
    	rm -f /etc/X11/xorg.conf.d/20-intel.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-intel.conf
    	rm -f /etc/X11/xorg.conf.d/20-nvidia.conf
    	rm -f /usr/share/X11/xorg.conf.d/20-nvidia.conf
    	echo "ERROR: Unsupported VGA controller"
	fi
fi

# Fix apps Qt4
if test -e /etc/xdg/Trolltech.conf ; then
	cp -f /etc/xdg/Trolltech.conf /home/*/.config/Trolltech.conf
	chmod 777 /home/*/.config/Trolltech.conf
fi

# Set performance mode
kmsg=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
echo $kmsg

if [[ $kmsg == *"powersave"* ]]; then
	echo "Enabling performance mode"
    echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    echo "performance mode" > /tmp/regataos-prime/performance.txt
elif [[ $kmsg == *"performance"* ]]; then
    echo "performance mode is enabled"
    echo "performance mode" > /tmp/regataos-prime/performance.txt
else
	echo "Enabling performance mode"
    echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    echo "performance mode" > /tmp/regataos-prime/performance.txt
fi

# Close PackageKit and update system
echo "Loading update system..."
ps -C packagekitd > /dev/null
if [ $? = 0 ]
then

echo "packagekitd here..."

# Set performance mode
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

if [[ $governor == *"powersave"* ]]; then
	echo "Enabling performance mode"
    echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    echo "performance mode" > /tmp/regataos-prime/performance.txt
elif [[ $governor == *"performance"* ]]; then
    echo "performance mode is enabled"
    echo "performance mode" > /tmp/regataos-prime/performance.txt
else
	echo "Enabling performance mode"
    echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    echo "performance mode" > /tmp/regataos-prime/performance.txt
fi

else

	kmsg=$(rpm -q calamares)
	echo $kmsg > /dev/null

	if [[ $kmsg == *"x86"* ]]; then
    	echo "In Live Mode" > /dev/null
	else
		if test -e /usr/share/applications/YaST2/live-installer.desktop ; then
			rm -f /usr/share/applications/YaST2/live-installer.desktop
        fi

		# Set performance mode
		governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

		if [[ $governor == *"powersave"* ]]; then
			echo "Enabling performance mode"
    		echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    		echo "performance mode" > /tmp/regataos-prime/performance.txt
		elif [[ $governor == *"performance"* ]]; then
    		echo "performance mode is enabled"
    		echo "performance mode" > /tmp/regataos-prime/performance.txt
		else
			echo "Enabling performance mode"
    		echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    		echo "performance mode" > /tmp/regataos-prime/performance.txt
		fi

		sleep 180
		# Set powersave mode
		echo powersave | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

		# Fix directory "/tmp/apps-scripts"
		if test ! -e /tmp/apps-scripts ; then
  			mkdir -p /tmp/apps-scripts
  			chmod 777 /tmp/apps-scripts
		else
  			chmod 777 /tmp/apps-scripts
		fi
        
        break
	fi
fi

   sleep 2
done
