chomp(@text = `cat restrictions`);

$count = 0;
foreach (@text) {
  $count++;
  unless (/(\S+)\s+(\S+)\s+(\S+)/) {
    &syntaxError("\"$_\"\n");
    exit(1);
  }
  $exp  = $1;
  $type = $2;
  $max  = $3;
  if ($exp =~ /^(#\S+)/) {
    unless ($1 =~ /^(#max|#max_any_one_user|#max_any_one_domain)$/) {
      &syntaxError("bad user specification: \"$1\"\n");
      exit(1);
    }
  } else {
    eval {
      /$exp/;
    };
    if ($@) {
      &syntaxError("bad regular expression: \"$1\"\n");
      exit(1);
    }
  }
  unless ($type =~ /^(minute|hour|day|month)$/) {
    &syntaxError("Bad limiting type: \"$type\"\n");
    exit(1);
  } 
  unless ($max =~ /^\d+$/) {
    &syntaxError("Bad limit: \"$max\"\n");
    exit(1);
  }
  $new .= "$_ $max -1 -1\n";
}

open(handle, ">restrictions");
print handle $new;
close(handle);
exit(0);

sub syntaxError {
  print "Invalid usage restrictions file.\n";
  print "Syntax error on line $count.\n";
  print "$_[0]\n";
  print "Each line must take one of the following forms:\n\n";
  print "<regular expression> <minute|hour|day|month> <integer>\n";  
  print "#max                 <minute|hour|day|month> <integer>\n";  
  print "#max_any_one_user    <minute|hour|day|month> <integer>\n";  
  print "#max_any_one_domain  <minute|hour|day|month> <integer>\n\n";  
  print "(without the angle brackets)\n";
  exit(-1);
}
