#!/bin/bash

deploy_dir="$(dirname "$(cd -- "$(dirname "$0")" > /dev/null 2>&1; pwd)")"

# source shflags
. "${deploy_dir}/tools/third/shflags"

# define FLAGS
DEFINE_string 'common_conf_dir' '/opt/tmp/fsx' 'fusemonitor conf dir'
DEFINE_string 'common_work_dir' '/opt/tmp/fsx' 'fsx work dir, if not set, will be set to ${FLAGS_common_conf_dir}.'
DEFINE_string 'common_log_dir' '/var/log/fsx' 'fusemonitor log dir'
DEFINE_string 'common_deploy_mode' 'default' 'fsx deploy mode'
DEFINE_integer 'common_socket_poller_num' 0 'fusemonitor socket poller count; 0 means use fusemonitor default'
DEFINE_integer 'fusemonitor_fusedaemon_tos_shared_meta_thread_num' 0 'TOS metadata worker count when restarting fusedaemon; 0 keeps default'
DEFINE_integer 'fusemonitor_fusedaemon_vhost_user_max_queue_num' 0 'vhost-user maximum queue number when restarting fusedaemon; 0 keeps default'
DEFINE_boolean 'fusemonitor_fusedaemon_vhost_user_fast_fail_on_umount' ${FLAGS_FALSE} 'Preserve vhost-user fast-fail-on-umount when starting fusedaemon'
DEFINE_string 'fusemonitor_metrics_agent_name' "${FSX_AGENT_NAME:-}" 'fsx-agent name to preserve when starting fusedaemon'
DEFINE_string 'fusemonitor_metrics_cluster_id' "${FSX_RESOURCE_OWNER_K8S_CLUSTER_ID:-}" 'Kubernetes cluster ID used as fusemonitor metric tag'
DEFINE_string 'fusemonitor_metrics_agent_group' "${FSX_AGENT_GROUP:-}" 'fsx-agent group to preserve when starting fusedaemon'
DEFINE_string 'fusemonitor_metrics_agent_runtime_tag' "${FSX_AGENT_RUNTIME_TAG:-}" 'fsx-agent runtime tag to preserve when starting fusedaemon'
DEFINE_string 'fusemonitor_fusedaemon_common_metrics_override_tags' "${FSX_COMMON_METRICS_OVERRIDE_TAGS:-}" 'fusedaemon metric tag overrides to preserve during recovery'
DEFINE_integer 'fusemonitor_fusedaemon_common_metrics_purge_metrics_timeout_ms' "${FSX_COMMON_METRICS_PURGE_METRICS_TIMEOUT_MS:-0}" 'fusedaemon metric inactivity timeout to preserve during recovery'

# parse the command-line
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"

alias python=python3

if [ "x${FLAGS_fusemonitor_metrics_agent_name}" == "x" ] && [ "x${FSX_AGENT_NAME:-}" != "x" ]; then
    FLAGS_fusemonitor_metrics_agent_name="${FSX_AGENT_NAME}"
fi
if [ "x${FLAGS_fusemonitor_metrics_agent_group}" == "x" ] && [ "x${FSX_AGENT_GROUP:-}" != "x" ]; then
    FLAGS_fusemonitor_metrics_agent_group="${FSX_AGENT_GROUP}"
fi
if [ "x${FLAGS_fusemonitor_metrics_agent_runtime_tag}" == "x" ] && [ "x${FSX_AGENT_RUNTIME_TAG:-}" != "x" ]; then
    FLAGS_fusemonitor_metrics_agent_runtime_tag="${FSX_AGENT_RUNTIME_TAG}"
fi

ext_opts=()
ext_opts+=(--common_deploy_dir="${deploy_dir}")
ext_opts+=(--common_conf_dir="${FLAGS_common_conf_dir}")
ext_opts+=(--common_work_dir="${FLAGS_common_work_dir}")
if [ "x${FLAGS_common_log_dir}" != "x${__flags_common_log_dir_default}" ]; then
    ext_opts+=(--common_log_dir="${FLAGS_common_log_dir}")
