#!/usr/bin/env bash

PATH_SOLRCONFIG="${SOLR_HOME}/configsets/ext_solr_13_0_0/conf"
PATH_AND_FILENAME_SOLRCONFIG="${PATH_SOLRCONFIG}/solrconfig.xml"

PATH_AND_FILENAME_SOLRXML="${SOLR_HOME}/solr.xml"

if grep -q "<lib " "${PATH_AND_FILENAME_SOLRCONFIG}"; then
  echo "The Apache Solr instance is affected on CVE-2025-24814"
  echo "  removing usages of <lib> tags in EXT:solr schemas and moving typo3lib from configset to solr.xml"

  cp "${PATH_AND_FILENAME_SOLRCONFIG}" "${PATH_SOLRCONFIG}/solrconfig.xml.Backup-CVE-2025-24814"
  sed -i ':a;N;N;s/.*\<lib .*//g' "${PATH_AND_FILENAME_SOLRCONFIG}"

  mv "${PATH_SOLRCONFIG}"/../typo3lib "${SOLR_HOME}/".

  cp "${PATH_AND_FILENAME_SOLRXML}" "${SOLR_HOME}/solr.xml.Backup-CVE-2025-24814"
  # shellcheck disable=SC2016,SC1004
  sed -i 's/<str name="modules">scripting<\/str>/<str name="modules">scripting,analytics,analysis-extras,langid,clustering,extraction,${solr.modules:}<\/str>\
	<str name="allowPaths">${solr.allowPaths:}<\/str>\
	<str name="allowUrls">${solr.allowUrls:}<\/str>\
\
	<!-- TYPO3 Plugins -->\
	<str name="sharedLib">typo3lib\/<\/str>/g' "${PATH_AND_FILENAME_SOLRXML}"
fi


