#!/bin/bash

# Set the primary directory to check
DIR="/web/html/outrightsystems.org"
BACKUP_DIR="/web/html/wordpress"

# Fetch required folders and files from the backup directory
REQUIRED_FOLDERS=($(find "$BACKUP_DIR" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;))
REQUIRED_FILES=($(find "$BACKUP_DIR" -mindepth 1 -maxdepth 1 -type f -exec basename {} \;))

MISSING_FOLDERS=()
MISSING_FILES=()

# Restore missing folders
for FOLDER in "${REQUIRED_FOLDERS[@]}"; do
    if [ ! -d "$DIR/$FOLDER" ]; then
        MISSING_FOLDERS+=("$FOLDER")
        cp -r "$BACKUP_DIR/$FOLDER" "$DIR/"
    fi
done

# Restore missing files
for FILE in "${REQUIRED_FILES[@]}"; do
    if [ ! -f "$DIR/$FILE" ]; then
        MISSING_FILES+=("$FILE")
        cp "$BACKUP_DIR/$FILE" "$DIR/"
    fi
done

# Count files and directories in the home directory
FILE_COUNT=$(find "$DIR" -type f | wc -l)
FOLDER_COUNT=$(find "$DIR" -type d | wc -l)

# Format missing files and folders for email
MISSING_FOLDERS_LIST=$(printf "%s\n" "${MISSING_FOLDERS[@]}")
MISSING_FILES_LIST=$(printf "%s\n" "${MISSING_FILES[@]}")

# Set email subject and body
EMAIL="ashish@outrightcrm.com"
SUBJECT="Bot Scanning completed for outrightsystems.org"
BODY="Directory: $DIR
Files: $FILE_COUNT
Folders: $FOLDER_COUNT
Restored Files/Folders: $((${#MISSING_FILES[@]} + ${#MISSING_FOLDERS[@]}))
Missing Folders:$MISSING_FOLDERS_LIST
Missing Files:$MISSING_FILES_LIST"
source send_email.sh "$EMAIL" "$SUBJECT" "$BODY"


