#!/bin/bash

# This is a simple program that makes shutdown easier for QubesOS.
# May not work out of the box, you will have to specify in the script 
# your net-vm and usb-vm names.

# i3-exit - A simple QubesOS shutdown script
# Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>

# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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, 
# see <http://www.gnu.org/licenses/>

# Put this file in /bin/ or in /usr/bin. Do not forget to give 
# sudo chmod +x i3-exit
# to make it executable.

version=1.0

qubesnetvm=sys-net
qubesusbvm=sys-usb

# Shuts down all the virtual machines in a wise way
vm_shutdown()
{
        echo "Shutting down everything"
        qvm-shutdown --wait --all --exclude=$qubesnetvm --exclude=$qubesusbvm
        echo "Killing sys-usb"
        qvm-kill sys-usb
        echo "Killing sys-net"
        qvm-kill sys-net
        sleep 3
}

# Checks which screensaver is installed and tries to use it
screen_lock()
{
	if [ -a /bin/i3-blur ]; then
                i3-blur
        elif [ -a /bin/i3lock ]; then
                i3lock
        else
        	echo "What screensaver are you using? I cannot find it."
                exit 2
	fi
}


if [[ "$#" -ge 2 ]]; then
        echo "0 {-v, --version|-h, --help|lock|logout|suspend|hibernate|reboot|shutdown}"
        exit 2
elif [[ "$#" -eq 1 ]]; then
case "$1" in
        -v | --version)
                echo "$0 - Version $version"
                exit 0;;
        -h | --help)
                echo "This is a simple program that makes shutdown/hibernate/reboot etc. easier for QubesOS users having i3. May not work out of the box, you will have to specify in the script your net-vm and usb-vm names."
                echo "- v, --version		Display current version"
                echo "-h, --help		Display this message"
		echo "lock			Lock the screen"
		echo "logout			Logout current user"
		echo "suspend			Suspend system"
		echo "hibernate		Hibernate system"
		echo "reboot			Reboot system"
		echo "shutdown		Power off system"
                exit 0
		;;
        lock)
		screen_lock
                ;;
        logout)
                i3-msg exit
                ;;
        suspend)
                screen_lock && systemctl suspend
                ;;
        hibernate)
                screen_lock && systemctl hibernate
                ;;
        reboot)
                vm_shutdown; reboot
                ;;
        shutdown)
                vm_shutdown; shutdown now
                ;;
        *)
                echo "Usage: $0 {-v, --version|-h, --help|lock|logout|suspend|hibernate|reboot|shutdown}"
                exit 2
esac
fi
exit 0