#!/usr/bin/env php 
<?php 
 
use Symfony\Component\Finder\Finder; 
use Symfony\Component\Finder\SplFileInfo; 
 
require_once(__DIR__ . '/../vendor/autoload.php'); 
 
$rootDir = __DIR__ . '/..'; 
 
$filename = 'php-gameboy.phar'; 
$filePath = $rootDir . '/bin/' . $filename; 
$stub = <<<STUB 
#!/usr/bin/env php 
<?php 
 
Phar::mapPhar("php-gameboy.phar"); 
 
require_once("phar://php-gameboy.phar/php-terminal-gameboy-emulator/boot.php"); 
 
__HALT_COMPILER(); 
 
STUB; 
 
if (file_exists($filePath)) { 
    unlink($filePath); 
} 
 
$finderSort = function ($a, $b) { 
    return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/')); 
}; 
 
function addFile($phar, $file) 
{ 
    $path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/'); 
 
    $content = file_get_contents($file); 
 
    $phar->addFromString($path, $content); 
} 
 
$phar = new Phar( 
    $filePath, 
    FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, 
    $filename 
); 
$phar->setSignatureAlgorithm(\Phar::SHA1); 
$phar->startBuffering(); 
 
addFile($phar, new SplFileInfo('boot.php', $rootDir . '/boot.php', 'boot.php')); 
addFile($phar, new SplFileInfo('composer.json', $rootDir . '/composer.json', 'composer.json')); 
addFile($phar, new SplFileInfo('vendor/autoload.php', $rootDir . '/vendor/autoload.php', 'vendor/autoload.php')); 
 
$finder = new Finder(); 
$finder->files() 
    ->ignoreVCS(true) 
    ->name('*.php') 
    ->in([ 
        $rootDir . '/src/', 
        $rootDir . '/vendor/composer', 
        $rootDir . '/vendor/whatthejeff', 
    ]) 
    ->sort($finderSort) 
; 
 
foreach ($finder as $file) { 
    addFile($phar, $file); 
} 
 
$phar->setStub($stub); 
 
$phar->stopBuffering(); 
 
chmod($filePath, 0775); 
 
 |