<?php

$output        = 'README.md';
$per_row       = 5;
$files         = glob( '*.{png,gif,jpg,jpeg}', GLOB_BRACE );
$listing       = [];
$per_row_width = floor( 100 / $per_row ) . '%';

sort( $files );

if ( count( $files ) < 1 ) {
    die( 'No images to continue with.' );
}

foreach ( $files as $file ) {
    $first = trim( $file[0] );

    if ( preg_match( '/([^a-zA-Z:])/', $first ) ) {
        $first = '\[^a-zA-Z:\]';
    }

    if ( ! array_key_exists( $first, $listing ) ) {
        $listing[ $first ] = [];
    }

    $listing[ $first ][] = $file;
}

$contents = "# Slack emotes\n\n";

foreach ( $listing as $header => $icons ) {
    $contents .= sprintf( "## %s\n\n", $header );

    $chunks = array_chunk( $icons, $per_row );

    $contents .= '<table style="text-align: center;width: 100%">' . "\n";

    foreach ( $chunks as $chunk_icons ) {
        $contents .= "<tr>\n";

        foreach ( $chunk_icons as $icon ) {
            [ $name, $ext ] = explode( '.', $icon, 2 );

            $format   = '<td style=\'width: %s\'><img width=\'30\' src="%2$s" alt="%2$s"><br><kbd>:%3$s:</kbd></td>';
            $contents .= sprintf( $format, $per_row_width, $icon, $name ) . "\n";
        }

        $contents .= "</tr>\n";
    }

    $contents .= "</table>\n\n";
}

$contents .= "\n\n Generated: " . date('c');

file_put_contents( $output, $contents );
