#include <stdio.h>
#include <stdlib.h>
#include "datedit.h"

int main(int argc, char *argv[])
{
    printf("StdDatEditor 0.1 (based on DAT Editor 0.21)\n\n");
    DATInfo info;
    int writeOperation = 0, extraParam; // Flag to write file and extra param for general use
    
    // Remove program path
    argv++;
    argc--;

    // Display arguments if no arguments
    if (argc == 0)
    {
       printf(
       "General syntax: StdDatEdit DATFILENAME.dat.ngc WADFILENAME.wad.ngc {params 1} {params 2}\n"
       "Possible params:\n"
       "\t-list\n\t\tDisplay a detailed list with the info of the DAT\n"
       "\t-quicklist\n\t\tDisplay a simple list with all filenames\n"
       "\t-extract_num NUMBER OUTPUTFILENAME\n\t\tExtract a file by his entry number in the DAT and write to OUTPUTFILENAME (*default* in OUTPUTFILENAME will extract with the same name as in the FSB)\n"
       "\t-extract_qbkey SOMESTRING OUTPUTFILENAME\n\t\tCalculates the QBKey of SOMESTRING, finds it in the DAT file and extracts the file. For OUTPUTFILENAME see above.\n"
       "\t-extract_all\n\t\tExtracts all files in the DAT/WAD with his default filenames\n"
       "\t-replace_num NUMBER INPUTFILENAME\n\t\tReplaces the content of the entry NUMBER with the content of INPUTFILENAME\n"
       "\t-replace_qbkey SOMESTRING INPUTFILENAME\n\t\tCalculates the QBKey of SOMESTRING, finds it in the DAT file and replaces his content with the content of INPUTFILENAME\n");
    }
    
    // Check arguments
    if (argc < 2)
    {
       printf("ERROR: Not enough command line arguments (expected 2 or more, got %i)!\n", argc);
       exit(-1);
    }
    
    // Set filenames
    info.datFileName = argv[0];
    info.wadFileName = argv[1];
    
    // Remove arguments
    argv += 2;
    argc -= 2;
    
    // Read file
    ReadDatInfo(&info);
    
    while (argc != 0)
    {
        // Check params
        if (strcmp(argv[0], "-list") == 0)
        {
           // Display
           MakeAudioList(&info);
           
           // Remove argument
           argv++;
           argc--;
        }
        else if (strcmp(argv[0], "-quicklist") == 0)
        {
           MakeAudioListSimple(&info);
           
           // Remove argument
           argv++;
           argc--;
        }
        else if (strcmp(argv[0], "-extract_num") == 0)
        {
            if (argc < 3)
            {
               printf("ERROR: Not enough command line arguments for extract_num (expected 3, got %i)!\n", argc);
               exit(-1);
            }
            
            if (CheckFileIdExists(&info, atoi(argv[1])) == 0)
            {
               ExtractWadFile(&info, atoi(argv[1]), argv[2]);
            }
             
            // Remove arguments
            argv += 3;
            argc -= 3;
        }
        else if (strcmp(argv[0], "-extract_qbkey") == 0)
        {
            if (argc < 3)
            {
               printf("ERROR: Not enough command line arguments for extract_num (expected 3, got %i)!\n", argc);
               exit(-1);
            }
            
            if (QbKeyStrToFileId(&info, argv[1], &extraParam) == 0)
            {
               ExtractWadFile(&info, extraParam, argv[2]);
            }
             
            // Remove arguments
            argv += 3;
            argc -= 3;
        }        
        else if (strcmp(argv[0], "-extract_all") == 0)
        {
             ExtractAllFiles(&info);
             
             // Remove argument
             argv++;
             argc--;
        }
        else if (strcmp(argv[0], "-replace_num") == 0)
        {
            if (argc < 3)
            {
               printf("ERROR: Not enough command line arguments for replace_num (expected 3, got %i)!\n", argc);
               exit(-1);
            }
            
            if (CheckFileIdExists(&info, atoi(argv[1])) == 0)
            {
               ReplaceWadItem(&info, atoi(argv[1]), argv[2]);
            
               writeOperation = 1;
            }
             
            // Remove arguments
            argv += 3;
            argc -= 3;
        }
        else if (strcmp(argv[0], "-replace_qbkey") == 0)
        {
            if (argc < 3)
            {
               printf("ERROR: Not enough command line arguments for replace_num (expected 3, got %i)!\n", argc);
               exit(-1);
            }
            
            if (QbKeyStrToFileId(&info, argv[1], &extraParam) == 0)
            {
               ReplaceWadItem(&info, extraParam, argv[2]);
               
               writeOperation = 1;
            }
             
            // Remove arguments
            argv += 3;
            argc -= 3;
        }
        else
        {
            printf("Unknown command line argument \"%s\"\n", argv[0]);
            exit(-1);
        }
    }
    
    // Write to file if required
    if (writeOperation == 1)
       WriteOut(&info);
    
    // Free info
    FreeDatInfo(&info);
    
    printf("\n");
    
	return 0;
}

