#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';

use EnumGenerator\Factory;

if (empty($argv[1])) {
    fputs(STDERR, "Pass the argument.\n");
    exit(1);
}

$options = getopt('i:o:f', ['in:', 'out:', 'force']);

if ($options === false) {
    fputs(STDERR, "Invalid argument.\n");
    exit(1);
}

$inFile = $options['i'] ?? $options['in'] ?? '';
if (empty($inFile)) {
    fputs(STDERR, "-i, --in options is required.\n");
    exit(1);
}
$outDir = $options['o'] ?? $options['out'] ?? '';

$isForce = false;
if (isset($options['f']) || isset($options['force'])) {
    $isForce = true;
}

try {
    $filename = realpath($inFile);
    if (!file_exists($filename)) {
        fputs(STDERR, "File not found.\n");
        exit(1);
    }

    $enum = Factory::create($filename);

    if (empty($outDir)) {
        $enum->generate();
    } else {
        $enum->generateFile($outDir, $isForce);
        echo "The class files was output in [{$outDir}].\n";
    }
} catch (Exception $e) {
    fputs(STDERR, $e->getMessage() . PHP_EOL);
}
