#!/bin/sh
#     Name:  gparted
#  Purpose:  Perform appropriate startup of GParted executable gpartedbin.
#
#            The purpose of these startup methods is to prevent
#            devices from being automounted, and to ensure only one
#            instance of GParted is running.  File system problems can
#            occur if devices are mounted prior to the completion of
#            GParted's operations, or if multiple partition editing
#            tools are in use concurrently.
#
# Copyright (C) 2008, 2009, 2010, 2013 Curtis Gedak
#
#  This file is part of GParted.
#
#  GParted 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.
#
#  GParted 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 GParted.  If not, see <http://www.gnu.org/licenses/>.
#

#
#  Only permit one instance of GParted to execute at a time
#
if test "z`ps -e | grep gpartedbin`" != "z"; then
	echo "The process gpartedbin is already running."
	echo "Only one gpartedbin process is permitted."
	exit 1
fi

#
#  Search PATH to determine if systemctl program can be found
#  and if appropriate daemon is running.
#
HAVE_SYSTEMCTL=no
for k in '' `echo "$PATH" | sed 's,:, ,g'`; do
	if test -x "$k/systemctl"; then
		if test "z`ps -e | grep systemd`" != "z"; then
			HAVE_SYSTEMCTL=yes
			break
		fi
	fi
done

#
#  Search PATH to determine if udisks program can be found
#  and if appropriate daemon is running.
#
HAVE_UDISKS=no
for k in '' `echo "$PATH" | sed 's,:, ,g'`; do
	if test -x "$k/udisks"; then
		if test "z`ps -e | grep udisks-daemon`" != "z"; then
			HAVE_UDISKS=yes
			break
		fi
	fi
done

#
#  Search PATH to determine if devkit-disks program can be found
#  and if appropriate daemon is running.
#  On December 1, 2009, devkit-disks was named udisks.
#
HAVE_DEVKIT_DISKS=no
for k in '' `echo "$PATH" | sed 's,:, ,g'`; do
	if test -x "$k/devkit-disks"; then
		if test "z`ps -e | grep devkit-disks-da`" != "z"; then
			HAVE_DEVKIT_DISKS=yes
			break
		fi
	fi
done

#
#  Search PATH to determine if hal-lock program can be found
#  and if appropriate daemon is running.
#
HAVE_HAL_LOCK=no
for k in '' `echo "$PATH" | sed 's,:, ,g'`; do
	if test -x "$k/hal-lock"; then
		if test "z`ps -e | grep hald`" != "z"; then
			HAVE_HAL_LOCK=yes
			break
		fi
	fi
done

#
#  Define base command for executing GParted
#
BASE_CMD="/usr/sbin/gpartedbin $*"

#
#  If no root privileges, then invoke gpartedbin directly
#  so that a graphical warning is displayed.
#  Otherwise udisks, devkit-disks, or hal-lock in the later
#  invocation may prevent gpartedbin from starting and hence
#  the user will not see a graphical warning.
#
if test "x`id -u`" != "x0"; then
	echo "Root privileges are required for running gparted."
	$BASE_CMD
	exit 1
fi

#
#  Use systemctl to prevent automount by masking currently unmasked mount points
#
if test "x$HAVE_SYSTEMCTL" = "xyes"; then
	MOUNTLIST=`systemctl list-unit-files --full --all -t mount --no-legend | grep -v masked | cut -f1 -d' '`
	systemctl --runtime mask --quiet -- $MOUNTLIST
fi

#
#  Use both udisks and hal-lock for invocation if both binaries exist and both
#  daemons are running.
#  Else use udisks if binary exists and daemon is running.
#  Else use both devkit-disks and hal-lock for invocation if both binaries exist
#  and both of the daemons are running.
#  Else use devkit-disks if binary exists and daemon is running.
#  Otherwise use hal-lock for invocation if binary exists and daemon is running.
#  If the above checks fail then simply run gpartedbin.
#
if test "x$HAVE_UDISKS" = "xyes" && test "x$HAVE_HAL_LOCK" = "xyes"; then
	udisks --inhibit -- \
		hal-lock --interface org.freedesktop.Hal.Device.Storage --exclusive \
			--run "$BASE_CMD"
elif test "x$HAVE_UDISKS" = "xyes"; then
	udisks --inhibit -- $BASE_CMD
elif test "x$HAVE_DEVKIT_DISKS" = "xyes" && test "x$HAVE_HAL_LOCK" = "xyes"; then
	devkit-disks --inhibit -- \
		hal-lock --interface org.freedesktop.Hal.Device.Storage --exclusive \
			--run "$BASE_CMD"
elif test "x$HAVE_DEVKIT_DISKS" = "xyes"; then
	devkit-disks --inhibit -- $BASE_CMD
elif test "x$HAVE_HAL_LOCK" = "xyes"; then
	hal-lock --interface org.freedesktop.Hal.Device.Storage --exclusive \
		--run "$BASE_CMD"
else
	$BASE_CMD
fi

#
#  Use systemctl to restore that status of any mount points changed above
#
if test "x$HAVE_SYSTEMCTL" = "xyes"; then
	systemctl --runtime unmask --quiet -- $MOUNTLIST
fi
