#!/usr/bin/perl
#
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  if $running_under_some_shell;

if (@ARGV) {
  $infile = shift @ARGV;
  die(2) if (! stat($infile));
  print STDERR "Reading $infile...\n";
  open(SOURCE,'<' . $infile) or mydie(2);
} else {
  open (SOURCE,'<-');
}

if (@ARGV) {
  $outfile = shift @ARGV;
  print STDERR "Writing $outfile...\n";
  open(DEST,'>' . $outfile) or mydie(2);
} else {
  open (DEST,'>-');
}

LINE: while ($line = <SOURCE>) {

  while($line =~ /[\w]*current(?:|->p_pptr)->fs[ug]id\s*=[^=]/) {
    $line =~ s/[\w]*current->(fs[ug]id)\s*=\s*([^\s=][^;]*)/current_set$1($2)/;
  }
#  while($line =~ /([\w]*ta?sk|current|current->p_pptr)->(fs|e|s|)gid\s*=[^=]/) {
#    $line =~ s/([\w]*ta?sk|current|current->p_pptr)->(fs|e|s|)gid\s*=\s*([^\s=][^;]*)/task_set$2gid($1, $3)/;
#  }

  while($line =~ /current_set[^(]*\([^,]*,\s*current_set[^(]*\([^,]*,\s*\S.*\)\s*\)/) {
    $line =~ s/(\s*)(current_set[^(]*\([^,]*,)\s*(current_set[^(]*\([^,]*,)\s*(\S.*)\)\s*\)/$1$2 $4);\n$1$3 $4)/;
  }

  while ($line =~ /[\w]*current->(?:fs[ug]id|ngroups)(?!\s*[-+*\/]?=\s*\w)/) {
    $line =~ s/[\w]*current->(fs[ug]id|ngroups)(?!\s*[-+*\/]?=\s*\w)/current_$1()/g;
  }

  print DEST "$line";
}
