-
Notifications
You must be signed in to change notification settings - Fork 0
/
focus.sh
executable file
·72 lines (60 loc) · 1.48 KB
/
focus.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
#!/bin/bash
usage() {
echo "Usage: sudo ./focus.sh --duration=30m or sudo ./focus.sh --duration=3h"
exit 1
}
if [ "$#" -ne 1 ]; then
usage
fi
case "$1" in
--duration=*)
DURATION="${1#*=}"
;;
*)
usage
;;
esac
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
if [ ! -f "block-list.txt" ]; then
echo "block-list.txt not found"
# create an empty block-list.txt file and fill it with some default entries
touch block-list.txt
{
echo "facebook.com"
echo "instagram.com"
echo "linkedin.com"
echo "reddit.com"
echo "twitter.com"
} >> block-list.txt
echo "block-list.txt created with default entries"
fi
if [ ! -f "/etc/hosts" ]; then
echo "/etc/hosts not found"
exit
fi
sed -i '' '/# Block-list start/,/# Block-list end/d' /etc/hosts
echo "# Block-list start" >> /etc/hosts
while IFS= read -r line; do
echo "127.0.0.1 $line" >> /etc/hosts
echo "127.0.0.1 www.$line" >> /etc/hosts
done < block-list.txt
echo "# Block-list end" >> /etc/hosts
echo "Focus session started for $DURATION."
case "$DURATION" in
*m)
SECONDS=$((${DURATION%m} * 60))
;;
*h)
SECONDS=$((${DURATION%h} * 3600))
;;
*)
usage
;;
esac
trap "sed -i '' '/# Block-list start/,/# Block-list end/d' /etc/hosts; echo '\nFocus session ended. /etc/hosts restored to original state.'; exit" SIGINT SIGTERM
sleep $SECONDS
sed -i '' '/# Block-list start/,/# Block-list end/d' /etc/hosts
echo "Focus session ended. /etc/hosts restored to original state."