/
check
executable file
·117 lines (81 loc) · 2.29 KB
/
check
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
set -e
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
cd ${DIR}
if [ "$1" = "git" ]; then
exec git ${@:2}
elif [ "$1" = "out" ]; then
TIME="$(date --rfc-3339=seconds) OUT"
echo "${TIME}" >> current.txt
git reset > /dev/null
git add current.txt
git commit -m "${TIME}"
elif [ "$1" = "in" ]; then
TIME="$(date --rfc-3339=seconds) IN"
echo "${TIME}" >> current.txt
git reset > /dev/null
git add current.txt
git commit -m "${TIME}"
elif [ "$1" = "rotate" ]; then
TODAY_TXT=$(date --rfc-3339=date).txt
cat current.txt >> ${TODAY_TXT}
: > current.txt
git reset > /dev/null
git add current.txt
git add ${TODAY_TXT}
git commit -m "ROTATE"
elif [ "$1" = "sum" ]; then
file="current.txt"
if [ ! -z "$2" ]; then
file="$2"
fi
cat "${file}" | sort | {
sum=0
last=""
while read line; do
arr=($line)
timestamp=$(date --date "${arr[0]} ${arr[1]}" +%s)
if [ "${arr[2]}" = "${last[2]}" ]; then
echo "Problem in ${file}:" > /dev/stderr
echo "> ${last[*]}" > /dev/stderr
echo "> ${arr[*]}" > /dev/stderr
exit 1
fi
if [ "${arr[2]}" = "IN" ]; then
let sum=${sum}-${timestamp} || true
else
let sum=${sum}+${timestamp} || true
fi
last=($line)
done;
if [ "${last[2]}" = "IN" ]; then
let sum=${sum}+$(date +%s) || true
fi
let hours=${sum}/60/60 || true
let minutes=${sum}/60%60 || true
let seconds=${sum}%60 || true
printf "%d:%02d:%02d\n" ${hours} ${minutes} ${seconds}
}
elif [ "$1" = "list" ]; then
ls *.txt
elif [ "$1" = "show" ] || [ -z "$1" ]; then
file="current.txt"
if [ ! -z "$2" ]; then
file="$2"
fi
(
echo "MOST RECENT FIRST"
echo "-----------------"
echo
tac ${file}
) | less
else
echo "Unknown argument: $1"
exit 1
fi