#!/bin/bash

# Check if the script is being run by a non-root user.
# If the user ID is not 0 (root), print a message and exit.
(($(id -u))) && { echo "Date can only be changed by root."; exit; }

# Function to validate and prompt for a date input.
DateChk() {
    # Loop until a valid date is entered.
    while [[ -z "${tmp_date}" ]] || ! date -d "${tmp_date}" > /dev/null; do
        read tmp_date  # Read user input for the date.
    done
    # Print the validated date.
    date -d "${tmp_date}"
}

# Prompt the user to confirm if the current date/time is correct.
while [[ -z "${yn}" ]] || [[ "${yn}" != [yYnN]* ]]; do
    read -p "Is it currently $(date)? " yn  # Prompt user for confirmation.
done

# Case statement to handle user's response.
case "${yn}" in
    [yY]*) ;;  # If yes, continue without any action.
    [nN]*)  # If no, prompt for the correct current date/time.
        echo -n "Enter current date/time: "
        # Set the system date to the user-provided date.
        date "$(date -d "$(DateChk)" +%m%d%H%M%Y.%S)"
    ;;
esac

# Prompt the user for the Disaster Recovery (DR) scheduled date.
echo -n "When is DR scheduled? "
sch_date="$(DateChk)"  # Capture the validated DR scheduled date.

# Prompt the user for the date that needs to be shown during the DR.
echo -n "What date does it need to show? "
bkp_date="$(DateChk)"  # Capture the validated backup date.

# Prompt the user for any time adjustment in hours.
read -p "Time adjustment? " time_adj

# Adjust the system date based on the time difference between scheduled and backup dates, plus any time adjustment.
date "$(date -d "$((($(date -d "${bkp_date}" +%s) - $(date -d "${sch_date}" +%s)) / 86400)) days ${time_adj} hours" +%m%d%H%M$(date -d "${bkp_date}" +%Y).%S)"