﻿#!/usr/bin/env bash

set -e

echo "======================================"
echo " LinduAI Hermes macOS Installer"
echo " License Activation Test"
echo "======================================"
echo ""

API_URL="https://linduai.com/api/installer/activate"

echo "Please enter your LinduAI license key:"
read -r LICENSE_KEY

if [ -z "$LICENSE_KEY" ]; then
  echo "License key is required."
  exit 1
fi

echo ""
echo "Detecting device ID..."

RAW_DEVICE_ID=$(ioreg -rd1 -c IOPlatformExpertDevice 2>/dev/null | awk -F\" '/IOPlatformUUID/{print $4}')

if [ -z "$RAW_DEVICE_ID" ]; then
  RAW_DEVICE_ID=$(hostname)
fi

DEVICE_ID=$(printf "%s" "linduai-${RAW_DEVICE_ID}" | shasum -a 256 | awk '{print $1}')
DEVICE_NAME=$(hostname)
OS_NAME="macos"

echo "Device name: $DEVICE_NAME"
echo "Device ID: $DEVICE_ID"
echo ""

echo "Activating license..."

RESPONSE=$(curl -s -X POST "$API_URL" \
  -H "Content-Type: application/json" \
  -d "{
    \"license_key\": \"$LICENSE_KEY\",
    \"device_id\": \"$DEVICE_ID\",
    \"device_name\": \"$DEVICE_NAME\",
    \"os\": \"$OS_NAME\"
  }")

echo "$RESPONSE"
echo ""

if echo "$RESPONSE" | grep -q '"success":true'; then
  echo "License activation successful."
  echo ""
  echo "Next step:"
  echo "Hermes installation will be added in the next version."
  echo ""
  echo "For now, this script only tests LinduAI license activation."
  exit 0
else
  echo "License activation failed."
  echo "Please check your license key or contact LinduAI support."
  exit 1
fi
