#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "schema.h"

int main(){
  char attr_name[MAX_NAME], rel_name[MAX_NAME];
  char file_name[MAX_NAME], s_file[MAX_NAME];
  int rel_card = 0, attr_code = 0, type_size = 0;
  int num_attrs = 0, i=0, len=0;
  FILE *schema_file = NULL;
  
  
  // initialize all char array to have 0
  memset(attr_name, 0, MAX_NAME);
  memset(rel_name, 0, MAX_NAME);
  memset(file_name, 0, MAX_NAME);

  printf("Enter the relation name: ");
  scanf("%s", rel_name);
  sprintf(s_file, "schemas/%s.sf", rel_name);
  schema_file = fopen(s_file, "w");
  fprintf(schema_file, "%s ", rel_name);
  
  printf("Enter the cardinality: ");
  scanf("%d", &rel_card);
  fprintf(schema_file, "%d ", rel_card);
  
  /*
   * printf("Enter the name of the storage file: ");
   * scanf("%s", file_name);
   */
  sprintf(file_name, "databases/%s.sto", rel_name);
  fprintf(schema_file, "%s ", file_name);
   
  printf("Enter the number of attributes: ");
  scanf("%d", &num_attrs);
  fprintf(schema_file, "%d ", num_attrs);
  
  len = num_attrs;
  for (i=0; i < len; ++i){
    printf("\t Reading attribute %d.\n", (i+1));
    printf("\t Enter the attribute name: ");
    scanf("%s", attr_name);
    fprintf(schema_file, "%s ", attr_name);
    memset(attr_name, 0, MAX_NAME);

    printf("\t Enter the attribute type code number: ");
    scanf("%d", &attr_code);
    fprintf(schema_file, "%d ", attr_code);
    
    printf("\t Enter the attribute type size: ");
    scanf("%d", &type_size);
    fprintf(schema_file, "%d ", type_size);
  }
  fclose(schema_file);
  printf("Done");
  exit(0);
}
