#!/bin/bash
#########################################################
#                           #
# This is BashStyle-NG                  #
#                           #
# Licensed under GNU GENERAL PUBLIC LICENSE v3      #
#                           #
# Copyright Christopher Roy Bratušek            #
#                           #
#########################################################

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

targets=("${@:2}")
if (( ${#targets[@]} == 0 )); then
    targets=("$PWD")
fi

gen_stat_targets () {
    stat_targets=()
    for t in "${targets[@]}"; do
        if [[ -d "$t" ]]; then
            shopt -s nullglob
            stat_targets+=("$t"/*)
            shopt -u nullglob
        else
            stat_targets+=("$t")
        fi
    done
}

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 "${targets[@]}"
    ;;
    -g | group )
        gen_stat_targets
        stat -c "%n in Group -> %G" "${stat_targets[@]}" 2>/dev/null | column -t
    ;;
    -v | verbose)
        ls -l --group-directories-first "${targets[@]}"
    ;;
    -d | detailed)
        ls -Al --group-directories-first "${targets[@]}"
    ;;
    -p | perm)
        find "${targets[@]}" -maxdepth 1 -printf "Permissions of %f -> %M (%m)\n" 2>/dev/null | column -t
    ;;
    -o | owner)
        gen_stat_targets
        stat -c "%n by User -> %U" "${stat_targets[@]}" 2>/dev/null | column -t
    ;;
    -V | voctal)
        gen_stat_targets
        stat -c "%a %A %F %U %G %s %y %n" "${stat_targets[@]}" 2>/dev/null
    ;;
    -D | doctal)
        find "${targets[@]}" -maxdepth 1 -mindepth 1 -exec stat -c "%a %A %F %U %G %s %y %n" {} + 2>/dev/null
    ;;
esac
