/*
 * fileutil.cpp by Matthias Braun <matze@braunis.de>
 *
 * Copyright (C) 2002 Atomic Blue (info@planeshift.it, http://www.atomicblue.org) 
 *
 *
 * 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 (version 2 of the License)
 * 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.
 *
 */

// All OS specific stuff should be in this file
#define CS_SYSDEF_PROVIDE_DIR
#include <psconfig.h>

#include <sys/stat.h>
#include <csutil/util.h>
#include <iutil/vfs.h>
#include <iutil/databuff.h> 

#include "fileutil.h"

#include "updaterglobals.h"

namespace updater
{

FileStat* StatFile (const char* path)
{
    struct stat filestats;
    if (stat (path, &filestats) < 0)
    return NULL;

    FileStat* filestat = new FileStat;

#ifdef CS_PLATFORM_WIN32
    if (filestats.st_mode & _S_IFDIR)
    filestat->type = FileStat::TYPE_DIRECTORY;
    else
    filestat->type = FileStat::TYPE_FILE;
#else
    if (S_ISDIR (filestats.st_mode))
    filestat->type = FileStat::TYPE_DIRECTORY;
    else
    filestat->type = FileStat::TYPE_FILE;
#endif

    // XXX: Need support for symbolic links and executable
    filestat->link = false;
    filestat->executable = false;
    filestat->size = (uint32_t) filestats.st_size;
    filestat->readonly = !(filestats.st_mode & S_IWRITE);

    return filestat;
}

void RemoveFile (const char* filename)
{
    //If vfs deletefile fails, fall back on old variant
    if (!psupdaterengine->GetVFS()->DeleteFile(filename))
    {
        int rc = remove(filename);
        if (rc < 0)
        {
            csString format;
            format.Format("Removal of '%s' failed.", filename);
            psupdaterengine->OutToScreen(format);
        }
    }
    else
    {
        if (!psupdaterengine->GetVFS()->Sync())
        {
            printf("Couldn't sync VFS!\n");
        }
    }
}

#ifdef CS_PLATFORM_UNIX
#include <sys/stat.h>

char* ConvertToSystemPath (const char* path)
{
    return csStrNew (path);
}

void MakeDirectory (const char* directory)
{
  char * destpath;
    //append the /this/ path, since our assertion only works for relative paramaters
  csRef<iVFS> vfs = psupdaterengine->GetVFS();
  if (vfs){
    csRef<iDataBuffer> prefixpath = vfs->GetRealPath("/this/");
    csString destpath(prefixpath->GetData());
    destpath += directory;
  
    if (mkdir (destpath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0)
      if(!vfs->Exists(csString("/this/") + directory))
        psupdaterengine->CriticalError("Couldn't create directory '%s'.", (const char *)destpath);
  } else {
    psupdaterengine->CriticalError("I've lost VFS! Whatever shall I do? Alas, I die.");
  }


}
#endif

#ifdef CS_PLATFORM_WIN32
#include <direct.h>

char* ConvertToSystemPath (const char* path)
{
    char* newpath = csStrNew(path);

    for (char*p = newpath; *p != 0; p++)
    {
    if (*p == '/')
        *p = '\\';
    }

    return newpath;
}

void MakeDirectory (const char* directory)
{
    char* path = ConvertToSystemPath (directory);
    int rc = mkdir(path);
    delete[] path;

    if (rc < 0)
        if(!psupdaterengine->GetVFS()->Exists(csString("/this/") + directory))
            printf("Couldn't create directory '%s'.", directory);
}
#endif

} // end of namespace updater

