#!/bin/sh
#
#  xfce4
#
#  Copyright (C) 1999, 2003 Olivier Fourdan (fourdan@xfce.org)
#  Copyright (C) 2011       Guido Berhoerster (guido+xfce.org@berhoerster.name)
#  Copyright (C) 2014       Jarno Suni (8@iki.fi)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty 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, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
set -o nounset
set -o errexit
# OpenBSD and possibly some other OSs require
# /usr/local/bin:/usr/X11R6/bin in PATH
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
export PATH

# Lock by a screensaver, if its daemon is running.
for lock_cmd in \
	"light-locker-command --lock" \
    "xscreensaver-command -lock"
do
   $lock_cmd >/dev/null 2>&1 && exit
done

# Lock by gnome-screensaver only, if it is running
if [ -n "`gnome-screensaver-command --query 2>/dev/null`" ] ; then 
 gnome-screensaver-command --lock >/dev/null
else

# TODO: 
# Similar check for mate-screensaver-command and cinnamon-screensaver-command ??

# No daemon was found; try some available daemonless utility to lock:

# i3lock can fork, so better to run it without trailing &.
# i3lock cab also handle display power management.
# (Esc turns display off)
 i3lock --dpms --color=000000 >/dev/null 2>&1 ||
 
 for lock_cmd in \
  "xlock -mode blank" \
  "slimlock" \
  "slock" \
  "xtrlock" # Note: xtrlock shows desktop
  do
    if command -v -- $lock_cmd >/dev/null; then
# Run in background so that this script exits before unlocking.
# Note: Does not tell to this script, if the command actually succeeds.
# Also, if starting the locker takes noticeable time, this script 
# may finnish before locking is effective revealing the desktop when
# resuming from suspend.
        $lock_cmd >/dev/null 2>&1 &
# Turn off display backlight
		xset dpms force off     
        exit
    fi
 done ||
 exit 1
fi
