/
procrastinator.sh
executable file
·125 lines (94 loc) · 2.03 KB
/
procrastinator.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# Block domain for a specified duration to prevend procrastination
# License WTFPL
blocklist="twitter.com youtube.com reddit.com facebook.com"
# this file overwrite the previous list, (remember that $HOME is the home of the root user)
blocklist_file="$HOME/.procrastinator_blocklist"
hostfile="/etc/hosts"
temphostfile="/tmp/procrastinator_hosts"
help () {
echo "$0
Usage:
start Start blocking domains
Xm Block domains for the next X minutes
Xh Block domains for the next X hours
list List the blocked domains
stop Stop the blocking
force-stop Force the stop (usefull if the computer have been rebooted since the start)
-h | help Print this help message
"
}
start () {
if [ -e "$temphostfile" ]
then
echo "Procrastinator seems to be already running"
exit 1
fi
root
echo "Starting procrastinator"
cp "$hostfile" "$temphostfile"
for website in $blocklist
do
echo 127.0.0.1 "$website" >> "$hostfile"
echo 127.0.0.1 www."$website" >> "$hostfile"
done
}
stop () {
if [ -e "$temphostfile" ]
then
root
echo "Stopping procrastinator"
mv "$temphostfile" "$hostfile"
else
echo "Procrastinator not running"
echo "Use \"$0 force-stop\" if needed"
fi
}
force-stop () {
root
echo "Force stopping procrastinator"
for website in $blocklist
do
sed -i /^127.0.0.1\ .*$website$/d "$hostfile"
done
rm "$temphostfile"
}
list () {
echo "Domain blocked:"
echo "$blocklist" | sed 's/\ /\n/g'
}
runduration () {
root
arg=$(echo $* | sed 's/h/\ hours/' | sed 's/m/\ minutes/')
echo mv "$temphostfile" "$hostfile" | at now + $arg
if [ "$?" != 0 ]
then
help
fi
}
root () {
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
}
if [ -e "$blocklist_file" ]
then
blocklist=$(cat "$blocklist_file")
fi
case "$1" in
start)
start;;
stop)
stop;;
force-stop)
force-stop;;
list)
list;;
*h | *m)
start
runduration $*
;;
*)
help;;
esac