<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!extension_loaded('zip')) {
    echo "ERROR: Zip extension not loaded!\n";
    // Try to load via dl if possible (unlikely in modern PHP)
    // Or check if we can enable it dynamically (unlikely)
    exit(1);
}

$zipPath = __DIR__ . '/libs/tcpdf.zip';
$extractPath = __DIR__ . '/libs/';

if (!file_exists($zipPath)) {
    die("ERROR: $zipPath not found!\n");
}

$zip = new ZipArchive;
if ($zip->open($zipPath) === TRUE) {
    echo "Opened ZIP successfully.\n";
    if ($zip->extractTo($extractPath)) {
        echo "Extracted successfully to $extractPath\n";
        $zip->close();
        
        // Find extracted folder (likely strictly TCPDF-main or TCPDF-6.6.2)
        $folders = glob($extractPath . 'TCPDF*');
        foreach ($folders as $folder) {
            if (is_dir($folder)) {
                $target = $extractPath . 'tcpdf';
                // If target exists, rename/merge? Just rename for now as we don't have it
                if (rename($folder, $target)) {
                    echo "Renamed " . basename($folder) . " to tcpdf\n";
                } else {
                    echo "Failed to rename $folder to $target\n";
                }
                break; // Found one
            }
        }
    } else {
        echo "Extraction failed!\n";
    }
} else {
    echo "Failed to open ZIP!\n";
}
?>
