forked from colby/dyno
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdyno.sh
executable file
·94 lines (82 loc) · 1.55 KB
/
dyno.sh
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
#!/bin/bash
# vi: syntax=sh ts=4 expandtab
set -e
set -x
# variables
DYNO_PATH="/Users/colbyolson/src/dyno"
DIR="$DYNO_PATH/dynos"
LIST="$DYNO_PATH/dyno.list"
INDEX="$DYNO_PATH/index.html"
# functions
errcho() {
>&2 echo "$*"
exit 1
}
_check() {
which "$1" || errcho "Missing dependency: $1"
}
_hash() {
$md5 <<< "$*" | cut -d' ' -f1
}
_store() {
hash=$1
shift
mkdir -p "$DIR"
echo "$*" > "$DIR/$hash"
}
_track() {
echo "$1 $2 $3" >> $LIST
}
header() {
cat > $INDEX <<EOL
<!DOCTYPE html>
<html>
<head>
<title>Dyno!</title>
</head>
<body>
EOL
}
footer() {
cat >> $INDEX <<EOL
</body>
</html>
EOL
}
# deps
md5=$(_check md5sum)
sed=$(_check sed)
date=$(_check gdate)
edit=$(which $EDITOR || errcho 'Missing variable: $EDITOR')
# arguments and logic
now=$($date +%s)
case $# in
0)
header
while read line; do
set -- $line
# fulldate=$($date --date="@$1")
post="$DIR/$3"
title=$(head -n1 "$DIR/$3")
echo "<a href='$post'>$title</a>" >> "$INDEX"
done < $LIST
footer
;;
1)
if [ ${#1} == 10 ]; then
while read line; do
set -- $line
[ $2 -lt $now ] && $sed -i "/^$1/d" "$LIST"
done < $LIST
fi
[ ${#1} == 32 ] && $edit "$DIR/$1"
;;
*)
# default is die in +5 years
die=$($date -d '+5 year' "+%s")
content="$*"
hash=$(_hash "$content")
_track "$now" "$die" "$hash"
_store "$hash" "$content"
;;
esac