Current File : //root/23042024.sh.2
#!/bin/bash

# Define variables
mkdir -p /patch/
filechk="/patch/patch24042024.log"

if [ -f "$filechk" ]
then
    echo "Patch 24042024 Already Updated" >> $filechk
    exit
fi


lines_to_append="<Location \"/.well-known\">\n  ProxyPass !\n  ProxyPassReverse !\n</Location>"

# Function to test the Apache configuration
test_apache_config() {
  if httpd -t; then
    echo "Apache configuration test passed." 
    return 0
  else
    echo "Apache configuration test failed."
    return 1
  fi
}

# Function to set correct ownership for configuration files
set_file_ownership() {
  local config_file="$1"
  chown apache:apache "$config_file"
  chmod 644 "$config_file"
}

# Check if either port_mapping or nodejs directory exists
if [ -d "/etc/sentora/configs/apache/port_mapping/" ] || [ -d "/etc/sentora/configs/apache/nodejs/" ]; then
  # Check if the patch has already been applied
  if [ -f "$filechk" ]; then
    echo "Patch 24042024 Already Updated" >> "$filechk"
    exit
  fi

  echo "Patch started" > "$filechk"

  # Loop through config files in the /etc/sentora/configs/apache/nodejs/ directory
  if [ -d "/etc/sentora/configs/apache/nodejs/" ]; then
    for config_file in /etc/sentora/configs/apache/nodejs/*; do
      # Check if the file is not empty
      if [ -s "$config_file" ]; then
        # Check if the lines_to_append already exist in the file
        if grep -qF "$lines_to_append" "$config_file"; then
          echo "Lines already exist in $config_file. Skipping."  >> "$filechk"
        else
          # Append the lines to the file if they don't exist
          if ! grep -qF "<Location \"/.well-known\">" "$config_file"; then
            echo -e "$lines_to_append" >> "$config_file"
            echo "Appended to $config_file"  >> "$filechk"
          fi
        fi
      else
        echo "Skipping empty file: $config_file"  >> "$filechk"
      fi
    done
  fi

  # Loop through config files in the port_mapping directory
  if [ -d "/etc/sentora/configs/apache/port_mapping/" ]; then
    for config_file in /etc/sentora/configs/apache/port_mapping/*; do
      # Check if the file is not empty
      if [ -s "$config_file" ]; then
        # Check if the lines_to_append already exist in the file
        if grep -qF "$lines_to_append" "$config_file"; then
          echo "Lines already exist in $config_file. Skipping."  >> "$filechk"
        else
          # Append the lines to the file if they don't exist
          if ! grep -qF "<Location \"/.well-known\">" "$config_file"; then
            echo -e "$lines_to_append" >> "$config_file"
            echo "Appended to $config_file"  >> "$filechk"
          fi
        fi
      else
        echo "Skipping empty file: $config_file"  >> "$filechk"
      fi
    done
  fi

  # Run dos2unix on all .sh files in the /scripts/ directory
  #find /scripts/ -type f -name "*.sh" -exec dos2unix {} \;

  # Test the Apache configuration
  if test_apache_config; then
    # Reload Apache if the configuration test passes
    echo "Reloading Apache..." >> "$filechk"
    service httpd reload
	/usr/bin/sh /scripts/check_domains_log.sh > /scripts/check_domains_log.log
  else
    echo "Apache configuration test failed. Apache not reloaded."  >> "$filechk"
  fi
else
  echo "Neither /etc/sentora/configs/apache/port_mapping/ nor /etc/sentora/configs/apache/nodejs/ directory exists."  >> "$filechk"

fi