.deploy2zenodo:
  script:
    - |
      #    deploy2zenodo
      #    Copyright (C) 2023, 2024  Daniel Mohr
      #    Version: 0.6.4
      #    DOI: 10.5281/zenodo.14247330
      #
      # Instead of command line parameters we use environment variables.
      #
      # You have to provide:
      #   * DEPLOY2ZENODO_API_URL
      #   * DEPLOY2ZENODO_ACCESS_TOKEN
      #   * DEPLOY2ZENODO_DEPOSITION_ID
      #   * DEPLOY2ZENODO_JSON
      #   * DEPLOY2ZENODO_UPLOAD
      #
      # And there are optional variables you can provide:
      #   * DEPLOY2ZENODO_SKIP_PUBLISH
      #   * DEPLOY2ZENODO_DRYRUN
      #   * DEPLOY2ZENODO_SKIPRUN
      #   * DEPLOY2ZENODO_SKIP_NEW_VERSION
      #   * DEPLOY2ZENODO_GET_METADATA
      #   * DEPLOY2ZENODO_SKIP_UPLOAD
      #   * DEPLOY2ZENODO_CURL_MAX_TIME
      #   * DEPLOY2ZENODO_CURL_MAX_TIME_PUBLISH
      #   * DEPLOY2ZENODO_ADD_IsCompiledBy_DEPLOY2ZENODO
      #   * DEPLOY2ZENODO_ADD_IsNewVersionOf
      #   * DEPLOY2ZENODO_ADD_IsPartOf
      #
      # For more information see: https://gitlab.com/deploy2zenodo/deploy2zenodo
      #
      # Copyright 2023, 2024 Daniel Mohr and
      #    Deutsches Zentrum fuer Luft- und Raumfahrt e. V., D-51170 Koeln
      #
      # Licensed under the Apache License, Version 2.0 (the "License");
      # you may not use this file except in compliance with the License.
      # You may obtain a copy of the License at
      #
      #     http://www.apache.org/licenses/LICENSE-2.0
      #
      # Unless required by applicable law or agreed to in writing, software
      # distributed under the License is distributed on an "AS IS" BASIS,
      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      # See the License for the specific language governing permissions and
      # limitations under the License.
      set +v
      log() {
          echo "$(date +"%H:%M:%S")" "$*"
      }
      errlog() {
          printf "\033[0;31m%s %s\033[0m\n" "$(date -Iseconds)" "$*" 1>&2;
      }
      echo "$(date -Iseconds) run deploy2zenodo v0.6.4 https://gitlab.com/deploy2zenodo/deploy2zenodo/-/releases/0.6.4 (DOI: 10.5281/zenodo.14247330)"
      # check available variables
      log "check available variables"
      missing_variables=""
      for varname in DEPLOY2ZENODO_API_URL DEPLOY2ZENODO_ACCESS_TOKEN DEPLOY2ZENODO_DEPOSITION_ID DEPLOY2ZENODO_JSON DEPLOY2ZENODO_UPLOAD; do
          if [ -z "$(eval "echo \"\$$varname\"")" ]; then
              missing_variables="$missing_variables $varname"
          fi
      done
      if [ -n "$missing_variables" ]; then
          errlog "ERROR: you have to provide environment variables"
          errlog "missing variables are at least:$missing_variables"
          exit 1
      else
          log "available variables are looking fine"
          for varname in DEPLOY2ZENODO_API_URL DEPLOY2ZENODO_DEPOSITION_ID DEPLOY2ZENODO_JSON DEPLOY2ZENODO_UPLOAD DEPLOY2ZENODO_SKIP_PUBLISH DEPLOY2ZENODO_DRYRUN DEPLOY2ZENODO_SKIPRUN DEPLOY2ZENODO_SKIP_NEW_VERSION DEPLOY2ZENODO_GET_METADATA DEPLOY2ZENODO_SKIP_UPLOAD DEPLOY2ZENODO_CURL_MAX_TIME DEPLOY2ZENODO_CURL_MAX_TIME_PUBLISH DEPLOY2ZENODO_ADD_IsCompiledBy_DEPLOY2ZENODO DEPLOY2ZENODO_ADD_IsNewVersionOf DEPLOY2ZENODO_ADD_IsPartOf; do
              echo "$varname: \"$(eval "echo \"\$$varname\"")\""
          done
      fi
      if [ -n "$DEPLOY2ZENODO_SKIP_UPLOAD" ] && [ -z "$DEPLOY2ZENODO_SKIP_PUBLISH" ]; then
          errlog "ERROR: DEPLOY2ZENODO_SKIP_UPLOAD can only be used if DEPLOY2ZENODO_SKIP_PUBLISH is used, too!"
          exit 2
      fi
      if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
          curl() {
              echo "$@" > /dev/null
          }
      fi
      if [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
          jq() {
              echo "$@" > /dev/null
          }
          md5sum() {
              echo "$@" > /dev/null
          }
      fi
      CURL_MAX_TIME_PUBLISH=${DEPLOY2ZENODO_CURL_MAX_TIME_PUBLISH:-300}
      hjson='Content-Type: application/json'
      alias curlauth='curl --header "Authorization: Bearer $DEPLOY2ZENODO_ACCESS_TOKEN" --max-time "${DEPLOY2ZENODO_CURL_MAX_TIME:-60}"'
      echojq() {
          echo "$1:"
          echo "$2" | jq -C . || (errlog "$2"; exit 3)
      }
      echo "content of DEPLOY2ZENODO_JSON:"
      jq -C . "$DEPLOY2ZENODO_JSON"
      DEPLOY2ZENODO_JSON_DATA="$(jq -c . "$DEPLOY2ZENODO_JSON")"
      if [ -z "$DEPLOY2ZENODO_SKIPRUN" ]; then
          PUBLICATION_DATE=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -r .metadata.publication_date)
          echo "check publication_date=\"$PUBLICATION_DATE\" in DEPLOY2ZENODO_JSON is EDTF Level 0"
          if [ -n "$PUBLICATION_DATE" ] && [ "$PUBLICATION_DATE" != "null" ]; then
              RE_EDTF_LEVEL_0="((^[0-9]{4}-[0-9]{2}-[0-9]{2}$)|(^[0-9]{4}-[0-9]{2}$)|(^[0-9]{4}$))|(((^[0-9]{4}-[0-9]{2}-[0-9]{2})|(^[0-9]{4}-[0-9]{2})|(^[0-9]{4}))/(([0-9]{4}-[0-9]{2}-[0-9]{2}$)|([0-9]{4}-[0-9]{2}$)|([0-9]{4}$)))"
              echo "$PUBLICATION_DATE" | grep -q -E "$RE_EDTF_LEVEL_0"
              echo "publication_date is EDTF Level 0"
          else
              echo "publication_date is empty or not given"
          fi
      fi
      if [ -n "$DEPLOY2ZENODO_ADD_IsCompiledBy_DEPLOY2ZENODO" ]; then
          IsCompiledBy="{\"relation\":\"IsCompiledBy\",\"identifier\":\"10.5281/zenodo.14247330\",\"scheme\":\"doi\",\"resource_type\":\"software\"}"
          log "add IsCompiledBy:"
          echojq IsCompiledBy "$IsCompiledBy"
          DEPLOY2ZENODO_JSON_DATA=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -c ".metadata.related_identifiers += [$IsCompiledBy]")
          echojq DEPLOY2ZENODO_JSON_DATA "$DEPLOY2ZENODO_JSON_DATA"
      fi
      if [ "$DEPLOY2ZENODO_UPLOAD" = "do NOT provide data" ]; then
          log "adapt DEPLOY2ZENODO_JSON_DATA due to 'do NOT provide data'"
          DEPLOY2ZENODO_JSON_DATA=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq '.files = {"enabled": false}')
          echojq DEPLOY2ZENODO_JSON_DATA "$DEPLOY2ZENODO_JSON_DATA"
      fi
      if [ "$DEPLOY2ZENODO_DEPOSITION_ID" = "create NEW record" ]; then
          # create new record
          log "create new record"
          #   * create new record and upload metadata
          NEWRECORD="$(curlauth --header "$hjson" --request POST --data-binary "$DEPLOY2ZENODO_JSON_DATA" "$DEPLOY2ZENODO_API_URL"/deposit/depositions)"
          echojq NEWRECORD "$NEWRECORD"
          DEPLOY2ZENODO_DEPOSITION_ID=$(echo "$NEWRECORD" | jq .id)
          if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
              log "Skip testing checksum due to DRYRUN or SKIPRUN."
          else
              test -n "$DEPLOY2ZENODO_DEPOSITION_ID" -a "$DEPLOY2ZENODO_DEPOSITION_ID" != "null" || (errlog "DEPLOY2ZENODO_DEPOSITION_ID=\"$DEPLOY2ZENODO_DEPOSITION_ID\""; exit 4)
          fi
          LATESTID=$DEPLOY2ZENODO_DEPOSITION_ID
          log "##################################################################"
          log "# add the id of the deposition/record on zenodo for the next run #"
          log "# id: $DEPLOY2ZENODO_DEPOSITION_ID"
          log "#                                                                #"
          log "# DEPLOY2ZENODO_DEPOSITION_ID=$DEPLOY2ZENODO_DEPOSITION_ID"
          log "##################################################################"
          LATESTDRAFT=$(echo "$NEWRECORD" | jq -r .links.latest_draft)
          BUCKETURL=$(echo "$NEWRECORD" | jq -r .links.bucket)
          if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
              log "Skip testing checksum due to DRYRUN or SKIPRUN."
          else
              test -n "$LATESTDRAFT" -a "$LATESTDRAFT" != "null" || (errlog "LATESTDRAFT=\"$LATESTDRAFT\""; exit 5)
              test -n "$BUCKETURL" -a "$BUCKETURL" != "null" || (errlog "BUCKETURL=\"$BUCKETURL\""; exit 6)
          fi
          #   * reference DOI for all versions
          if [ -n "$DEPLOY2ZENODO_ADD_IsPartOf" ]; then
              log "reference DOI for all versions"
              conceptrecid=$(echo "$NEWRECORD" | jq -r .conceptrecid)
              prereserve_doi="$(echo "$NEWRECORD" | jq -r .metadata.prereserve_doi.doi)"
              CONCEPTDOI=$(echo "$prereserve_doi" | sed "s/$LATESTID/$conceptrecid/g")
              LATESTUPLOADTYPE=$(echo "$NEWRECORD" | jq -r .metadata.upload_type)
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  test -n "$CONCEPTDOI" -a "$CONCEPTDOI" != "null" || (errlog "CONCEPTDOI=\"$CONCEPTDOI\""; exit 28)
              fi
              IsPartOf="{\"relation\":\"IsPartOf\",\"identifier\":\"$CONCEPTDOI\",\"scheme\":\"doi\",\"resource_type\":\"$LATESTUPLOADTYPE\"}"
              log "add IsPartOf:"
              echojq IsPartOf "$IsPartOf"
              DEPLOY2ZENODO_JSON_DATA=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -c ".metadata.related_identifiers += [$IsPartOf]")
              echojq DEPLOY2ZENODO_JSON_DATA "$DEPLOY2ZENODO_JSON_DATA"
              #   * update metadata
              log "update metadata"
              UPLOADMETADATA=$(curlauth --header "$hjson" --request PUT --data-binary "$DEPLOY2ZENODO_JSON_DATA" "$LATESTDRAFT")
              echojq UPLOADMETADATA "$UPLOADMETADATA"
          fi
          #   * upload files/archives
          if [ -n "$DEPLOY2ZENODO_SKIP_UPLOAD" ]; then
              log "##############################"
              log "# skip upload files/archives #"
              log "##############################"
          else
              if [ "$DEPLOY2ZENODO_UPLOAD" = "do NOT provide data" ]; then
                  log "#####################################"
                  log "# do not provide any files/archives #"
                  log "#####################################"
              else
                  log "upload files/archives"
                  for filename in $DEPLOY2ZENODO_UPLOAD; do
                      log "upload \"$filename\""
                      FILEUPLOAD=$(curlauth --upload-file "$filename" "$BUCKETURL"/"$(basename "$filename")")
                      echojq FILEUPLOAD "$FILEUPLOAD"
                      CHECKSUMRAW=$(echo "$FILEUPLOAD" | jq -r .checksum)
                      CHECKSUM=$(echo "$CHECKSUMRAW" | cut -d ':' -f 2)
                      if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                          log "Skip testing checksum due to DRYRUN or SKIPRUN."
                      else
                          test -n "$CHECKSUMRAW" -a "$CHECKSUMRAW" != "null" || (errlog "CHECKSUMRAW=\"$CHECKSUMRAW\""; exit 7)
                          echo "$CHECKSUM  $filename" | md5sum -c -
                      fi
                  done
                  log "files uploaded"
              fi
          fi
          #   * publish
          if [ -n "$DEPLOY2ZENODO_SKIP_PUBLISH" ]; then
              log "#################################################"
              log "# --> Publishing is skipped!                    #"
              log "# --> You have to publish this record manually! #"
              log "#################################################"
          else
              log "publish"
              PUBLISHRECORD=$(curlauth --max-time "$CURL_MAX_TIME_PUBLISH" --request POST "$LATESTDRAFT"/actions/publish)
              echojq PUBLISHRECORD "$PUBLISHRECORD"
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  if [ "$DEPLOY2ZENODO_UPLOAD" = "do NOT provide data" ]; then
                      # Check for error 'Missing uploaded files.'
                      if [ "$(echo "$PUBLISHRECORD" | jq .status)" = 400 ] && [ "$(echo "$PUBLISHRECORD" | jq .errors[].field)" = "\"files.enabled\"" ]; then
                          # Seems used zenodo instance does not support
                          # metadata-only records. Try to use empty dummy file.
                          log "zenodo instance does not support metadata-only records"
                          log "Try to use empty dummy file."
                          dummyfile="$(mktemp)"
                          FILEUPLOAD=$(curlauth --upload-file "$dummyfile" "$BUCKETURL/dummy")
                          echojq FILEUPLOAD "$FILEUPLOAD"
                          CHECKSUMRAW=$(echo "$FILEUPLOAD" | jq -r .checksum)
                          test -n "$CHECKSUMRAW" -a "$CHECKSUMRAW" != "null" || (errlog "CHECKSUMRAW=\"$CHECKSUMRAW\""; exit 8)
                          CHECKSUM=$(echo "$CHECKSUMRAW" | cut -d ':' -f 2)
                          echo "$CHECKSUM  $dummyfile" | md5sum -c -
                          PUBLISHRECORD=$(curlauth --max-time "$CURL_MAX_TIME_PUBLISH" --request POST "$LATESTDRAFT"/actions/publish)
                          echojq PUBLISHRECORD "$PUBLISHRECORD"
                      fi
                  fi
                  test "$(echo "$PUBLISHRECORD" | jq .state)" = '"done"' || (errlog "state=\"$(echo "$PUBLISHRECORD" | jq .state)\""; exit 9)
                  test "$(echo "$PUBLISHRECORD" | jq .submitted)" = "true" || (errlog "submitted=\"$(echo "$PUBLISHRECORD" | jq .submitted)\""; exit 10)
              fi
          fi
          log "##################################################################"
          log "# add the id of the deposition/record on zenodo for the next run #"
          log "# id: $DEPLOY2ZENODO_DEPOSITION_ID"
          log "#                                                                #"
          log "# DEPLOY2ZENODO_DEPOSITION_ID=$DEPLOY2ZENODO_DEPOSITION_ID"
          log "##################################################################"
      else
          # update record
          log "update record"
          #   * get data from record
          RECORD=$(curlauth "$DEPLOY2ZENODO_API_URL"/deposit/depositions/"$DEPLOY2ZENODO_DEPOSITION_ID")
          echojq RECORD "$RECORD"
          conceptrecid=$(echo "$RECORD" | jq -r .conceptrecid)
          NEWVERSIONURL=$(echo "$RECORD" | jq -r .links.newversion)
          if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
              log "Skip testing result due to DRYRUN or SKIPRUN."
          else
              test -n "$conceptrecid" -a "$conceptrecid" != "null" || (errlog "conceptrecid=\"$conceptrecid\""; exit 24)
              test -n "$NEWVERSIONURL" -a "$NEWVERSIONURL" != "null" || (errlog "NEWVERSIONURL=\"$NEWVERSIONURL\""; exit 11)
          fi
          #   * reference previous version
          if [ -n "$DEPLOY2ZENODO_ADD_IsNewVersionOf" ] && [ -z "$DEPLOY2ZENODO_SKIP_NEW_VERSION" ]; then
              log "reference previous version"
              LATESTDOI=$(echo "$RECORD" | jq -r .doi)
              LATESTUPLOADTYPE=$(echo "$RECORD" | jq -r .metadata.upload_type)
              if [ -z "$LATESTUPLOADTYPE" ]; then
                  # try to use upload_type from current data
                  LATESTUPLOADTYPE=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -r .metadata.upload_type)
              fi
              # It is not an error if LATESTDOI is empty:
              # This could be the first version.
              IsNewVersionOf="{\"relation\":\"IsNewVersionOf\",\"identifier\":\"$LATESTDOI\",\"scheme\":\"doi\",\"resource_type\":\"$LATESTUPLOADTYPE\"}"
              log "add IsNewVersionOf:"
              echojq IsNewVersionOf "$IsNewVersionOf"
              if [ -z "$LATESTDOI" ] || [ "$LATESTDOI" = "null" ] || [ -z "$LATESTUPLOADTYPE" ] || [ "$LATESTUPLOADTYPE" = "null" ]; then
                  log "No LATESTDOI or no LATESTUPLOADTYPE found. IsNewVersionOf not added!"
              else
                  DEPLOY2ZENODO_JSON_DATA=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -c ".metadata.related_identifiers += [$IsNewVersionOf]")
                  if [ -z "$DEPLOY2ZENODO_ADD_IsPartOf" ]; then
                      echojq DEPLOY2ZENODO_JSON_DATA "$DEPLOY2ZENODO_JSON_DATA"
                  fi
              fi
          elif [ -n "$DEPLOY2ZENODO_ADD_IsNewVersionOf" ] && [ -n "$DEPLOY2ZENODO_SKIP_NEW_VERSION" ]; then
              log "Due to skipping creating new version the previous version is not available."
          fi
          #   * reference DOI for all versions
          if [ -n "$DEPLOY2ZENODO_ADD_IsPartOf" ]; then
              log "reference DOI for all versions"
              CONCEPTDOI=$(echo "$RECORD" | jq -r .conceptdoi)
              if [ -z "$CONCEPTDOI" ] || [ "$CONCEPTDOI" = "null" ]; then
                  LATESTDOI=$(echo "$RECORD" | jq -r .doi)
                  LATESTID=$(echo "$RECORD" | jq .id)
                  CONCEPTDOI="$(echo "$LATESTDOI" | sed "s $LATESTID $conceptrecid g")"
              fi
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  test -n "$CONCEPTDOI" -a "$CONCEPTDOI" != "null" || (errlog "CONCEPTDOI=\"$CONCEPTDOI\""; exit 27)
              fi
              LATESTUPLOADTYPE=$(echo "$RECORD" | jq -r .metadata.upload_type)
              if [ -z "$LATESTUPLOADTYPE" ]; then
                  # try to use upload_type from current data
                  LATESTUPLOADTYPE=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -r .metadata.upload_type)
              fi
              IsPartOf="{\"relation\":\"IsPartOf\",\"identifier\":\"$CONCEPTDOI\",\"scheme\":\"doi\",\"resource_type\":\"$LATESTUPLOADTYPE\"}"
              log "add IsPartOf:"
              echojq IsPartOf "$IsPartOf"
              DEPLOY2ZENODO_JSON_DATA=$(echo "$DEPLOY2ZENODO_JSON_DATA" | jq -c ".metadata.related_identifiers += [$IsPartOf]")
              echojq DEPLOY2ZENODO_JSON_DATA "$DEPLOY2ZENODO_JSON_DATA"
          fi
          #   * create new version
          if [ -n "$DEPLOY2ZENODO_SKIP_NEW_VERSION" ]; then
              log "#########################################"
              log "# --> Creating new version is skipped!  #"
              log "# --> Try to find latest draft.         #"
              log "#########################################"
              if [ -z "$LISTRES" ]; then
                  LISTRES=$(curlauth "$DEPLOY2ZENODO_API_URL"/deposit/depositions)
              fi
              # echojq LISTRES "$LISTRES"
              LATESTRECORDPRE=$(echo "$LISTRES" | jq -c ".[] | select(.conceptrecid==\"$conceptrecid\" and .state!=\"done\")")
              echojq LATESTRECORDPRE "$LATESTRECORDPRE"
              LATESTID=$(echo "$LATESTRECORDPRE" | jq .id)
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  test -n "$LATESTID" -a "$LATESTID" != "null" || (errlog "LATESTID=\"$LATESTID\""; exit 12)
              fi
              log "#############################"
              log "# found latest id: $LATESTID"
              log "#############################"
              LATESTRECORD=$(curlauth "$DEPLOY2ZENODO_API_URL/deposit/depositions/$LATESTID")
              echojq LATESTRECORD "$LATESTRECORD"
              LATESTDRAFT=$(echo "$LATESTRECORD" | jq -r .links.latest_draft)
              BUCKETURL=$(echo "$LATESTRECORD" | jq -r .links.bucket)
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  test -n "$LATESTDRAFT" -a "$LATESTDRAFT" != "null" || (errlog "LATESTDRAFT=\"$LATESTDRAFT\""; exit 13)
                  test -n "$BUCKETURL" -a "$BUCKETURL" != "null" || (errlog "BUCKETURL=\"$BUCKETURL\""; exit 14)
              fi
          else
              log "create new version"
              NEWVERSION=$(curlauth --request POST "$NEWVERSIONURL")
              echojq NEWVERSION "$NEWVERSION"
              LATESTDRAFT=$(echo "$NEWVERSION" | jq -r .links.latest_draft)
              BUCKETURL=$(echo "$NEWVERSION" | jq -r .links.bucket)
              LATESTID=$(echo "$NEWVERSION" | jq .id)
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  test -n "$LATESTDRAFT" -a "$LATESTDRAFT" != "null" || (errlog "LATESTDRAFT=\"$LATESTDRAFT\""; exit 15)
                  test -n "$BUCKETURL" -a "$BUCKETURL" != "null" || (errlog "BUCKETURL=\"$BUCKETURL\""; exit 16)
                  test -n "$LATESTID" -a "$LATESTID" != "null" || (errlog "LATESTID=\"$LATESTID\""; exit 17)
              fi
              log "#############################"
              log "# found latest id: $LATESTID"
              log "#############################"
          fi
          #   * update metadata
          log "update metadata"
          UPLOADMETADATA=$(curlauth --header "$hjson" --request PUT --data-binary "$DEPLOY2ZENODO_JSON_DATA" "$LATESTDRAFT")
          echojq UPLOADMETADATA "$UPLOADMETADATA"
          #   * remove all files from new version
          log "remove all files from new version"
          for id in $(echo "$UPLOADMETADATA" | jq -r ".files[] | .id"); do
              log "remove file with id \"$id\""
              DELETEFILE=$(curlauth --request DELETE "$LATESTDRAFT"/files/"$id")
              echojq DELETEFILE "$DELETEFILE"
          done
          log "all files removed"
          #   * upload files/archives
          if [ -n "$DEPLOY2ZENODO_SKIP_UPLOAD" ]; then
              log "##############################"
              log "# skip upload files/archives #"
              log "##############################"
          else
              if [ "$DEPLOY2ZENODO_UPLOAD" = "do NOT provide data" ]; then
                  log "#####################################"
                  log "# do not provide any files/archives #"
                  log "#####################################"
              else
                  log "upload files/archives"
                  for filename in $DEPLOY2ZENODO_UPLOAD; do
                      log "upload \"$filename\""
                      FILEUPLOAD=$(curlauth --upload-file "$filename" "$BUCKETURL"/"$(basename "$filename")")
                      echojq FILEUPLOAD "$FILEUPLOAD"
                      CHECKSUMRAW=$(echo "$FILEUPLOAD" | jq -r .checksum)
                      CHECKSUM=$(echo "$CHECKSUMRAW" | cut -d ':' -f 2)
                      if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                          log "Skip testing checksum due to DRYRUN or SKIPRUN."
                      else
                          test -n "$CHECKSUMRAW" -a "$CHECKSUMRAW" != "null" || (errlog "CHECKSUMRAW=\"$CHECKSUMRAW\""; exit 18)
                          echo "$CHECKSUM  $filename" | md5sum -c -
                      fi
                  done
                  log "files uploaded"
              fi
          fi
          #   * publish
          if [ -n "$DEPLOY2ZENODO_SKIP_PUBLISH" ]; then
              log "#################################################"
              log "# --> Publishing is skipped!                    #"
              log "# --> You have to publish this record manually! #"
              log "#################################################"
          else
              log "publish"
              PUBLISHRECORD=$(curlauth --max-time "$CURL_MAX_TIME_PUBLISH" --request POST "$LATESTDRAFT"/actions/publish)
              echojq PUBLISHRECORD "$PUBLISHRECORD"
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  if [ "$DEPLOY2ZENODO_UPLOAD" = "do NOT provide data" ]; then
                      # Check for error 'Missing uploaded files.'
                      if [ "$(echo "$PUBLISHRECORD" | jq .status)" = 400 ] && [ "$(echo "$PUBLISHRECORD" | jq .errors[].field)" = "\"files.enabled\"" ]; then
                          # Seems used zenodo instance does not support
                          # metadata-only records. Try to use empty dummy file.
                          log "zenodo instance does not support metadata-only records"
                          log "Try to use empty dummy file."
                          dummyfile="$(mktemp)"
                          FILEUPLOAD=$(curlauth --upload-file "$dummyfile" "$BUCKETURL/dummy")
                          echojq FILEUPLOAD "$FILEUPLOAD"
                          CHECKSUMRAW=$(echo "$FILEUPLOAD" | jq -r .checksum)
                          test -n "$CHECKSUMRAW" -a "$CHECKSUMRAW" != "null" || (errlog "CHECKSUMRAW=\"$CHECKSUMRAW\""; exit 19)
                          CHECKSUM=$(echo "$CHECKSUMRAW" | cut -d ':' -f 2)
                          echo "$CHECKSUM  $dummyfile" | md5sum -c -
                          PUBLISHRECORD=$(curlauth --max-time "$CURL_MAX_TIME_PUBLISH" --request POST "$LATESTDRAFT"/actions/publish)
                          echojq PUBLISHRECORD "$PUBLISHRECORD"
                      fi
                  fi
                  test "$(echo "$PUBLISHRECORD" | jq .state)" = '"done"' || (errlog "state=\"$(echo "$PUBLISHRECORD" | jq .state)\""; exit 20)
                  test "$(echo "$PUBLISHRECORD" | jq .submitted)" = "true" || (errlog "submitted=\"$(echo "$PUBLISHRECORD" | jq .submitted)\""; exit 21)
              fi
              LATESTID=$(echo "$PUBLISHRECORD" | jq .id)
              HTMLURL=$(echo "$PUBLISHRECORD" | jq .links.html)
              if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
                  log "Skip testing result due to DRYRUN or SKIPRUN."
              else
                  test -n "$LATESTID" -a "$LATESTID" != "null" || (errlog "LATESTID=\"$LATESTID\""; exit 22)
                  test -n "$HTMLURL" -a "$HTMLURL" != "null" || (errlog "HTMLURL=\"$HTMLURL\""; exit 29)
              fi
              log "#############################"
              log "# found latest id: $LATESTID"
              log "# $HTMLURL"
              log "#############################"
          fi
      fi
      if [ -n "$DEPLOY2ZENODO_GET_METADATA" ]; then
          LATESTMETADATA=$(curlauth "$DEPLOY2ZENODO_API_URL"/deposit/depositions/"$LATESTID" | tee "$DEPLOY2ZENODO_GET_METADATA")
          echojq LATESTMETADATA "$LATESTMETADATA"
          if [ -n "$DEPLOY2ZENODO_DRYRUN" ] || [ -n "$DEPLOY2ZENODO_SKIPRUN" ]; then
              log "Skip testing result due to DRYRUN or SKIPRUN."
          else
              test "$(echo "$LATESTMETADATA" | jq .id)" = "$LATESTID" || (errlog "LATESTMETADATA=\"$LATESTMETADATA\""; exit 23)
          fi
      fi
      echo "$(date -Iseconds) finished deploy2zenodo v0.6.4 https://gitlab.com/deploy2zenodo/deploy2zenodo/-/releases/0.6.4"
      # stop deploy2zenodo

deploy2zenodo:
  stage: .post
  image:
    name: alpine:latest
  before_script:
    - apk add --no-cache curl jq
  extends: .deploy2zenodo
