#!/bin/bash

FOLDER_PREFIX="test_folder"
FOLDER_NUMBER=10
readonly __ERROR_REGEX='^[^{].*\|HTTP Status 40\|org.nuxeo.ecm.automation.OperationException\|org.nuxeo.ecm.core.model.NoSuchDocumentException\|com.sun.jersey.api.NotFoundException\|org.codehaus.jackson.JsonParseException\|org.nuxeo.ecm.webengine.WebException'
__URL_PROTOCOL="http"
__URL_IP_PORT="localhost:8080"

getJSONStrValue() {
  local JSON_STR=$1
  local FIELD=$2

  # extracting JSON value and unescaping reserved double-quotes
  echo -e "$(echo "${JSON_STR}" | grep "\"${FIELD}\"" | sed 's/^\s*"'"${FIELD}"'" : "\s*\(.*\)"\s*[,]*$/\1/g' | sed 's/\\"/"/g' | head -1)"
}

executeQuery() {
  local CONNECT_HOST=$1
  local QUERY=$2
  local NX_PROPERTIES=$3
  local QUERY_RES
  local ERR_RES

  QUERY_RES=$(executeQueryRaw "${CONNECT_HOST}" "${QUERY}" "${NX_PROPERTIES}")
  ERR_RES=$?
  if [ ${ERR_RES} -eq 0 ]; then
    echo "${QUERY_RES}" | json_pp
    return 0
  else
    return ${ERR_RES}
  fi
}

executeQueryRaw() {
  local CONNECT_HOST=$1
  local QUERY=$2
  local NX_PROPERTIES
  [ -n "$3" ] && NX_PROPERTIES="-H 'X-NXDocumentProperties: ${3}'"
  local JSON_RES
  local ERR_RES
  local JSON_BODY='{"params":{"query":"'$(echo "${QUERY}" | sed -e "s/'/'\\\''/g" -e 's/"/\\"/g' )'"},"context":{}}'
  local CURL_COMMAND="curl -sSL -X POST ${NX_PROPERTIES} -H 'authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9y' -H 'Content-Type: application/json' -H 'Cache-Control: no-cache' -d '${JSON_BODY}' \"${__URL_PROTOCOL}://${CONNECT_HOST}/nuxeo/api/v1/automation/Document.Query\" 2> /dev/null"

  # echo "[executeQueryRaw][DEBUG] JSON_BODY=${JSON_BODY}" >> /tmp/mycmd.txt
  # echo "[executeQueryRaw][DEBUG] CURL_COMMAND=${CURL_COMMAND}" >> /tmp/mycmd.txt

  if JSON_RES=$(eval "${CURL_COMMAND}"); then
    if ERR_RES=$(echo "${JSON_RES}" | grep ''"${__ERROR_REGEX}"''); then # error occured ?
      # echo "[executeQueryRaw][DEBUG] JSON_RES=${JSON_RES}" >> /tmp/mycmd.txt
      # echo "[executeQueryRaw][DEBUG] ERR_RES=${ERR_RES}" >> /tmp/mycmd.txt
      return 2
    else
      # echo "[executeQueryRaw][DEBUG] JSON_RES=${JSON_RES}" >> /tmp/mycmd.txt
      # echo "[executeQueryRaw][DEBUG] ERR_RES=${ERR_RES}" >> /tmp/mycmd.txt
      echo "${JSON_RES}"
      return 0
    fi
  else
    return 1
  fi
}

for i in $(seq 0 $FOLDER_NUMBER); do
  CURRENT_FOLDER=${FOLDER_PREFIX}${i}

  echo "Renaming folder ${CURRENT_FOLDER}"

  if ! JSON_RES=$(executeQuery "${__URL_IP_PORT}" "SELECT * FROM Folder WHERE dc:title='${CURRENT_FOLDER}'"); then
    echo "Error while performing Query"
    exit 1
  fi

  DOCUMENT_UID="$(getJSONStrValue "${JSON_RES}" "uid")"
  echo "DOCUMENT_UID=${DOCUMENT_UID}"

  curl -X PUT \
    -H 'authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9y' \
    -H 'cache-control: no-cache' \
    -H 'content-type: application/json' \
    -d '{"entity-type": "document", "repository": "default", "uid": "'${DOCUMENT_UID}'", "properties": {"dc:title": "'${CURRENT_FOLDER}'_renamed"}}' \
    "http://${__URL_IP_PORT}/nuxeo/api/v1/id/${DOCUMENT_UID}"
done
