#!/bin/sh

# with openrc use loginctl
[ "$(cat /proc/1/comm)" = "systemd" ] && logind=systemctl || logind=loginctl

command="$1"
system_control() {
	if [ "$(loginctl show-session --property=NCurrentSessions --value)" -gt 2 ]; then
		notify-send --replace-id=4252 \
			"i3exit" "Multiple users are logged in\nCant't $command"
		exit
	fi
	eval "$@"
}

lockWall="$HOME/.local/share/lockWall"
lock_desktop() {
	[ -f "$lockWall" ] &&
		i3lock --tiling --image="$lockWall" ||
		i3lock --color=001177
	pkill ssh-agent
}

case "$command" in
    lock)
        lock_desktop
				xset dpms force off
        ;;
    logout)
				switch-session
        i3-msg exit
        ;;
    switch_user)
        dm-tool switch-to-greeter
        ;;
    suspend)
        lock_desktop; $logind suspend
        ;;
    hibernate)
        system_control lock_desktop && $logind hibernate
        ;;
    reboot)
        system_control $logind reboot
        ;;
    shutdown)
        system_control $logind poweroff
        ;;
    *)
        echo "== ! i3exit: missing or invalid argument ! =="
        echo "Try again with: lock | logout | switch_user | suspend | hibernate | reboot | shutdown"
        exit 2
esac

exit 0