/*---------------------------------------------------------------------------------
 libBAG  Copyright (C) 2010 - 2010      
  BassAceGold - <BassAceGold@gmail.com>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any
  damages arising from the use of this software.

  Permission is granted to anyone to use this software for any
  purpose, including commercial applications, and to alter it and
  redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you
     must not claim that you wrote the original software. If you use
     this software in a product, an acknowledgment in the product
     documentation would be appreciated but is not required.
     
  2. Altered source versions must be plainly marked as such, and
     must not be misrepresented as being the original software.
     
  3. Restricting the usage of or portions of code in this library
     from other software developers and or content creators
     is strictly prohibited in applications with software creation capabilities
     and or with the intent to profit from said restrictions.
     
  4. This notice may not be removed or altered from any source
     distribution.
---------------------------------------------------------------------------------*/

#include "filesystem.h"
#include "fs_api.h"
#include <libBAG_Debug.h>

int BAG_Filesystem_GetArgsRoot(char *origPath){
    long len = strlen(origPath) - 1;
    do{
        origPath[len] = '\0';len--;
    }while(origPath[len] != '/' && len > 0);
    //hit root directory
    if(len <= 0){
        len = 0;
        origPath[0] = '/';
        origPath[1] = '\0';
    }
    return len + 1;
}

int BAG_Filesystem_GetArgsEx(const char *file, char **filebuf, int lim){
    FILE* dat = fopen(file, "rb");
    if(dat){
        filebuf = calloc(lim + 1, sizeof(char*));
        if(!filebuf){
          BAG_DBG_LibMsg(BUFFER_FAILED_MSG, lim+1, "ARGS");
          return ERR_END;
        }

        int i = 0;
        char tempBuf[MAX_PATH];
        while(!feof (dat)){
            fgets(tempBuf, MAX_PATH, dat);
            filebuf[i] = calloc(strlen(tempBuf) + 1, sizeof(char));
            if(!filebuf[i]){
              BAG_DBG_LibMsg(BUFFER_FAILED_MSG, strlen(tempBuf) + 1, "ARGS Single");
              return ERR_END;
            }

            strncpy(filebuf[i], tempBuf, MAX_PATH);
            int len = strlen(filebuf[i]);
            if(filebuf[i][len - 1] == '\n')
                filebuf[i][len - 1] = '\0';
            i++;
            if(lim > -1 && i >= lim)
                break;
        }
        fclose(dat);
        remove(file);
        return i;
    }
    else{
      BAG_DBG_LibMsg(OPEN_FAIL_MSG, file);
    }
    return 0;
}

void BAG_Filesystem_FreeArgs(char **argv, int num){
    if(argv){
        int i = 0;
        for(i = 0; i < num; i++){
            if(argv[i])
                free(argv[i]);
            argv[i] = NULL;
        }
        free(argv);
    }
    argv = NULL;
}

//reads all arguments from the default arg dat file
int BAG_Filesystem_GetArgs(char **argv, int num){
    return BAG_Filesystem_GetArgsEx("/plgargs.dat", argv, num);
}
