#! /bin/sh
# file://~/bin/dir.sh
# @author:  www.rzr.online.FR - Rev:$Author$
# Copyright and Licence : http://RzR.online.fr/license.htm
#                                                              -*- Mode: Sh -*-
#------------------------------------------------------------------------------
#set -x
URL="http://rzr.online.fr/contrib.htm"
VERSION="0.0.0"

usage_()
{
    cat<<EOF
Usage: $0 <OPTIONS> <DIR>

==== OPTIONS ====

--tree

--txt
--txt-charset "us-ascii"
--txt-charset-no "us-ascii"

--dir-empty

[Press Return]

==== MISC ===

URL: ${URL}

EOF

}


### find all files not in $1 format
txt_()
{
    d="."
    [ ! -z $1 ] && d="$1" && shift
    find "$d" -type f -exec file --mime '{}' \; \
        | grep  -e".*: text/.*; .*"
}

txt_charset_()
{
    charset="us-ascii"
    [ ! -z $1 ] && charset="$1" && shift
    d="."
    [ ! -z $1 ] && d="$1" && shift
    txt_  "$d" | grep ".*; charset=${charset}$"
}

## find all files not in $1 format
txt_charset_no_()
{
    charset="us-ascii"
    [ ! -z $1 ] && charset="$1" && shift
    d="."
    [ ! -z $1 ] && d="$1" && shift
    txt_  "$d" | grep -v ".*; charset=${charset}$"
}


ls_()
{
    ls -aF "$@"
}

# merged http://rzr.online.fr/docs/contribs/bin/finddirempty.sh
dir_empty_()
{
    d="."
    [ "_" != "_$1" ]  && d="$1"
    find "$d" -depth -type d -empty
#   find "$d/" -type d -exec ${SHELL} $0 --dir-empty '{}' \;
#   v=`ls "$1" | wc -l`
#   echo $v
#   [ "$v" -eq 0 ] && echo $1
}

dir_tree_()
{
    d="."
    [ "_" != "_$1" ]  && d="$1"
    find "$d" | sed 's/[^/]*\//|   /g;s/| *\([^| ]\)/+--- \1/'
}


test_()
{
    clear
    set -x
    mkdir dir
    date > dir/file
    echo "àçsénts suçks" > dir/non-ascii
    mkdir dir-empty
    $0
    $0 --dir-empty .
    $0 --txt .
    $0 --txt-charset .
    $0 --txt-charset-no .
}

main_()
{
#   [ -z $1 ] && usage_

    case $1 in
        -h | --help)
            shift
            usage_ "$@"
            ;;
        --dir-empty)
            shift
            dir_empty_ "$@"
            ;;
        --tree)
            shift
            dir_tree_ "$@"
            ;;
        --txt | --text)
            shift
            txt_ "$@"
            ;;
        --txt-charset)
            shift
            txt_charset_ "$@"
            ;;
        --txt-charset-no)
            shift
            txt_charset_no_ "$@"
            ;;
        --test)
            test_
            ;;
        *)
            dir_tree_ "$@"
            ;;
    esac
}


main_ "$@"

#eof "$Id:$"
