#!/bin/bash

# Function to execute a command and capture its output and errors
execute_command() {
    local command=$1
    local output=$(eval "$command" 2>&1)
    echo "$output"
}

# Function to generate HTML report
generate_html_report() {
    local hostname=$(hostname)
    local date_time=$(date -u '+%Y-%m-%d_%H-%M-%S_%Z')
    local report_name="${hostname}-${date_time}.html"

    cat <<EOF >"$report_name"
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Linux Incident Response Diagnosis Report</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <h1 class="mt-3">Linux - IR Computer Diagnosis Report</h1>
        <form action="/submit-incident" method="post">
            <fieldset class="form-group">
                <legend>Analyst Notes</legend>
                <textarea class="form-control" rows="5"></textarea>
            </fieldset>
            <fieldset class="form-group">
                <legend>Meta Information</legend>
                <div class="form-group">
                    <label for="reportGenerationDate" class="col-form-label">Report Generation Date (UTC):</label>
                    <input type="text" id="reportGenerationDate" name="reportGenerationDate" class="form-control"
                        value="$(date -u '+%Y-%m-%d %H:%M:%S %Z')" disabled>
                </div>
                <div class="form-group">
                    <label for="incidentHandlerName">Report Generated By:</label>
                    <input type="text" id="incidentHandlerName" name="incidentHandlerName" class="form-control">
                </div>
            </fieldset>
        </form>
    </div>

    <div class="container mt-4">
EOF

    # Loop through the provided commands and add sections to the HTML report
    commands=(
        "ifconfig && ip a|Network Interfaces and IP Addresses."
        "arp -a|ARP Table."
        "hostname|Display the system's hostname."
        "uname -a|Display system information including the kernel version."
        "df -h|Display disk usage."
        "free -m|Display memory usage."
        "ps aux|Display running processes."
        "top -n 1 -o %CPU|Display real-time system statistics."
        "cat /etc/passwd|User Accounts."
        "cat /etc/shadow|Password Information."
        "cat /etc/group|Information about user groups."
        "cat /etc/sudoers|sudoers file content."
        "lastlog|Last Login Information."
        "tail /var/log/auth.log|Authentication logs."
        "tail /var/log/syslog.log|System logs."
        "tail /var/log/demon.log|Demon logs."
        "tail /var/log/apache/access.log|Apache Access Logs."
        "tail /var/log/nginx/access.log|Nginx Access Logs."
        "tail /var/log/mysqld.log|MySQL Server Logs."
        "ps -aux|Detailed Process Information."
        "uptime|System Uptime."
        "cat /proc/meminfo|Memory Information."
        "ps aux|Currently Running Processes."
        "last -f /var/log/wtmp|Login History."
        "cat /etc/resolv.conf|DNS Resolver Configuration."
        "cat /etc/hosts|Display Hosts File Content."
        "ls -alR /proc/*/cwd|List current working directories of processes."
        "iptables -L -v -n|Display Firewall Rules."
        "service --status-all|List All Available Services."
        "find / -type f -size +512k -exec ls -lh {} \;|Find and list large files on the system."
        "netstat -punta|Network Statistics."
        "echo \$PATH|Display the system's PATH environment variable."
    )

    for command_info in "${commands[@]}"; do
        IFS="|" read -r command description <<<"$command_info"
        echo "<div class=\"card mt-3\">" >>"$report_name"
        echo "    <div class=\"card-header bg-dark text-white\">" >>"$report_name"
        echo "        <h5 class=\"mb-0\">$description - $command</h5>" >>"$report_name"
        echo "    </div>" >>"$report_name"
        echo "    <div class=\"card-body\">" >>"$report_name"
        output=$(execute_command "$command")
        echo "        <pre>" >>"$report_name"
        echo "$output" >>"$report_name"
        echo "        </pre>" >>"$report_name"
        echo "    </div>" >>"$report_name"
        echo "</div>" >>"$report_name"
        echo "Command Completed: $command_info"  # Verbose output
    done

    # Closing HTML
    cat <<EOF >>"$report_name"
    </div>
</body>

</html>
EOF

    echo "HTML report generated: $report_name"
}

# Run checks and generate the report
generate_html_report
