gboudreau/Greyhole

View on GitHub
greyhole-dfree

Summary

Maintainability
Test Coverage
#!/bin/bash

CACHE_DIR="/var/cache/greyhole-dfree"
CACHE_TIME=15 # in seconds

if [[ "$1" == /* ]]; then
    DIR="$1" # absolute path given, ignore working directory
elif [[ "$1" != "-h" ]]; then
    DIR="$(pwd)/$1" # relative path given, use working directory
else
    DIR="$(pwd)" # no path given, use working directory
fi

TMP_FILE="$CACHE_DIR/${DIR//"/"/_}"
RESULT="0 0 1024" # fail-safe default result

if [ ! -e "$CACHE_DIR" ]; then
    mkdir -p "$CACHE_DIR"
fi

if [ ! -e "$TMP_FILE" ]; then
    RESULT=$(/usr/share/greyhole/greyhole-dfree.php "$1")
    echo "$RESULT" > "$TMP_FILE"
else
    MODIFIED=$(stat -c %Y "$TMP_FILE")
    CURRENT=$(date +%s)
    if [ $((CURRENT - MODIFIED)) -lt $CACHE_TIME ]; then
        RESULT=$(cat "$TMP_FILE")
    else
        # update cache by running greyhole-dfree
        RESULT=$(/usr/share/greyhole/greyhole-dfree.php "$1")
        echo "$RESULT" > "$TMP_FILE"
    fi
fi

if [ "$1" == "-h" ] || [ "$2" == "-h" ]; then
    what=(Total: Free:)
    for num in $(echo "$RESULT" | tr " " "\n"); do
        units=(KB MB GB TB)
        i=0
        unit=${units[i]}
        while [ "$num" -ge 1024 ]; do
            (( "num = $num / 1024" ))
            (( i++ ))
            unit=${units[i]}
        done
        if [[ "${what[k]}" != "" ]]; then
            echo -n "${what[k]} ${num}${unit} "
        fi
        (( k++ ))
    done
    echo
else
    echo "$RESULT"
fi