#!/bin/bash
################################################################################
# Script Name: version/4.0.0/UPDATE.sh
# Description: Apply version 4.0.0. Major release migration, binaries, and configs.
# Company/Brand: HiTechCloud by Pho Tue SoftWare Solutions JSC
# Copyright (c) HiTechCloud by Pho Tue SoftWare Solutions JSC
################################################################################
set -euo pipefail

echo "==> HiTechCloud Panel 4.0.0 major update starting..."

# 1. Sync CLI scripts (Rust implementation is authoritative)
if [ -x hitechcloudpanel/sync-hitechcloudcli-scripts.sh ]; then
  echo "==> Syncing hitechcloudcli scripts"
  ./hitechcloudpanel/sync-hitechcloudcli-scripts.sh || true
fi

# 2. Bump version marker
echo "4.0.0" > hitechcloudpanel/version 2>/dev/null || true

# 3. Ensure system directory structure and permission sanity
mkdir -p /etc/hitechcloud/hitechadmin/config /var/log/hitechcloud 2>/dev/null || true
chmod 750 /etc/hitechcloud 2>/dev/null || true

# 4. Reload services that embed version strings
echo "==> Reloading panel and admin services"
systemctl try-reload hitechcloud-panel 2>/dev/null || true
systemctl try-reload hitechcloud-admin 2>/dev/null || true
systemctl try-reload hitechcloud-engine 2>/dev/null || true

echo "==> [4.0.0] HiTechCloud Panel version 4.0.0 applied successfully."
exit 0
