#!/bin/bash
# postinst script for slimbookbattery

#DEBHELPER#

shopt -s globstar

echo "Debian postinst $1 :"


if [[ $(python3 /usr/share/slimbookbattery/src/check_config.py) ]]; then
	echo "Configuration OK"
fi


if [[ $(chmod -R 766 /home/*/.config/slimbookbattery) ]]; then
	echo "Read/Write permissions given to config files."

fi


if [[ $(chmod -R 755 /usr/share/slimbookbattery/src/**/*.sh) ]]; then
	echo "Exec permissions given."

fi


if [[ $(chmod -R 755 /usr/share/slimbookbattery/src/**/*.py) ]]; then
	echo "Exec permissions given to src."
fi


if [[ $(sudo systemctl enable tlp.service) ]]; then
	echo "TLP service enabled!"
fi


echo "Checking autostart files for users..."
for user in $(ls /home); do
	echo -e "\n$user"

	filename="slimbookbattery-autostart.desktop"
	root_path="/usr/share/slimbookbattery/src"
	user_path="/home/$user/.config/autostart"

	if [[ $(test -d "$user_path") ]]; then
		if [[ ! $(test -f "$user_path/$filename") ]]; then
			echo "Creating $user_path/$filename"
			cp "$root_path/$filename" "$user_path/$filename"
		fi
	fi
done


rm -f /lib/systemd/system/slimbookbattery.service


echo "Debian postinst completed."
