#!/bin/sh -
PATH=/usr/vice/bin:$PATH; export PATH

# lsacl - display AFS or POSIX ACL file information in short format
# Steve Kinzler, steve@kinzler.com, May 09
# https://kinzler.com/me/home.html#unixuni

# NOTE: MacOS-style ACLs using `ls -e` and `chmod` are not supported here yet
# NOTE: NFSv4 ACLs using `nfs4_getfacl` are not supported here yet

case "`fs listacl ${1+"$@"} 2>&1 | head -1`" in
*not\ found*|*Invalid*|*not\ in\ AFS*)

	# display discretionary `getfacl` file information in short format
	getfacl ${1+"$@"} 2>&1 | perl -ne '
		chop;
		$n && print("\n"), $n = 0, next if /^\s*$/;
		$n++, s/\\(\d{3})/chr(oct($1))/ge, print,
					   next if s/^#\s*file: (.*)/$1:\t/i;
		next if /^#/;
		s/\s*#effective:/#/;
		s/^default:/d:/;
		s/^(d:)?user:/$1u:/;
		s/^(d:)?group:/$1g:/;
		s/^(d:)?mask:/$1m:/;
		s/^(d:)?other:/$1o:/;
		s/^/, / if $n++ > 1;
		print;
		END { $n && print "\n" }
	';;

*)	# display AFS `fs listacl` file information in short format
	fs listacl ${1+"$@"} 2>&1 | perl -ne '
		die "'"$0"': fs command is font server, not AFS\n"
			if $. == 1 && /\[-cf /;
		chop;
		$n && print("\n"), $n = 0, next if /^\s*$/;
		$n++, print, next if s/^Access\s*list\s*for (.*) is/$1:\t/i;
		$n && print("\n"), $n = 0,
			s/^File '\''(.*)'\'' /$1:\t/i, print("$_\n"), next
				if s/^fs:\s*//;
		next if /^Normal\s*rights/i;
		s/^  //;
		s/^system:/sys:/;
		s/:members /:m /;
		s/:administrators /:adm /;
		s/:anyuser /:any /;
		s/^/, / if $n++ > 1;
		print;
		END { $n && print "\n" }
	';;
esac
