#!/usr/bin/bash

################################################################################
# Copyright (C) 2012-2026 NICE s.r.l.
# All Rights Reserved
#
# This software is the confidential and proprietary information
# of NICE s.r.l. ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with NICE.
################################################################################

dcvxgrantaccess () {
    local help="Grant X access to specified DCV user.

Usage:
dcvxgrantaccess [MODE] [OPTIONS]

MODE can be specified in the form:
USER: grant access to local user USER
+USER: grant access to local user USER
-USER: deny access to local user USER

If no MODE is specified all local users are granted access.

Options:
--help          print this message and exit
"

    local xhost_name="+local:"
    local xhost_name_description="all local users"
    local action=grant
    local has_mode=no

    while [ "$1" != "" ]; do
        case "$1" in
            --help )
                echo "$help"
                return 0
                ;;
            --* )
                echo "Unknown option '$1', use --help for usage instructions"
                return 1
                ;;
            -* )
                xhost_name="-si:localuser:${1:1}"
                xhost_name_description="user '${1:1}'"
                action=deny
                has_mode=yes
                ;;
            +* | * )
                if [ "$has_mode" = "yes" ]; then
                    echo "MODE was already specified, invalid syntax"
                    return 1
                fi

                local user=${1#+*}
                xhost_name="+si:localuser:${user}"
                xhost_name_description="user '${user}'"
                action=grant
                has_mode=yes
                ;;
        esac
        shift
    done

    echo "Amazon DCV: ${action}ing access to X server to ${xhost_name_description}..."
    /usr/bin/xhost "${xhost_name}"
}

dcvxgrantaccess "$@"

# --------------------------------------------------------------------------- #
# ex: set ts=4 sw=4 et syntax=sh :
# --------------------------------------------------------------------------- #
