#!/usr/bin/perl

# Convert comma seperated excel files to tab seperated files.

foreach $arg (@ARGV) {
  print STDERR "Reading $arg ...\n";
  $out = lc($arg);
  $out =~ s/\.[^.]*$//;
  $out =~ s%^.*/%%;
  $sheet = $out;
  $count=0;
  while (-e "new/$out.tsv") {
    $count++;
    $out = "UNIQUE$count-$sheet";
  }
  if (!(-f $arg && open(INPUT,"<$arg"))) {
    print STDERR "Unable to read $arg.\n";
  }
  elsif (!open(OUTPUT,">new/$out.tsv")) {
    print STDERR "Unable to open $out.\n";
  }
  else {
    print STDERR "Writing new/$out.tsv ...\n";
    while(<INPUT>) {
      $line = $_;
      $line =~ s/,/\t/g;
      print OUTPUT $line;
    }
    close(INPUT);
  }
}
