#!/usr/bin/perl -w

my @courses_json;
my $filename = shift;
open (FILE,$filename) or die "$!";
@courses_json = <FILE>;
close FILE;

my $line_count = scalar @courses_json;
for ($i = 0; $i < $line_count; $i++)
{
  my $line = $courses_json[$i];
  if ($line =~ m:\"content\".*http.*faqs/(.*)\":)
  {
    my $file = "../".$1;
    $file =~ s:%20: :g;
    print "$file\n";
    open (SHA1,"sha1sum -b $file|") or die "$!";
    my $sha1 = <SHA1>;
    close SHA1;
    $sha1 =~ s:\s+.*$::g;
    $sha1 =~ s:\n$::g;
    $sha1 =~ s:\r$::g;
    if ($line =~ m:,:)
    {
      $leading_comma = "";
      $trailing_comma = ",";
    }
    else
    {
      $leading_comma = ",";
      $trailing_comma = "";
    }
    $line .= "$leading_comma \"sha1\":\"$sha1\"$trailing_comma\n";
    $courses_json[$i] = $line;
  }
}

open (FILE,">$filename") or die "$!";
print FILE @courses_json;
close FILE;
