#!/usr/bin/php
<?php
/*
bntfolderstofiles
A tool to rename the images in the Assets folder of the Japanese Food Box db

Copyright (c) 2013, Giacomo M. Vernoni
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// Define the folder name, change for tests
define(ASSETS_FOLDER, "Assets/");

// if the folder doesn't exist, exit with an error and some instructions
if (!file_exists(ASSETS_FOLDER))
{
    echo "Error: folder '".ASSETS_FOLDER."' not found.

Quick instructions:
1) make a backup of your database.

2) right click on the backup database
and select 'Show package contents'

3) copy this script into the Contents/Resources/ folder

4) open Applications/Utilities/Terminal and
- type 'cd' without the quotes
- add a space
- drag the Resources folder to the Terminal window
- hit [return]
- type ./bntfolderstofiles [return]
";
    exit();
}

// Scan the contents of the Assets folder
$folders = scandir(ASSETS_FOLDER);

// Process every image folder
foreach ($folders as $folderName)
{
    // Proceed only if the item does not begin with "." and it's a directory
    if (substr($folderName, 0, 1) <> "." and is_dir(ASSETS_FOLDER.$folderName))
    {
        // Scan the content of the image folder
        $files = scandir(ASSETS_FOLDER.$folderName);
        
        //Every folder contains a single image
        foreach ($files as $fileName)
        {
            // Proceed only if the item does not begin with "." and it's an image
            $ext = substr($fileName, -3);
            if (substr($fileName, 0, 1) <> "." and ($ext == "jpg" | $ext == "png"))
            {
                // Rename the file as the folder, moving one level up
                if (rename(ASSETS_FOLDER."$folderName/$fileName" , ASSETS_FOLDER."$folderName.$ext"))
                {
                    // Delete the folder
                    rmdir(ASSETS_FOLDER.$folderName);
                }
            }
        }
        // Print out the old name and the new name
        echo $fileName." -> $folderName.$ext\n";
    }
}

?>