#!/bin/sh
# postinst script for debathena-python-pyhesiodfs
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

# Automatically added by dh_systemd_enable
# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask debathena-pyhesiodfs.service >/dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled debathena-pyhesiodfs.service; then
	# Enables the unit on first installation, creates new
	# symlinks on upgrades if the unit file has changed.
	deb-systemd-helper enable debathena-pyhesiodfs.service >/dev/null || true
else
	# Update the statefile to add new symlinks (if any), which need to be
	# cleaned up on purge. Also remove old symlinks.
	deb-systemd-helper update-state debathena-pyhesiodfs.service >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit
# In case this system is running systemd, we need to ensure that all
# necessary tmpfiles (if any) are created before starting.
if [ -d /run/systemd/system ] ; then
	systemd-tmpfiles --create /usr/lib/tmpfiles.d/debathena-pyhesiodfs.conf >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit
if [ -x "/etc/init.d/debathena-pyhesiodfs" ] || [ -e "/etc/init/debathena-pyhesiodfs.conf" ]; then
	if [ ! -e "/etc/init/debathena-pyhesiodfs.conf" ]; then
		update-rc.d debathena-pyhesiodfs defaults 15 85 >/dev/null
	fi
	if [ -n "$2" ]; then
		_dh_action=restart
	else
		_dh_action=start
	fi
	invoke-rc.d debathena-pyhesiodfs $_dh_action || exit $?
fi
# End automatically added section
# displace.sh.in: diversion helpers for maintainer scripts
#
# displace_link <prefix> <suffix>
#
#   Ensures that the file <prefix><suffix> is properly diverted to
#   <prefix>.divert-orig<suffix> by this package, and becomes a
#   symbolic link to either <prefix>.divert<suffix> (default) or
#   <prefix>.divert-orig<suffix>.
#
# undisplace_unlink <prefix> <suffix>
#
#   Undoes the action of displace_link <prefix> <suffix> specified
#   above.
#
# Version: 4.0
#
# Copyright © 2008–2012 Tim Abbott <tabbott@mit.edu> and Anders
# Kaseorg <andersk@mit.edu>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the “Software”), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

package=debathena-pyhesiodfs

ours=.debathena
theirs=.debathena-orig

displace_link_displace()
{
    file=$1
    ourfile=$2
    theirfile=$3
    if ! LC_ALL=C dpkg-divert --list "$package" | \
	grep -xFq "diversion of $file to $theirfile by $package"; then
	dpkg-divert --divert "$theirfile" --rename --package "$package" --add "$file"
    fi
}

displace_link_symlink()
{
    file=$1
    ourfile=$2
    theirfile=$3
    if [ ! -L "$file" ] && [ ! -e "$file" ]; then
	ln -s "$(basename "$ourfile")" "$file"
    elif [ ! -L "$file" ] || \
	[ "$(readlink "$file")" != "$(basename "$ourfile")" -a \
	  "$(readlink "$file")" != "$(basename "$theirfile")" ]; then
	echo "*** OMINOUS WARNING ***: $file is not linked to either $(basename "$ourfile") or $(basename "$theirfile")" >&2
    fi
}

displace_link()
{
    prefix=$1
    suffix=$2

    file=$prefix$suffix
    ourfile=$prefix$ours$suffix
    theirfile=$prefix$theirs$suffix
    displace_link_displace "$file" "$ourfile" "$theirfile"
    displace_link_symlink "$file" "$ourfile" "$theirfile"
}

displace_hide()
{
    file=$1
    ourfile=""
    theirfile=$2
    displace_link_displace "$file" "$ourfile" "$theirfile"
}

undisplace_unlink_symlink()
{
    file="$1"
    ourfile="$2"
    theirfile="$3"
    if [ ! -L "$file" ] || \
	[ "$(readlink "$file")" != "$(basename "$ourfile")" -a \
	  "$(readlink "$file")" != "$(basename "$theirfile")" ]; then
	echo "*** OMINOUS WARNING ***: $file is not linked to either $(basename "$ourfile") or $(basename "$theirfile")" >&2
    else
	rm -f "$file"
    fi
}

undisplace_unlink_displace()
{
    file="$1"
    if [ ! -L "$file" ] && [ ! -e "$file" ]; then
	dpkg-divert --remove --rename --package "$package" "$file"
    else
	echo "Not removing diversion of $file by $package" >&2
    fi
}

undisplace_unlink()
{
    prefix=$1
    suffix=$2

    file=$prefix$suffix
    ourfile=$prefix$ours$suffix
    theirfile=$prefix$theirs$suffix

    undisplace_unlink_symlink "$file" "$ourfile" "$theirfile"
    undisplace_unlink_displace "$file"
}

undisplace_unhide()
{
    file=$1
    undisplace_unlink_displace "$file"
}

check_undisplace_unlink()
{
    prefix=$1
    suffix=$2

    file=$prefix$suffix
    ourfile=$prefix$ours$suffix
    theirfile=$prefix$theirs$suffix

    if LC_ALL=C dpkg-divert --list "$package" | \
	grep -xFq "diversion of $file to $theirfile by $package"; then
	undisplace_unlink "$prefix" "$suffix"
    fi
}

check_undisplace_unhide()
{
    file=$1
    hiddenfile=$2
    if LC_ALL=C dpkg-divert --list "$package" | \
	grep -xFq "diversion of $file to $hiddenfile by $package"; then
	undisplace_unhide "$file"
    fi
}

# End of divert.sh.in
if [ "$1" = "configure" ]; then
    displace_link /etc/fuse.conf 
fi


case "$1" in
    configure)
	# Adding pyhesiodfs only if the local user group fuse exists.
	# The group fuse no longer exists in jessie.
	# If the group fuse is needed, it should have been added when the package
	# fuse, a dependency, was configured, before this script is run.
	if grep -q "^fuse:" /etc/group; then
	    adduser pyhesiodfs fuse
	fi

	if hash invoke-rc.d 2>/dev/null; then
	    # If /dev/fuse has group root, restart udev to work around
	    # <https://bugs.launchpad.net/ubuntu/+source/fuse/+bug/293502>
	    [ "$(stat -c %g /dev/fuse)" != 0 ] || invoke-rc.d udev restart || :
	    invoke-rc.d debathena-pyhesiodfs restart
	else
	    [ "$(stat -c %g /dev/fuse)" != 0 ] || /etc/init.d/udev restart || :
	    /etc/init.d/debathena-pyhesiodfs restart
	fi
        update-rc.d -f debathena-afuse-automounter remove >/dev/null || :
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0


