// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

// This is a really stupid tool that takes a file or multiple files
// and converts them to C source as a buffer that gets compiled into a
// program the program can read.

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

#include "../filesystem/filesystem.h"
#include "../string/string.h"
#include "../compress/compress.h"

int main(int argc, char *argv[]) {

    if(argc <= 1) {
        cout << "Usage: " << argv[0] << " <file> [file] [...]" << endl;
        return 1;
    }
    
    cout << "BuiltInBuffer *builtinBuffers[] = {" << endl << endl;

    for(int i = 1; i < argc; i++) {
        
        int length = 0;
        char *buf = FileSystem::loadFile(argv[i], &length);

        if(buf) {

            unsigned int compressedLength = 0;
            char *compressedBuf = compressRLE(buf, length, &compressedLength, 4);

            bool compressed = false;

            if(int(compressedLength) < length) {
                delete[] buf;
                buf = compressedBuf;
                length = compressedLength;
                compressed = true;
            } else {
                delete[] compressedBuf;
            }

            cout << "    { \"" << stringEscape(argv[i]) << "\", " << length << ", " << (compressed ? "true" : "false")  << endl;
            cout << "      { ";

            if(length) {

                for(int j = 0; j < length; j++) {
                    if(j % 8 == 0 && j != 0) {
                        cout << endl << "        ";
                    }

                    ostringstream str;
                    str << "0x" << hex << (int(buf[j]) & 0xff);
                    
                    cout << setw(4) << str.str()  << ", ";
                }

            }

            cout << endl << "      } " << endl;
            cout << "    }," << endl;

            delete[] buf;

            cout << endl;
        }

    }

    cout << "};" << endl;

    return 0;
}

