### Generic variables and functions
### -------------------------------

if [ -z "${SYNOPKG_PKGNAME}" ] || [ -z "${SYNOPKG_DSM_VERSION_MAJOR}" ]; then
  echo "Error: Environment variables are not set." 1>&2;
  echo "Please run me using synopkg instead. Example: \"synopkg start [packagename]\"" 1>&2;
  exit 1
fi

USER="sc-minio"
EFF_USER="sc-minio"

# DSM shared folder location from UI if provided
if [ -n "${wizard_data_directory}" ]; then SHARE_PATH="${wizard_data_directory}"; fi

# start-stop-status script redirect stdout/stderr to LOG_FILE
LOG_FILE="${SYNOPKG_PKGVAR}/${SYNOPKG_PKGNAME}.log"

# Service command has to deliver its pid into PID_FILE
PID_FILE="${SYNOPKG_PKGVAR}/${SYNOPKG_PKGNAME}.pid"


### Package specific variables and functions
### ----------------------------------------


PATH="${SYNOPKG_PKGDEST}/bin:${PATH}"
GROUP="sc-minio"

INST_ETC="/var/packages/${SYNOPKG_PKGNAME}/etc"
INST_VARIABLES="${INST_ETC}/installer-variables"
ENV_VARIABLES="${SYNOPKG_PKGVAR}/environment-variables"

# Reload wizard variables stored by postinst
if [ -r "${INST_VARIABLES}" ]; then
    # we cannot source the file to reload the variables, when values have special characters like <, >, ...
    for _line in $(cat "${INST_VARIABLES}"); do
        _key="$(echo ${_line} | awk -F'=' '{print $1}')"
        _value="$(echo ${_line} | awk -F'=' '{print $2}')"
        declare "${_key}=${_value}"
    done
fi

export MINIO_ROOT_USER="${WIZARD_ROOT_USER}"
export MINIO_ROOT_PASSWORD="${WIZARD_ROOT_PASSWORD}"
export HOME="${SYNOPKG_PKGVAR}"

# Load custom variables
if [ -r "${ENV_VARIABLES}" ]; then
    . "${ENV_VARIABLES}"
fi

CONSOLE_PORT=9001

SERVICE_COMMAND="${SYNOPKG_PKGDEST}/bin/minio server --quiet --console-address :${CONSOLE_PORT} --anonymous ${WIZARD_DATA_VOLUME}/${WIZARD_DATA_DIRECTORY}"
SVC_BACKGROUND=y
SVC_WRITE_PID=y


service_postinst ()
{
    echo WIZARD_DATA_VOLUME="${wizard_data_volume}"         >> ${INST_VARIABLES}
    echo WIZARD_DATA_DIRECTORY="${wizard_data_directory}"   >> ${INST_VARIABLES}
    echo WIZARD_ROOT_USER="${wizard_root_user}"             >> ${INST_VARIABLES}
    echo WIZARD_ROOT_PASSWORD="${wizard_root_password}"     >> ${INST_VARIABLES}
}

