/*
 * Copyright (c) 2013, 2014, 2015, 2016, 2023 Jonas 'Sortie' Termansen.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * fatfs.cpp
 * The File Allocation Table (FAT) filesystem.
 */

#include <sys/stat.h>
#include <sys/types.h>

#include <assert.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#if defined(__sortix__)
#include "fsmarshall.h"
#else
#include "fuse.h"
#endif

#include "block.h"
#include "device.h"
#include "fat.h"
#include "fatfs.h"
#include "filesystem.h"
#include "inode.h"
#include "ioleast.h"
#include "util.h"

uid_t request_uid;
uid_t request_gid;

// TODO: Encapsulate.
void StatInode(Inode* inode, struct stat* st)
{
	memset(st, 0, sizeof(*st));
	st->st_ino = inode->inode_id;
	st->st_mode = inode->Mode();
	st->st_nlink = 1; // TODO: Encapsulate.
	st->st_uid = inode->UserId();
	st->st_gid = inode->GroupId();
	st->st_size = inode->Size();
	// TODO: Encapsulate.
	// TODO: For the root dir, mount time, or maybe volume label or first file?
	//       Or maybe the time of the mount point?
	time_t mtime = 0;
	if ( inode->dirent )
	{
		struct tm tm;
		memset(&tm, 0, sizeof(tm));
		tm.tm_sec = ((inode->dirent->modified_time >> 0) & 0x1F) * 2;
		tm.tm_min = (inode->dirent->modified_time >> 5) & 0x3F;
		tm.tm_hour = (inode->dirent->modified_time >> 11) & 0x1F;
		tm.tm_mday = (inode->dirent->modified_date >> 0) & 0x1F;
		tm.tm_mon = ((inode->dirent->modified_date >> 5) & 0xF) - 1;
		tm.tm_year = ((inode->dirent->modified_date >> 9) & 0x7F) + 80;
		mtime = mktime(&tm);
	}
	st->st_atim.tv_sec = mtime; // TODO: The actual accessed time;
	st->st_atim.tv_nsec = 0;
	st->st_ctim.tv_sec = mtime; // TODO: Probably fine to keep as modified time.
	st->st_ctim.tv_nsec = 0;
	st->st_mtim.tv_sec = mtime;
	st->st_mtim.tv_nsec = 0;
	st->st_blksize = inode->filesystem->bytes_per_sector *
	                 inode->filesystem->bpb->sectors_per_cluster;
	// TODO: Encapsulate.
	st->st_blocks = 0; // TODO inode->data->i_blocks;
}

static void compact_arguments(int* argc, char*** argv)
{
	for ( int i = 0; i < *argc; i++ )
	{
		while ( i < *argc && !(*argv)[i] )
		{
			for ( int n = i; n < *argc; n++ )
				(*argv)[n] = (*argv)[n+1];
			(*argc)--;
		}
	}
}

static void help(FILE* fp, const char* argv0)
{
	fprintf(fp, "Usage: %s [OPTION]... DEVICE [MOUNT-POINT]\n", argv0);
}

static void version(FILE* fp, const char* argv0)
{
	fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
}

