#!/bin/bash
# -*- mode: shell-script; coding: utf-8 -*-
#
# Copyright 2002-2014 Cendio AB.
# For more information, see http://www.cendio.com
#

has_ewmh_wm()
{
    local name
    local id
    local child_id
        
    # If property does not exist, "id" will contain "no such atom on any window"
    read name id <<< $(/opt/thinlinc/libexec/xprop -root 32x ' $0\n' _NET_SUPPORTING_WM_CHECK)
    read name child_id <<< $(/opt/thinlinc/libexec/xprop -id "${id}" 32x ' $0\n' _NET_SUPPORTING_WM_CHECK 2>/dev/null)

    if [ "${id}" != "${child_id}" ]; then
        return 1
    fi

    return 0
}

wait_for_wm()
{
    for i in {1..30}; do
        if has_ewmh_wm; then
            return 0
        fi

        sleep 1
    done
    return 1
}

# Compatibility functions that will be called when upgrading older
# versions of ThinLinc (< 4.12.0):

init_is_systemd()
{
    # We lie here and claim that this is not a systemd system in order
    # to work around a bug in remove_service. It should fall back to
    # chkconfig instead, which is still included.
    #
    # Note that this also means that we will fall back to "service" for
    # stopping our services. Fortunately it is clever enough to redirect
    # to systemctl on its own.

    return 1
}

systemctl_redirect()
{
    # Don't do anything as we should already be in the correct context
    return 0
}
