/* Copyright (C) 1998-2004 Elliott Hughes <enh@jessies.org>
 *               2004 Stefan Bellon <sbellon@sbellon.de>
 *
 * This file is part of JetDirectFS.
 *
 * JetDirectFS 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.
 *
 * JetDirectFS 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
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <socklib.h>

#include "config.h"
#include "connection.h"
#include "errors.h"
#include "statics.h" // because of logfile when ifdef DEBUG
#include "veneers.h"

static int
open_file (char *filename)
{
    /* This assumes that filename starts with ":Unset.$." */
#ifdef DEBUG
    fprintf(logfile, "Opening %s\n", filename+9);
#endif
    return establish_connection(filename+9);
}

os_error const *
fsentry_args(FSEntry_Args_Parameter *parm)
{
    os_error const* err = ERR(err_Unsupported);

#ifdef DEBUG
    fprintf(logfile, "FSEntry_Args(%i)\n", parm->reason);
#endif

    switch( parm->reason )
    {
    case FSEntry_Args_Reason_ReadSequentialFilePointer:
        /* If your filing system does not support a pointer ...*/
        /* it must return a pointer of 0. -- PRM 2-538*/
        parm->arg.sequential_pointer = 0;
        err = 0;
        break;

    case FSEntry_Args_Reason_FlushFileBuffer:
        /* I don't understand why this ever gets called. */
        parm->arg.sequential_pointer = parm->arg.extent = 0;
        err = 0;
        break;

    case FSEntry_Args_Reason_ReadFileDateStamp:
        /* PRM 2-544. */
        parm->arg.sequential_pointer = parm->arg.extent = 0;
        err = 0;
        break;
    }

    return err;
}

os_error *
fsentry_close(FSEntry_Close_Parameter *parm)
{
#ifdef DEBUG
    fprintf(logfile, "FSEntry_Close #%i\n", parm->handle);
#endif

    close(parm->handle);
    return 0;
}

os_error const *
fsentry_file(FSEntry_File_Parameter *parm)
{
    os_error const *err = ERR(err_Unsupported);

#ifdef DEBUG
    fprintf(logfile, "FSEntry_File(%i), \"%s\"\n",
            parm->reason, parm->special_field);
#endif

    switch ( parm->reason )
    {
    case FSEntry_File_Reason_CreateFile:
        err = 0;
        break;

    case FSEntry_File_Reason_SaveFile:
        {
            char *p = parm->extras.location.start;
            char *end = parm->extras.location.end;
            int handle = open_file(parm->name);
            if (handle == -1)
                return error;
            while(*p && p < end){
                socketwrite(handle, p, 1);
                p++;
            }
            close(handle);
        }
        err = 0;
        break;

    case FSEntry_File_Reason_WriteCatalogueInformation:
        /* PRM 2-547. */
        err = 0;
        break;

    case FSEntry_File_Reason_ReadCatalogueInformation:
        /* PRM 2-549. */
        parm->reason = (FSEntry_File_Reason) 0;
        err = 0;
        break;
    }

    return err;
}

os_error const *
fsentry_func(FSEntry_Func_Parameter *parm)
{
    os_error const *err = ERR(err_Unsupported);

#ifdef DEBUG
    fprintf(logfile, "FSEntry_Func(%i), \"%s\"\n",
            parm->reason, parm->special_field_1);
#endif

    switch(parm->reason) {
    case FSEntry_Func_Reason_ReadNameAndBootOptionOfDisc:
        {
            char *p = parm->second_parameter.destination_address;
            *p = strlen(CSD_Unset);
            strcpy(p+1, CSD_Unset);
        }
        err = 0;
        break;

    case FSEntry_Func_Reason_ReadDirectoriesAndInformation:
    case FSEntry_Func_Reason_ReadDirectoryEntries:
        parm->third_parameter.read_number = 0;
        parm->read_offset = -1;
        err = 0;
        break;

    case FSEntry_Func_Reason_SetCurrentDirectory:
    case FSEntry_Func_Reason_ShutDown:
        err = 0;
        break;
    }

    return err;
}


os_error const *
fsentry_gbpb(FSEntry_GBPB_Parameter *parm)
{
    parm = parm;
    return ERR(err_Unsupported);
}

os_error const *
fsentry_getbytes(FSEntry_GetBytes_Parameter *parm)
{
    parm = parm;
    return ERR(err_Unsupported);
}

os_error const *
fsentry_open(FSEntry_Open_Parameter *parm)
{
#ifdef DEBUG
    fprintf(logfile, "FSEntry_Open(%i), \"%s\"\n",
            parm->open_definition.reason, parm->open_definition.special_field);
#endif

    if(parm->open_definition.reason == FSEntry_Open_Reason_Update) {
        parm->open_result.handle = open_file(parm->open_definition.filename);
        parm->open_result.information_word = InformationWord_WritePermitted;
        parm->open_result.buffer_size = 0;

        return (parm->open_result.handle == -1) ? error : 0;
    }

    return ERR(err_Unsupported);
}

os_error const *
fsentry_putbytes(FSEntry_PutBytes_Parameter *parm)
{
#ifdef DEBUG
    fprintf(logfile, "FSEntry_PutBytes #%i\n", parm->handle);
#endif
    socketwrite(parm->handle, &(parm->byte_to_put), 1);

    return 0;
}
