#include "../lib/crc.h"
#include <stdio.h>
#include <string.h>

void usage() {
  fprintf(stderr, "Usage: checksum <COMMAND> <INPUT>\n");
  fprintf(stderr, "Commands:\n");
  fprintf(stderr, "\tappend - Compute checksum of the file and append it to "
                  "the end of file.\n");
  fprintf(stderr, "\tverify - Verify checksum of the file.\n");
  fprintf(stderr, "\n");
  fprintf(stderr, "Input:\n");
  fprintf(stderr, "\tPath to input file.\n");
}

enum Command {
  CommandUnknown,
  CommandAppend,
  CommandVerify,
};

int main(int argc, char *argv[]) {
  Command command;

  if (argc < 3)
    command = CommandUnknown;
  else if (strcmp(argv[1], "append") == 0)
    command = CommandAppend;
  else if (strcmp(argv[1], "verify") == 0)
    command = CommandVerify;
  else
    command = CommandUnknown;

  switch (command) {
  case CommandAppend:
    if (crc_append(argv[2])) {
      printf("Checksum appended.\n");
    } else {
      fprintf(stderr, "Failed to append checksum.\n");
      return 2;
    }
    break;

  case CommandVerify:
    if (crc_verify(argv[2])) {
      printf("Checksum verified.\n");
    } else {
      fprintf(stderr, "Failed to verify checksum.\n");
      return 3;
    }
    break;

  case CommandUnknown:
  default:
    usage();
    break;
  }

  return 0;
}
