//CSU 480 File System Project
//2/17/06
//Ken Eimer, Brandon Schory, Brian Tobia
//main.cc

#include "disk.h"
#include "filesystem.h"
#include <iostream>
using namespace std;

char* filename = "TESTFILESYSTEM"; // uses this file to simulate disk
int blocksize = 50;  // every block holds 50 bytes
int numblocks = 1000; // suppose the disk has 1000 blocks

// Check to see if "line" starts with "command".
bool StartWith( char* line, char* command ) {
  return ( strncmp(line, command, strlen(command)) == 0 );
}

int main() {
  Disk* disk;
  FileSystem* filesystem;

  // test if the file exists
  FILE* file;
  file = fopen( filename, "r");

  // if exists, open an existing disk and filesystem
  // otherwise, create a new disk and filesystem
  if ( file ) {
    fclose(file);
    file = fopen( filename, "r+" );
    disk = new Disk( file );
    filesystem = new FileSystem( disk, blocksize, false );
  }
  else {
    file = fopen( filename, "w+" );
    disk = new Disk( file, blocksize, numblocks );
    filesystem = new FileSystem( disk, blocksize, true );
  }

  printf( "You have logged into Utopia, a file-system simulator.\n" );
  printf( "Type \"help\" to see a list of commands and their functions.\n" );

  // repeatedly gets and processes user input
  while ( true ) {
    char line[100];
    printf( "Utopia:~/$ " );
    cin.getline(line, 100);
    if ( StartWith( line, "help") ) {
      printf( "help: prints help information.\n" );
      printf( "quit: exits.\n" );
      printf( "ls: lists the existing files.\n" );
      printf( "create filename: creates a new file.\n" );
      printf( "rm filename: deletes a file.\n" );
      printf( "open filename: opens an existing file.\n" );
      printf( "seek offset: moves the file pointer to the given place.\n" );
      printf( "read numbytes: reads some bytes from the file.\n" );
      printf( "write string: writes to the file.\n" );
      printf( "size: gets the size of the file.\n" );
      printf( "close: closes the file.\n" );
    }
    else if ( StartWith( line, "quit" ) ) {
      break;
    }
    else if ( StartWith( line, "ls" ) ) {
      filesystem->ls();
    }
    else if ( StartWith( line, "create" ) ) {
      filesystem->create( line + strlen("create") + 1 );
    }
    else if ( StartWith( line, "rm" ) ) {
      filesystem->rm( line + strlen("rm") + 1 );
    }
    else if ( StartWith( line, "open" ) ) {
      filesystem->open( line + strlen("open") + 1 );
    }
    else if ( StartWith( line, "seek" ) ) {
      filesystem->seek( atoi(line + strlen("seek") + 1) );
    }
    else if ( StartWith( line, "read" ) ) {
      int length = atoi(line + strlen("read") + 1);
      char* buffer = new char[length + 1];
      buffer[length] = 0;
      filesystem->read(length, buffer );
      printf( buffer );
      printf("\n");
      delete buffer;
    }
    else if ( StartWith( line, "write" ) ) {
      filesystem->write( line + strlen("write") + 1 );
    }
    else if ( StartWith( line, "size" ) ) {
      int x = filesystem->size();
      if(x == -1) printf("No file open\n");
      else printf("%i Bytes\n", x);
    }
    else if ( StartWith( line, "close" ) ) {
      filesystem->close();
    }
    else {
      printf( "Invalid Command\n" );
    }
  }

  delete filesystem;
  delete disk;
  return 0;
}