fi
if [ "x${FLAGS_common_deploy_mode}" != "x${__flags_common_deploy_mode_default}" ]; then
    ext_opts+=(--common_deploy_mode="${FLAGS_common_deploy_mode}")
fi
if [ ${FLAGS_common_socket_poller_num} -gt 0 ]; then
    ext_opts+=(--common_socket_poller_num="${FLAGS_common_socket_poller_num}")
fi
if [ ${FLAGS_fusemonitor_fusedaemon_tos_shared_meta_thread_num} -gt 0 ]; then
    ext_opts+=(--fusemonitor_fusedaemon_tos_shared_meta_thread_num="${FLAGS_fusemonitor_fusedaemon_tos_shared_meta_thread_num}")
fi
if [ ${FLAGS_fusemonitor_fusedaemon_vhost_user_max_queue_num} -gt 0 ]; then
    ext_opts+=(--fusemonitor_fusedaemon_vhost_user_max_queue_num="${FLAGS_fusemonitor_fusedaemon_vhost_user_max_queue_num}")
fi
if [ ${FLAGS_fusemonitor_fusedaemon_vhost_user_fast_fail_on_umount} -eq ${FLAGS_TRUE} ]; then
    ext_opts+=(--fusemonitor_fusedaemon_vhost_user_fast_fail_on_umount)
fi
if [ -n "${FLAGS_fusemonitor_metrics_agent_name}" ]; then
    ext_opts+=(--fusemonitor_metrics_agent_name="${FLAGS_fusemonitor_metrics_agent_name}")
fi
if [ -n "${FLAGS_fusemonitor_metrics_cluster_id}" ]; then
    ext_opts+=(--fusemonitor_metrics_cluster_id="${FLAGS_fusemonitor_metrics_cluster_id}")
fi
if [ -n "${FLAGS_fusemonitor_metrics_agent_group}" ]; then
    ext_opts+=(--fusemonitor_metrics_agent_group="${FLAGS_fusemonitor_metrics_agent_group}")
fi
if [ -n "${FLAGS_fusemonitor_metrics_agent_runtime_tag}" ]; then
    ext_opts+=(--fusemonitor_metrics_agent_runtime_tag="${FLAGS_fusemonitor_metrics_agent_runtime_tag}")
fi
if [ -n "${FLAGS_fusemonitor_fusedaemon_common_metrics_override_tags}" ]; then
    ext_opts+=(--fusemonitor_fusedaemon_common_metrics_override_tags="${FLAGS_fusemonitor_fusedaemon_common_metrics_override_tags}")
fi
if [ ${FLAGS_fusemonitor_fusedaemon_common_metrics_purge_metrics_timeout_ms} -gt 0 ]; then
    ext_opts+=(--fusemonitor_fusedaemon_common_metrics_purge_metrics_timeout_ms="${FLAGS_fusemonitor_fusedaemon_common_metrics_purge_metrics_timeout_ms}")
fi

# Poller and BPF descriptors can exceed the systemd default soft limit.
nofile_soft=$(ulimit -Sn)
nofile_hard=$(ulimit -Hn)
if [ "${nofile_soft}" != unlimited ] && [ "${nofile_soft}" -lt 4096 ]; then
    if [ "${nofile_hard}" = unlimited ] || [ "${nofile_hard}" -ge 4096 ]; then
        if ulimit -Sn 4096; then
            echo "Configured fusemonitor NOFILE: group=${FLAGS_fusemonitor_metrics_agent_group} soft=${nofile_soft}->4096 hard=${nofile_hard}"
        else
            echo "WARNING: failed to raise fusemonitor NOFILE soft limit to 4096" >&2
        fi
    else
        echo "WARNING: fusemonitor NOFILE hard limit ${nofile_hard} is below 4096; leaving limits unchanged" >&2
    fi
fi

cd "${deploy_dir}/bin"
./fusemonitor "${ext_opts[@]}"
