#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright Christopher Bratusek			#
#							#
#########################################################

. gettext.sh
export TEXTDOMAIN="bashstyle-rc"

case ${1} in
	--help | -h | "")
		bashstyle-help -a "Christopher Roy Bratusek" -e "nano@jpberlin.de" -h "https://www.nanolx.org/"\
			-l "GNU GPL v3" -n "list" -s "$(eval_gettext "various ls variants")"\
			-v "${BSNG_VERSION}" -y "${BSNG_YEAR}"\
			-o "$(eval_gettext "single:|one-column ls, directories first")"\
			-o "$(eval_gettext "group:|show group for files in directory")"\
			-o "$(eval_gettext "verbose:|detailed ls without hidden files")"\
			-o "$(eval_gettext "detailed:|detailed ls including hidden files")"\
			-o "$(eval_gettext "voctal:|detailed ls without hidden files (octal perms)")"\
			-o "$(eval_gettext "doctal:|detailed ls including hidden files (octal perms)")"\
			-o "$(eval_gettext "perm:|show plain and octal permissions for files")"\
			-o "$(eval_gettext "owner:|show owner for files in directory")"
	;;

	-s | single)	ls -1 --group-directories-first "${@:2}" ;;
	-g | group )	ls -l --group-directories-first "${@:2}" | \
			  sed -e '1d' | \
			  gawk '{print $9, "in Group ->", $4}' | \
			  column -t ;;
	-v | verbose)	ls -l --group-directories-first "${@:2}" ;;
	-d | detailed)	ls -Al --group-directories-first "${@:2}" ;;
	-p | perm)	find "${@:2}" -maxdepth 1 -printf \
			  "Permissons of %f -> %M (%m)\n" | column -t ;;
	-o | owner)	ls -l --group-directories-first "${@:2}" | \
			  gawk '{print $9, "by User ->", $3}' | \
			  sed -e '1d' | column -t ;;
	-V | voctal)	ls -l --group-directories-first "${@:2}" | \
			  awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}' ;;
	-D | doctal)	ls -Al --group-directories-first "${@:2}" | \
			  awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}' ;;
esac
