-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgreyhole-dfree
executable file
·55 lines (49 loc) · 1.32 KB
/
greyhole-dfree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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