### 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="readarr"
EFF_USER="sc-readarr"


# Service port
SERVICE_PORT="8787"

# 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"


# use DSM7 package specific tmp folder as alternate TMPDIR, this can be useful to avoid errors like:
# Could not install packages due to an EnvironmentError: [Errno 28] No space left on device
# as /tmp might be too small.
# typically used to install a huge amount of wheels with pip (see homeassistant package)
# TMPDIR is supported by pip https://github.com/pypa/pip/issues/4462 ('--build dir' is not supported anymore)

TMPDIR=${SYNOPKG_PKGTMP}
export TMPDIR=${TMPDIR}


# Normally files in the temp folder are removed after usage (e.g. by pip)
# but if not, this is done after installation with this function.
# NOTE:
# Do not remove the TMPDIR, as it may be used by the service later.
service_clean_tmpdir ()
{
  if [[ -e "${TMPDIR}" ]]; then
    if [ -n "$(ls -A ${TMPDIR})" ]; then
       echo "Cleanup ${TMPDIR}"
       rm -rvf ${TMPDIR}/*
    fi
    if [ -n "$(ls -A ${TMPDIR})" ]; then
       echo "Cleanup hidden files in ${TMPDIR}"
       rm -rvf ${TMPDIR}/.*
    fi
    if [ -n "$(ls -A ${TMPDIR})" ]; then
       echo "WARNING: Failed to remove all files in ${TMPDIR}"
    fi
  fi
}

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


# Readarr service setup
READARR="${SYNOPKG_PKGDEST}/share/Readarr/bin/Readarr"

# Readarr uses custom Config and PID directories
HOME_DIR="${SYNOPKG_PKGVAR}"
CONFIG_DIR="${HOME_DIR}/.config"
READARR_CONFIG_DIR="${CONFIG_DIR}/Readarr"
PID_FILE="${READARR_CONFIG_DIR}/readarr.pid"
CMD_ARGS="-nobrowser -data=${READARR_CONFIG_DIR}"

if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
    GROUP="sc-download"
    SERVICE_COMMAND="env HOME=${HOME_DIR} LD_LIBRARY_PATH=${SYNOPKG_PKGDEST}/lib ${READARR} ${CMD_ARGS}"
else
    SERVICE_COMMAND="env HOME=${HOME_DIR} ${READARR} ${CMD_ARGS}"
fi

SVC_BACKGROUND=y
SVC_WAIT_TIMEOUT=90

service_postinst ()
{
    echo "Set update required"
    # Make Readarr do an update check on start
    touch ${READARR_CONFIG_DIR}/update_required 2>&1

    if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
        set_unix_permissions "${CONFIG_DIR}"
    fi
}

service_preupgrade ()
{
    ## never update Readarr distribution, use internal updater only
    [ -d ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup ] && rm -rf ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup
    echo "Backup existing distribution to ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup"
    mkdir -p ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup 2>&1
    rsync -aX ${SYNOPKG_PKGDEST}/share ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup/ 2>&1
}

service_postupgrade ()
{
    ## restore Readarr distribution
    if [ -d ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup/share ]; then
        echo "Restore previous distribution from ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup"
        rm -rf ${SYNOPKG_PKGDEST}/share/Readarr/bin 2>&1
        # prevent overwrite of updated package_info
        rsync -aX --exclude=package_info ${SYNOPKG_TEMP_UPGRADE_FOLDER}/backup/share/ ${SYNOPKG_PKGDEST}/share 2>&1
    fi

    if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
        set_unix_permissions "${SYNOPKG_PKGDEST}/share"
    fi
}
