#!/usr/bin/env bash

set -euo pipefail

INSTALL_COMPONENT_SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DEB_FILE=$(basename $(find "${INSTALL_COMPONENT_SCRIPT_DIR}" -name '*.deb'))

echo "Installing PXF DEB ${DEB_FILE} ..."

owner=$(whoami)
sudo_if_needed=''
if [[ "${owner}" != "root" ]]; then
    sudo_if_needed='sudo'
fi

${sudo_if_needed} dpkg --install ${INSTALL_COMPONENT_SCRIPT_DIR}/${DEB_FILE}
${sudo_if_needed} chown --recursive ${owner} /usr/local/cloudberry-pxf*

echo "Successfully installed PXF version $(cat /usr/local/cloudberry-pxf*/version)"
