#!/bin/bash

################################################################################
# Copyright (C) 2017-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.
################################################################################

# Configure substitutions
readonly prefix=/usr
readonly exec_prefix=/usr
readonly bindir=/usr/bin
readonly datarootdir=/usr/share
readonly datadir=/usr/share
readonly libdir=/usr/lib/x86_64-linux-gnu
readonly libexecdir=/usr/lib/x86_64-linux-gnu
readonly sysconfdir=/etc

. "${sysconfdir}/dcv/simpleextauth.conf"

readonly host=${EXTAUTH_HOST:-localhost}
readonly port=${EXTAUTH_PORT:-8444}
readonly auth_dir=${EXTAUTH_AUTH_DIR:-/var/run/dcvsimpleextauth}
readonly log_dir=${EXTAUTH_LOG_DIR:-/var/log/dcv}

if [ "$1" = "start-server" ]; then
    if [ -z "${EXTAUTH_CERTIFICATE}"  -o -z "${EXTAUTH_KEY}" ] ; then
        "${libexecdir}/dcvsimpleextauth.py" start-server --host "${host}" --port "${port}" --auth-dir "${auth_dir}" --log-dir "${log_dir}"
    else
        "${libexecdir}/dcvsimpleextauth.py" start-server --certificate "${EXTAUTH_CERTIFICATE}" --key "${EXTAUTH_KEY}" --host "${host}" --port "${port}" --auth-dir "${auth_dir}" --log-dir "${log_dir}"
    fi
elif [[ "$1" != -* ]]; then
    declare -a _args=()

    while [ $# -gt 0 ]; do
        case "$1" in
            --auth-dir)
                # reserved parameter
                shift; shift;
                ;;
            *)
                _args+=("$1")
                shift
                ;;
        esac
    done

    "${libexecdir}/dcvsimpleextauth.py" "${_args[@]}" --auth-dir "${auth_dir}"
elif [ -n "$1" ]; then
    "${libexecdir}/dcvsimpleextauth.py" "$@"
else
    "${libexecdir}/dcvsimpleextauth.py" --help
fi

# ex:ts=4:et:
