#!/usr/bin/env perl
# $Id: sels,v 1.3 2017/09/27 23:57:52 friedman Exp $

$^W = 1; # enable warnings

use FindBin;
use lib "$FindBin::Bin/../../../lib/perl";
use lib "$ENV{HOME}/lib/perl";

use NF::FmtCols;
use Symbol;
use strict;

sub startproc
{
  my ($rfh, $wfh) = (gensym, gensym); # readhandle, writehandle
  pipe ($rfh, $wfh);

  my $pid = fork;
  die "fork: $!\n" unless defined $pid;

  if ($pid == 0) # child
    {
      open (*STDOUT{IO}, ">&" . fileno ($wfh));
      close ($rfh);
      close ($wfh);
      local $SIG{__WARN__} = sub { 0 };
      exec (@_) || die "exec: $_[0]: $!";
    }
  else
    {
      close ($wfh);
      return $rfh;
    }
}

sub main
{
  # In older versions of ls that still understood --lcontext
  # (e.g. coreutils 8.12), "-lZ" and "-Zl" would print different columns!
  # The former would omit nlinks, size, and timestamp, and would display
  # the selinux context *after* user and group.
  # By coreutils 8.12 this seems to be fixed, but use -Zl for compatibility.
  my @lscmd = qw(/bin/ls -Zl);
  push @lscmd, @_;

  my $fh = startproc (@lscmd);
  my @lines = map { chomp; $_; } <$fh>;
  close ($fh);
  wait;

  my $fmt = NF::FmtCols->new
    ( output_style            => 'plain',
      numeric_regexp          => '^[-+]?[\d,.:]+%?$',
      right_justify_numeric_p => 1,
      skip_leading_whitespace => 1,
      format_empty_rows       => 0,
    );

  if ($lines[0] =~ '^total \d')
    {
      print $lines[0], "\n";
      shift @lines;
    }

  $fmt->read_from_array (\@lines);
  $fmt->output;

}

main (@ARGV);

# eof