#!/bin/bash
#############################################
#                                           #
#  Shows Folder size and total files.       #
#  Can be used as a nemo action script.     #
#  Depends on: yad                          #
#                                           #
#  usage:  dirsize                          #
#          dirsize /etc                     #
#                                           #
#  Made by Koentje  (remon@cobrasoft.nl)    #
#                                           #
#############################################

# If no dir is givin as arg, use this dir
if [ "$1" = "" ]; then
  isdir="."
else
  isdir="$1"
fi

# Set mousepointer to hourglass
$HOME/bin/mousepointer hourglass

# Get dirsize
dsize=$(find "$isdir" -type f -printf "%s\n" | awk '{c+=$1}END{print c}' | numfmt --to=iec)

# Get total files of folder and subfolders
tfiles=$(find "$isdir" -type f | wc -l)

# If executed on terminal exit status is 0, else it's 1 (or higher?)
tty -s; es="$?"

# If not executed on terminal, then popup yad message, else echo on terminal
if [ "$es" != "0" ]; then
  echo -e "\n Foldername : $isdir\n Foldersize : $dsize\n Total Files: $tfiles\n" | yad --text-info --margins=6 --back=#181818\
  --title="$isdir" --listen\
  --center --fixed --on-top --wrap\
  --tail --always-print-result --on-top\
  --fontname="Ubuntu Mono 14"\
  --text-align=left\
  --width=600 --height=200\
  --button="OK"
else
  echo -e "\n Foldername : $isdir\n Foldersize : $dsize\n Total Files: $tfiles\n"
fi

$HOME/bin/mousepointer default
