/* Copyright (C) 2004 Stefan Bellon <sbellon@sbellon.de>
 *
 * This file is part of RemotePrinterFS.
 *
 * RemotePrinterFS 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.
 *
 * RemotePrinterFS 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 <stdlib.h>
#include <kernel.h>

#include <oslib/osfscontrol.h>

/* Kludge in order to pull in headers before including config.h/rpfs.h */
#include <oslib/os.h>
#include <kernel.h>
#define _kernel_oserror os_error const

#include "config.h"
#include "syslogf.h"

/* Base address of module, imported from assembler file */
extern int Image__RO_Base;

static void
remoteprinterfs_finalise(void)
{
    xosfscontrol_remove_fs(FilingSystemName);

    syslogf(LOG_INFO, "RemotePrinterFS stopped.");
}

static os_error const *
declare_remoteprinterfs(void *pw)
{
    osfscontrol_fs_info_block info_block = {
        (int)FilingSystemName           - (int)Image__RO_Base, // name_offset
        (int)FilingSystemName           - (int)Image__RO_Base, // banner_offset
        (int)fsentry_open               - (int)Image__RO_Base, // open_offset
        (int)fsentry_getbytes           - (int)Image__RO_Base, // get_bytes
        (int)fsentry_putbytes           - (int)Image__RO_Base, // put_bytes
        (int)fsentry_args               - (int)Image__RO_Base, // args_offset
        (int)fsentry_close              - (int)Image__RO_Base, // close_offset
        (int)fsentry_file               - (int)Image__RO_Base, // file_offset
        fileswitch_SUPPORTS_SPECIAL     |
        fileswitch_SUPPORTS_EMPTY_NAMES |
        fileswitch_NEEDS_CREATE         |
        FilingSystemNumber,                                    // info
        (int)fsentry_func               - (int)Image__RO_Base, // func_offset
        (int)fsentry_gbpb               - (int)Image__RO_Base, // gbpb_offset
        0                                                      // extra_info
    };

    return xosfscontrol_add_fs
      ((const byte *)Image__RO_Base, (int)&info_block - Image__RO_Base, pw);
}

os_error const *
remoteprinterfs_initialise(const char *cmd_tail, int podule_base, void *pw)
{
    os_error const *err;

    UNUSED(cmd_tail);
    UNUSED(podule_base);

    syslogf(LOG_INFO, "RemotePrinterFS %s starting ...", Module_VersionString);

    err = declare_remoteprinterfs(pw);

    if (!err)
        atexit(remoteprinterfs_finalise);

    return err;
}

void remoteprinterfs_service(int service_number, _kernel_swi_regs *r, void *pw)
{
    UNUSED(service_number);
    UNUSED(r);

    declare_remoteprinterfs(pw);
}
