#!/usr/bin/env bash
# _modules/scripts/migrate_options.sh
# THIS SCRIPT IS DESIGNED TO BE RUNNING BY MAILCOW SCRIPTS ONLY!
# DO NOT, AGAIN, NOT TRY TO RUN THIS SCRIPT STANDALONE!!!!!!

migrate_config_options() {

  sed -i --follow-symlinks '$a\' mailcow.conf

  KEYS=(
    SOLR_HEAP
    SKIP_SOLR
    SOLR_PORT
    FLATCURVE_EXPERIMENTAL
    DISABLE_IPv6
    ACME_CONTACT
  )

  for key in "${KEYS[@]}"; do
    if grep -q "${key}" mailcow.conf; then
      case "${key}" in
        SOLR_HEAP)
          echo "Removing ${key} in mailcow.conf"
          sed -i '/# Solr heap size in MB\b/d' mailcow.conf
          sed -i '/# Solr is a prone to run\b/d' mailcow.conf
          sed -i '/SOLR_HEAP\b/d' mailcow.conf
          ;;
        SKIP_SOLR)
          echo "Removing ${key} in mailcow.conf"
          sed -i '/\bSkip Solr on low-memory\b/d' mailcow.conf
          sed -i '/\bSolr is disabled by default\b/d' mailcow.conf
          sed -i '/\bDisable Solr or\b/d' mailcow.conf
          sed -i '/\bSKIP_SOLR\b/d' mailcow.conf
          ;;
        SOLR_PORT)
          echo "Removing ${key} in mailcow.conf"
          sed -i '/\bSOLR_PORT\b/d' mailcow.conf
          ;;
        FLATCURVE_EXPERIMENTAL)
          echo "Removing ${key} in mailcow.conf"
          sed -i '/\bFLATCURVE_EXPERIMENTAL\b/d' mailcow.conf
          ;;
        DISABLE_IPv6)
          echo "Migrating ${key} to ENABLE_IPv6 in mailcow.conf"
          local old=$(grep '^DISABLE_IPv6=' "mailcow.conf" | cut -d'=' -f2)
          local new
          if [[ "$old" == "y" ]]; then
            new="false"
          else
            new="true"
          fi
          sed -i '/^DISABLE_IPv6=/d' "mailcow.conf"
          echo "ENABLE_IPV6=$new" >> "mailcow.conf"
          ;;
        ACME_CONTACT)
          echo "Deleting obsoleted ${key} in mailcow.conf"
          sed -i '/^# Lets Encrypt registration contact information/d' mailcow.conf
          sed -i '/^# Optional: Leave empty for none/d' mailcow.conf
          sed -i '/^# This value is only used on first order!/d' mailcow.conf
          sed -i '/^# Setting it at a later point will require the following steps:/d' mailcow.conf
          sed -i '/^# https:\/\/docs.mailcow.email\/troubleshooting\/debug-reset_tls\//d' mailcow.conf
          sed -i '/^ACME_CONTACT=.*/d' mailcow.conf
          sed -i '/^#ACME_CONTACT=.*/d' mailcow.conf
          ;;
      esac
    fi
  done

  solr_volume=$(docker volume ls -qf name=^${COMPOSE_PROJECT_NAME}_solr-vol-1)
  if [[ -n $solr_volume ]]; then
    echo -e "\e[34mSolr has been replaced within mailcow since 2025-01.\nThe volume $solr_volume is unused.\e[0m"
    sleep 1
    if [ ! "$FORCE" ]; then
      read -r -p "Remove $solr_volume? [y/N] " response
      if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
        echo -e "\e[33mRemoving $solr_volume...\e[0m"
        docker volume rm $solr_volume || echo -e "\e[31mFailed to remove. Remove it manually!\e[0m"
        echo -e "\e[32mSuccessfully removed $solr_volume!\e[0m"
      else
        echo -e "Not removing $solr_volume. Run \`docker volume rm $solr_volume\` manually if needed."
      fi
    else
      echo -e "\e[33mForce removing $solr_volume...\e[0m"
      docker volume rm $solr_volume || echo -e "\e[31mFailed to remove. Remove it manually!\e[0m"
      echo -e "\e[32mSuccessfully removed $solr_volume!\e[0m"
    fi
  fi

  # Delete old fts.conf before forced switch to flatcurve to ensure update is working properly
  FTS_CONF_PATH="${SCRIPT_DIR}/data/conf/dovecot/conf.d/fts.conf"
  if [[ -f "$FTS_CONF_PATH" ]]; then
    if grep -q "Autogenerated by mailcow" "$FTS_CONF_PATH"; then
      rm -rf $FTS_CONF_PATH
    fi
  fi
}