#!/bin/bash

# Display company banner with current date and time
echo ""
echo "╔══════════════════════════════════════════════════════╗"
echo "║                                                      ║"
echo "║                  Brooklyn Tech Support               ║"
echo "║              System Info Collector v1.0.1            ║"
echo "║  www.brooklynsupport.com | info@brooklynsupport.com  ║"
echo "║                                                      ║"
echo "╚══════════════════════════════════════════════════════╝"
echo ""

# Ask for company name
read -p "Enter Company Name: " company_name

# Gather system information
host_name=$(hostname)
os_name=$(lsb_release -si)
os_version=$(lsb_release -sr)
total_physical_memory=$(free -m | awk '/^Mem:/{print $2}')
hard_drive_size=$(df -h / | awk 'NR==2{print $2}')
cpu=$(lscpu | grep "Model name" | awk -F':' '{print $2}' | xargs)
date_time=$(date +"%Y-%m-%d %H:%M:%S")
user_accounts=$(getent passwd | cut -d: -f1 | tr '\n' ',' | sed 's/,$//')
model=$(cat /sys/class/dmi/id/product_name)
service_tag=$(cat /sys/class/dmi/id/product_serial)
manufacturer=$(cat /sys/class/dmi/id/sys_vendor)
installation_date=$(ls -ld / | awk '{print $6, $7, $8}')
installed_packages=$(dpkg --get-selections | wc -l)

# Get all internal IP addresses
internal_ipv4_addresses=$(ip -o -f inet addr show | awk '{print $4}' | tr '\n' ',' | sed 's/,$//')

# Get external IP address
external_ipv4_address=$(curl -s https://api.ipify.org)

# Combine internal and external IP addresses
ipv4_addresses="${internal_ipv4_addresses},${external_ipv4_address}"

# Check OS distribution
case "$os_name" in
    Ubuntu|Fedora|CentOS)
        db_command="INSERT INTO linux_system_info (company_name, host_name, os_name, user_accounts, model, service_tag, manufacturer, total_physical_memory, hard_drive_size, date_time, os_version, system_type, ipv4_address, cpu, installation_date, installed_packages) VALUES ('$company_name', '$host_name', '$os_name', '$user_accounts', '$model', '$service_tag', '$manufacturer', '$total_physical_memory', '$hard_drive_size', '$date_time', '$os_version', '$system_type', '$ipv4_addresses', '$cpu', '$installation_date', '$installed_packages');"
        ;;
    *)
        echo "Unsupported OS distribution: $os_name"
        db_command="INSERT INTO linux_system_info (company_name, host_name, os_name, user_accounts, model, service_tag, manufacturer, total_physical_memory, hard_drive_size, date_time, os_version, system_type, ipv4_address, cpu, installation_date, installed_packages) VALUES ('$company_name', '$host_name', '$os_name', '$user_accounts', '$model', '$service_tag', '$manufacturer', '$total_physical_memory', '$hard_drive_size', '$date_time', '$os_version', '$system_type', '$ipv4_addresses', '$cpu', '$installation_date', '$installed_packages');"
        ;;
esac

# Prepare data for POST
post_data="company_name=${company_name}&host_name=${host_name}&os_name=${os_name}&user_accounts=${user_accounts}&model=${model}&service_tag=${service_tag}&manufacturer=${manufacturer}&total_physical_memory=${total_physical_memory}&hard_drive_size=${hard_drive_size}&date_time=${date_time}&os_version=${os_version}&system_type=${system_type}&ipv4_address=${ipv4_addresses}&cpu=${cpu}&installation_date=${installation_date}&installed_packages=${installed_packages}"

# Use base64 encoded URL
POST_URL_ENCODED="aHR0cHM6Ly9jb2xsZWN0b3IuYnJvb2tseW50ZWNoLnN1cHBvcnQvbGludXgucHAg"

# Decode the URL before sending
POST_URL=$(echo "$POST_URL_ENCODED" | base64 --decode)

# Send data to the server
response=$(curl -s -X POST -d "$post_data" "$POST_URL")

# Output response
echo "$response"
echo "System information has been sent."
echo "Task Completed!"
