#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# shellcheck source=start-utils
. "${SCRIPTS:-/}start-utils"
handleDebugMode

: "${SPIGET_RESOURCES:=}"
: "${SPIGET_DOWNLOAD_TOLERANCE:=5}" # in minutes

acceptArgs=(--accept application/zip --accept application/java-archive)

containsJars() {
  file=${1?}

  pat='\.jar$'

  while read -r line; do
    if [[ $line =~ $pat ]]; then
      return 0
    fi
  done < <(unzip -l "$file")

  return 1
}

containsPlugin() {
  file=${1?}

  pat='plugin.yml$'

  while read -r line; do
    if [[ $line =~ $pat ]]; then
      return 0
    fi
  done < <(unzip -l "$file")

  return 1
}

getResourceFromSpiget() {
  resource=${1?}

  log "Downloading resource ${resource} ..."

  mkdir -p /data/plugins

  versionfile="/data/plugins/.${resource}-version.json"
  versionfileNew="/tmp/.${resource}-version.json"

  if [ -f "$versionfile" ]; then
    if [[ -n $(find "$versionfile" -mmin +${SPIGET_DOWNLOAD_TOLERANCE}) ]]; then
      urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest"
      if ! get -o "${versionfileNew}" "${urlVersion}"; then
        log "ERROR failed to download resource version meta data '${resource}' from ${urlVersion}"
        exit 2
      fi

      installedVersion=$(jq -r '.name' $versionfile)
      newVersion=$(jq -r '.name' $versionfileNew)
      
      if [ "$installedVersion" = "$newVersion" ]; then
        log "resource '${resource}' not downloaded because installed version '${installedVersion}' already up to date ('${newVersion}')"
        mv "${versionfileNew}" "${versionfile}"
      else
        if downloadResourceFromSpiget "${resource}"; then
          mv "${versionfileNew}" "${versionfile}"
        fi
      fi
    else
      log "resource '${resource}' not checked because version meta file newer than '${SPIGET_DOWNLOAD_TOLERANCE}' minutes"
    fi
  else
    if downloadResourceFromSpiget "${resource}"; then
      urlVersion="https://api.spiget.org/v2/resources/${resource}/versions/latest"
      if ! get -o "${versionfileNew}" "${urlVersion}"; then
        log "ERROR failed to download resource version meta data '${resource}' from ${urlVersion}"
        exit 2
      fi
      mv "${versionfileNew}" "${versionfile}"
    fi
  fi

}

downloadResourceFromSpiget() {
  resource=${1?}

  resourceUrl="https://api.spiget.org/v2/resources/${resource}"
  if ! outfile=$(get --output-filename -o /tmp "${acceptArgs[@]}" "${resourceUrl}/download"); then
      log "ERROR: failed to download resource '${resource}' from ${resourceUrl}/download"
    if externalUrl=$(get --json-path '$.file.externalUrl' "${resourceUrl}"); then
      log "       Visit $externalUrl to pre-download the resource"
      log "       instead of using SPIGET_RESOURCES"
    fi
    exit 2
  fi

  contentType=$(file -b --mime-type "$outfile")
  if [[ $contentType == application/zip ]]; then
    log "Extracting contents of resource ${resource} into plugins"
    extract "$outfile" /data/plugins
    rm "$outfile"
  elif [[ $contentType == application/java-archive ]]; then
    log "Moving resource ${resource} into plugins"
    mv "$outfile" /data/plugins
  else
    log "ERROR: file for resource ${resource} is not a valid content type: ${contentType}"
    exit 2
  fi
}

if [[ ${SPIGET_RESOURCES} ]]; then
  if isTrue "${REMOVE_OLD_MODS:-false}"; then
    removeOldMods /data/plugins
    REMOVE_OLD_MODS=false
  fi

  log "Getting plugins via Spiget"
  IFS=',' read -r -a resources <<<"${SPIGET_RESOURCES}"
  for resource in "${resources[@]}"; do
    getResourceFromSpiget "${resource}"
  done
fi

exec "${SCRIPTS:-/}start-setupWorld" "$@"
