#!/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_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_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

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_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_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

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