#!/usr/bin/env bash

set -eu

# Usage for the install_tarball script
usage () {
    cat <<EOF
Usage: $0 [OPTIONS]
    The following options may be used:
        -u                  Update pmm-agent
        -help)              This help
Example:
    For help command - $0 -help
    To install PMM tarball - $0
    To update the installed PMM tarball - $0 -u
EOF
}

UPDATE=0
for arg in "$@"
do
    case "$arg" in
        "-u") UPDATE=1 ;;
        "-help") usage; exit 0 ;;
        *) echo "Invalid option: $arg"; usage; exit 1;;
    esac
done

CURRENT_DIR="$(pwd)"
WORKING_DIR="$(dirname "${0}")"
cd "${WORKING_DIR}" || exit 2

PMM_DIR=${PMM_DIR:-/usr/local/percona/pmm}

PMM_USER=${PMM_USER:-}
PMM_GROUP=${PMM_GROUP:-}

INSTALL_COMMAND="install"
if [ -n "${PMM_USER}" ] ; then
    INSTALL_COMMAND+=" -o ${PMM_USER}"
fi
if [ -n "${PMM_GROUP}" ] ; then
    INSTALL_COMMAND+=" -g ${PMM_GROUP}"
fi

# Check if PMM_DIR has the right permission to install files into it
mkdir -p "${PMM_DIR}" || true
if [ -w "${PMM_DIR}" ]; then
    echo "Installing into $(realpath "${PMM_DIR}")..."
else
    echo -e "Error: cannot write to ${PMM_DIR}. \nPlease make sure the user $(id -un) has permissions to write to this directory\n\n"
    usage
    exit 1
fi

${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/bin
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/tools
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/exporters
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/config
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/textfile-collector
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/textfile-collector/low-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/textfile-collector/medium-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/textfile-collector/high-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/mysql
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/mysql/low-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/mysql/medium-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/mysql/high-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/postgresql
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/postgresql/low-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/postgresql/medium-resolution
${INSTALL_COMMAND} -m 0755 -d "${PMM_DIR}"/collectors/custom-queries/postgresql/high-resolution
${INSTALL_COMMAND} -m 0755 queries-postgres-uptime.yml "${PMM_DIR}"/collectors/custom-queries/postgresql/high-resolution
${INSTALL_COMMAND} -m 0755 queries-hr.yml "${PMM_DIR}"/collectors/custom-queries/postgresql/high-resolution
${INSTALL_COMMAND} -m 0755 queries-mr.yaml "${PMM_DIR}"/collectors/custom-queries/postgresql/medium-resolution
${INSTALL_COMMAND} -m 0755 queries-lr.yaml "${PMM_DIR}"/collectors/custom-queries/postgresql/low-resolution
${INSTALL_COMMAND} -m 0755 queries-mysqld-group-replication.yml "${PMM_DIR}"/collectors/custom-queries/mysql/high-resolution

for FILE in "${PWD}"/bin/*; do
    FILE=$(basename "${FILE}")
    if [ "${FILE}" = "pmm-admin" ] || [ "${FILE}" = "pmm-agent" ] || [ "${FILE}" = "pmm-agent-entrypoint" ]; then
        ${INSTALL_COMMAND} -m 0755 "${PWD}/bin/${FILE}" "${PMM_DIR}"/bin
    elif [[ "${FILE}" =~ pt-summary|pt-mysql-summary|pt-pg-summary|pt-mongodb-summary|nomad ]]; then
        ${INSTALL_COMMAND} -m 0755 "${PWD}/bin/${FILE}" "${PMM_DIR}"/tools
    else
        ${INSTALL_COMMAND} -m 0755 "${PWD}/bin/${FILE}" "${PMM_DIR}"/exporters
    fi
done

for FILE in example.prom queries-mysqld.yml example-queries-postgres.yml; do
    for RESOLUTION in low medium high; do
        if [ "${FILE}" = "example.prom" ]; then
            ${INSTALL_COMMAND} -m 0755 ${FILE} "${PMM_DIR}"/collectors/textfile-collector/${RESOLUTION}-resolution
        elif [ "${FILE}" = "queries-mysqld.yml" ]; then
            ${INSTALL_COMMAND} -m 0755 ${FILE} "${PMM_DIR}"/collectors/custom-queries/mysql/${RESOLUTION}-resolution
        elif [ "${FILE}" = "example-queries-postgres.yml" ]; then
            ${INSTALL_COMMAND} -m 0755 ${FILE} "${PMM_DIR}"/collectors/custom-queries/postgresql/${RESOLUTION}-resolution
        fi
    done
done

if [ "${UPDATE}" = "1" ]; then
  echo "Config file was not removed!"
else
  if ${INSTALL_COMMAND} -m 0660 /dev/null "${PMM_DIR}"/config/pmm-agent.yaml; then
    echo "Config file was created!"
  else
    echo "Failed to create config file!"
    exit 1
  fi
fi

cd "${CURRENT_DIR}" || true
