<?php
require_once __DIR__ . '/app/bootstrap/init.php';

echo "ROOT_PATH: " . ROOT_PATH . PHP_EOL;
echo "STORAGE_PATH: " . STORAGE_PATH . PHP_EOL;
echo "Is STORAGE_PATH dir? " . (is_dir(STORAGE_PATH) ? 'YES' : 'NO') . PHP_EOL;
echo "Is STORAGE_PATH writable? " . (is_writable(STORAGE_PATH) ? 'YES' : 'NO') . PHP_EOL;
echo "STORAGE Permissions: " . substr(sprintf('%o', fileperms(STORAGE_PATH)), -4) . PHP_EOL;
echo "STORAGE Owner: " . posix_getpwuid(fileowner(STORAGE_PATH))['name'] . PHP_EOL;

$invoicesDir = STORAGE_PATH . '/invoices';
echo "Invoices Dir exists? " . (is_dir($invoicesDir) ? 'YES' : 'NO') . PHP_EOL;
if (is_dir($invoicesDir)) {
    echo "Invoices Writable? " . (is_writable($invoicesDir) ? 'YES' : 'NO') . PHP_EOL;
    echo "Invoices Permissions: " . substr(sprintf('%o', fileperms($invoicesDir)), -4) . PHP_EOL;
    echo "Invoices Owner: " . posix_getpwuid(fileowner($invoicesDir))['name'] . PHP_EOL;
}

echo "Current PHP Process User (whoami): " . exec('whoami') . PHP_EOL;

$testDir = STORAGE_PATH . '/test_browser_' . time();
if (@mkdir($testDir, 0777, true)) {
    echo "SUCCESS: Created $testDir" . PHP_EOL;
    rmdir($testDir);
} else {
    echo "FAILURE: Could not create $testDir" . PHP_EOL;
    print_r(error_get_last());
}
