#! perl -wl
die unless defined($package=$ARGV[0]);
$package=~s,^\./,,;
#print STDERR "package=$package";
open FI,'/mit/ghc/src/random/strace-cat-work-categories' or die;
while(<FI>){
    next if /^\s*#/;
    @F=split;
    die unless@F==2;
    $c{$F[0]}=$F[1];
}
while(<STDIN>){
    chomp;
    if (($func,$args,$result)=/^\s*\d+\s+(\w+)\((.*)\)\s+=\s+(.+)/){
        if(defined($cat=$c{$func})){
            if($cat eq 'n'){
                &already($func);
            } elsif($cat eq 'connect'){
                die unless ($args,$result)=/^\s*\d+\s+connect\(\d+,\s*(\{.*\},\s*\d+)\)\s*=\s*(-?\d+.*)/;
                &already("connect(...,$args) = $result");
            }elsif ($cat =~ /^s\d+$/) {
                if($cat eq 's0'){
                    if(($file,$flags)=$args=~/^(0x[0-9a-f]+)(.*)/){
                        $file='/WEIRD0x-'.$file; # very weird open(0x...) seen in cabal-install-1.24.0.2, access(0x...) seen in darcs
                    } else {
                        die "$package $func $args" unless ($file,$flags)=$args =~ /^"((?:[^"\\]|\\.)*)"(.*)/;
                    }
                }elsif($cat eq 's1'){
                    if($args=~/^\w+,\s*NULL/){
                        &already("$func(NULL)");
                        next;
                    }
                    die "$func $args" unless ($file,$flags)=$args =~ /^\w+,\s*"((?:[^"\\]|\\.)*)"(.*)/;
                } else {
                    die;
                }
                #$ofile=$file;
                #&already("$func($file)");
                for($file){
                    s,^//,/,; #maybe it is interesting to note this weirdness separately, but for now we combine them
                    #s,(^/afs/sipb.mit.edu/project/gcc-6plus/.install/go-2016122222-5401-0500/gcc/include/).*,$1...,;
                    #s,(^/afs/sipb.mit.edu/project/gcc-6plus/.install/go-2016122222-5401-0500/gcc/lib/).*,$1...,;
                    #s,(^/afs/sipb.mit.edu/project/gcc-6plus/lib/).*,$1...,;
                    s,(^(/var/tmp/kenta/hdd|/scratch/kenta)/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/lib-separated).*,$1...,;
                    s,(^(/var/tmp/kenta/hdd|/scratch/kenta)/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/lib/ghc-8.0.2).*,$1...,;
                    if(m!libHS[^/]+\.(so|a)$!){
                        $file='/libHS...';
                    }
                    s,(^/tmp/cabal-tmp-)\d+($|/.+),$1...,;
                    s,^/proc/self/task/\d+/comm$,/proc/self/task/.../comm,;
                    s,^/tmp/cc[^/]+$,/tmp/cc...,;
                    s,^(/tmp/ghc)\d+_0($|/.+),$1...,;
                    s,^/tmp/\d+(\.o|\.c|\.c\.gch|)$,/tmp/DIGITS...$1,;
                    s,^(/tmp/pkgConf-).*,$1...,;
                    s,^(/tmp/sh-thd-)\d+$,$1...,;
                    s,^(/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/workarea/home/.cabal/world).*,$1...,;
                    if(($rest)=m,^/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/workarea/buildlogs/(.*)\.log$,){
                        if($rest eq $package){
                            $_='/package/BUILDLOGs/...';
                        }
                    }
                    if(($rest)=m,^/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/workarea/home/.cabal/packages/hackage.haskell.org/.*/.*/(.*)\.tar\.gz$,){
                        if($rest eq $package){
                            $_='/package/cabal/...gz';
                        }
                    }
                    if(($rest)=m,^/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/hackage-bin/versioned/(.*),){
                        if(&isprefix($package,$rest)){
                            $_='/package/hackage-bin...';
                        }
                    }
                    if(($rest)=m,^/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/lib-separated/./x86_64-linux-ghc-8.0.2/(.*),){
                        if(&isprefix($package,$rest)){
                            $_='/package/lib-separated...';
                        }
                    }
                    if(($rest)=m,^/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/share/x86_64-linux-ghc-8.0.2/(.*),){
                        if(&isprefix($package,$rest)){
                            $_='/package/share/...';
                        }
                    }
                    if(($rest)=m,^/var/tmp/kenta/hdd/ghc/afs/sipb.mit.edu/project/ghc/arch/amd64_ubuntu1404/install/8.0.2-lts-8.5-a/share/doc/x86_64-linux-ghc-8.0.2/(.*),){
                        if(&isprefix($package,$rest)){
                            $_='/package/share/doc/...';
                        }
                    }
                    unless(m,^/,){
                        $_='...';
                    }
                }
                unless($result eq '0' or$result =~ /^-\d+ E\w+ \(/){
                    $result='...';
                }
                if($func eq 'open' or $func eq 'openat'){
                    &already("$file & ($flags) $func = $result");
                } else {
                    &already("$file & $func = $result");
                }
            } else {
                die
            }
        } else {
            print "NEW $func($args) = $result"; #connect
        }
    }
    elsif(/\s*\d+\s+--- SIG.* ---$/){
    }
    elsif(/\s*\d+\s+\+\+\+ (exited with \d+|killed by \w+( \(core dumped\))?) \+\+\+$/){
    }
    else{
        print STDERR "ERROR weird line '$_'"}
}

sub already{
    for($_[0]){
        unless($already{$_}){
            print;
        }
        $already{$_}=1;
    }
}

sub isprefix{
    $_[0] eq (substr $_[1],0,(length$_[0]));
}