int main(int argc, char* argv[])
{
	const char* argv0 = argv[0];
	const char* pretend_mount_path = NULL;
	bool foreground = false;
	bool read = false;
	bool write = false;
	for ( int i = 1; i < argc; i++ )
	{
		const char* arg = argv[i];
		if ( arg[0] != '-' || !arg[1] )
			continue;
		argv[i] = NULL;
		if ( !strcmp(arg, "--") )
			break;
		if ( arg[1] != '-' )
		{
			while ( char c = *++arg ) switch ( c )
			{
			case 'r': read = true; break;
			case 'w': write = true; break;
			default:
				fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
				help(stderr, argv0);
				exit(1);
			}
		}
		else if ( !strcmp(arg, "--help") )
			help(stdout, argv0), exit(0);
		else if ( !strcmp(arg, "--version") )
			version(stdout, argv0), exit(0);
		else if ( !strcmp(arg, "--background") )
			foreground = false;
		else if ( !strcmp(arg, "--foreground") )
			foreground = true;
		else if ( !strcmp(arg, "--read") )
			read = true;
		else if ( !strcmp(arg, "--write") )
			write = true;
		else if ( !strncmp(arg, "--pretend-mount-path=", strlen("--pretend-mount-path=")) )
			pretend_mount_path = arg + strlen("--pretend-mount-path=");
		else if ( !strcmp(arg, "--pretend-mount-path") )
		{
			if ( i+1 == argc )
			{
				fprintf(stderr, "%s: --pretend-mount-path: Missing operand\n", argv0);
				exit(1);
			}
			pretend_mount_path = argv[++i];
			argv[i] = NULL;
		}
		else
		{
			fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
			help(stderr, argv0);
			exit(1);
		}
	}

	// It doesn't make sense to have a write-only filesystem.
	read = read || write;

	// Default to read and write filesystem access.
	if ( !read && !write )
		 read = write = true;

	if ( argc == 1 )
	{
		help(stdout, argv0);
		exit(0);
	}

	compact_arguments(&argc, &argv);

	const char* device_path = 2 <= argc ? argv[1] : NULL;
	const char* mount_path = 3 <= argc ? argv[2] : NULL;

	if ( !device_path )
	{
		help(stderr, argv0);
		exit(1);
	}

	if ( !pretend_mount_path )
		pretend_mount_path = mount_path;

	int fd = open(device_path, write ? O_RDWR : O_RDONLY);
	if ( fd < 0 )
		err(1, "%s", device_path);

	// Read the bios parameter block from the filesystem so we can verify it.
	struct fat_bpb bpb;
	if ( preadall(fd, &bpb, sizeof(bpb), 0) != sizeof(bpb) )
	{
		if ( errno == EEOF )
			errx(1, "%s: Isn't a valid FAT filesystem (too short)", device_path);
		else
			err(1, "read: %s", device_path);
	}

	// Verify the boot.
	if ( !(bpb.boot_signature[0] == 0x55 && bpb.boot_signature[1] == 0xAA) )
		errx(1, "%s: Isn't a valid FAT filesystem (no boot signature)", device_path);

	// Verify the jump instruction at the start of the boot sector.
	if ( !(bpb.jump[0] == 0xEB && bpb.jump[2] == 0x90) &&
	     !(bpb.jump[0] == 0xE9) )
		errx(1, "%s: Isn't a valid FAT filesystem (bad jump)", device_path);

	// TODO: Validate all parameters make sense.

	uint16_t bytes_per_sector =
		bpb.bytes_per_sector_low | bpb.bytes_per_sector_high << 8;
	uint16_t root_dirent_count =
		bpb.root_dirent_count_low | bpb.root_dirent_count_high << 8;
	uint32_t root_dir_sectors =
		divup<uint32_t>(root_dirent_count * sizeof(fat_dirent), bytes_per_sector);
	uint32_t sectors_per_fat =
		bpb.sectors_per_fat ? bpb.sectors_per_fat : bpb.fat32_sectors_per_fat;
	uint32_t total_sectors =
		bpb.total_sectors_low | bpb.total_sectors_high << 8;
	if ( !total_sectors )
		total_sectors = bpb.total_sectors_large;

	uint32_t data_offset =
		bpb.reserved_sectors + bpb.fat_count * sectors_per_fat + root_dir_sectors;
	uint32_t data_sectors = total_sectors - data_offset;
	uint32_t cluster_count = data_sectors / bpb.sectors_per_cluster;

	uint8_t fat_type =
		cluster_count < 4085 ? 12 : cluster_count < 65525 ? 16 : 32;

	// Verify the filesystem version.
	if ( fat_type == 32 && bpb.fat32_version != 0x0000 )
		errx(1, "%s: Unsupported filesystem version 0x%04x", device_path,
		     bpb.fat32_version);

	// TODO: On FAT16/32 check FAT entry 1 for the high bits to see if fsck is needed.
	// Check whether the filesystem was unmounted cleanly.
	//if ( !(fat[1] & FAT_UNMOUNTED) || !(fat[1] & FAT_NO_IO_ERR) )
	//	warn("warning: %s: Filesystem wasn't unmounted cleanly\n", device_path);

	// TODO: The FAT and clusters are not aligned to cluster size so
	//       we can't use the cluster size here. Perhaps refactor the
	//       device so we can deal with whole clusters.
	Device* dev = new Device(fd, device_path, bytes_per_sector, write);
	if ( !dev ) // TODO: Use operator new nothrow!
		err(1, "malloc");
	Filesystem* fs = new Filesystem(dev, pretend_mount_path);
	if ( !fs ) // TODO: Use operator new nothrow!
		err(1, "malloc");

	if ( !mount_path )
		return 0;

#if defined(__sortix__)
	return fsmarshall_main(argv0, mount_path, foreground, fs, dev);
#else
	return fat_fuse_main(argv0, mount_path, foreground, fs, dev);
#endif
}
