/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <init.h>
#include <aio.h>
#include <drv/drv.h>
#include <io.h>
#include <devfs.h>
#include <rootfs.h>
#include <iso9660.h>
#include <sys/stat.h>
#include <stddef.h>

int init_vfs(void)
{
    static const struct aio_mount m[] =
    {
        {
            .mount =
            {
                .tgt = "/",
                .mode = 0755
            },

            .type = "rootfs"
        },

        {
            .mount =
            {
                .tgt = "/dev",
                .mode = 0755
            },

            .type = "devfs"
        }
    };

    static const struct fs_mkdir mk[] =
    {
        {
            .path = "/dev",
            .mode = 0755
        },

        {
            .path = "/mnt",
            .mode = 0755
        },

        {
            .path = "/mnt/cdrom",
            .mode = 0755
        }
    };

    if (rootfs_register()
        || devfs_register()
        || io_mount(m))
        return -1;

    for (size_t i = 0; i < sizeof mk / sizeof *mk; i++)
        if (io_mkdir(&mk[i]))
            return -1;

    for (size_t i = 1; i < sizeof m / sizeof *m; i++)
        if (io_mount(&m[i]))
            return -1;

    return 0;
}
