forked from viccherubini/get-shit-done
-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-shit-done.sh
executable file
·188 lines (147 loc) · 3.72 KB
/
get-shit-done.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
E_NO_PARAMS=1
E_USER_NOT_ROOT=2
E_NO_HOSTS_FILE=3
E_ALREADY_SET=4
E_WEIRD_PARAMS=5
function exit_with_error()
{
# output to stderr
echo $2 >&2
print_help
exit $1
}
function trim()
{
echo $1
}
function to_lower()
{
echo $1 | tr '[A-Z]' '[a-z]'
}
function print_help()
{
cat <<EOF
Usage: `basename $0` [work | play]
EOF
}
# just appends sites lines to the
# end of first param file
function work()
{
# if no hosts file found...
[ -e "$1" ] || exit_with_error $E_NO_HOSTS_FILE "No hosts file found"
ini_file="$HOME/.get-shit-done.ini"
site_list=( 'reddit.com' 'forums.somethingawful.com' 'somethingawful.com'
'digg.com' 'break.com' 'news.ycombinator.com'
'infoq.com' 'bebo.com' 'twitter.com'
'facebook.com' 'blip.com' 'youtube.com'
'vimeo.com' 'delicious.com' 'flickr.com'
'friendster.com' 'hi5.com' 'linkedin.com'
'livejournal.com' 'meetup.com' 'myspace.com'
'plurk.com' 'stickam.com' 'stumbleupon.com'
'yelp.com' 'slashdot.org' )
# add sites from ini file
# to site_list array
sites_from_ini $ini_file
file="$1"
# check if work mode has been set
if grep $start_token $file &> /dev/null; then
if grep $end_token $file &> /dev/null; then
exit_with_error $E_ALREADY_SET "Work mode already set."
fi
fi
echo $start_token >> $file
for site in "${site_list[@]}"
do
echo -e "127.0.0.1\t$site" >> $file
echo -e "127.0.0.1\twww.$site" >> $file
done
echo $end_token >> $file
$restart_network
}
function play()
{
# removes $start_token-$end_token section
# in any place of hosts file (not only in the end)
sed_script="{
s/$end_token/$end_token/
t finished_sites
s/$start_token/$start_token/
x
t started_sites
s/$start_token/$start_token/
x
t started_sites
p
b end
: started_sites
d
: finished_sites
x
d
: end
d
}"
# if no hosts file found...
[ -e "$1" ] || exit_with_error $E_NO_HOSTS_FILE "No hosts file found"
file=$1
sed --in-place -e "$sed_script" $file
$restart_network
}
function sites_from_ini()
{
[ -e "$1" ] || return 1
# read all lines from ini file
while read line
do
# split the equals sign
arr=( ${line/=/" "} )
key=${arr[0]}
value=${arr[1]}
# just save sites variable
if [ "sites" == $key ]; then
# remove trailing commas
clean_arr=$(echo "$value" | sed "s/,*$//")
# and leading
clean_arr=$(echo "$clean_arr" | sed "s/^,*//")
sites_arr=$(echo $clean_arr | tr ',' "\n")
# get array size
count=${#site_list[*]}
# add all sites to global sites array
for site in $sites_arr
do
site_list[$count]=$site
((count++))
done
fi
done < "$1"
}
# check for input parameters
[[ "$#" -eq 0 ]] && exit_with_error $E_NO_PARAMS "No parameters given"
curr_user=$(trim $(whoami))
curr_user=$(to_lower $curr_user)
# run fron root user
# to change hosts file
[ $curr_user == "root" ] || exit_with_error $E_USER_NOT_ROOT "Please, run from root"
uname=$(trim `uname`)
if [ "Linux" == $uname ]; then
restart_network="/etc/init.d/networking restart"
elif [ "Darwin" == $uname ]; then
restart_network="dscacheutil -flushcache"
else
message="Please, contribute DNS cache flush command on GitHub"
restart_network="echo $message"
fi
##############################
hosts_file="/etc/hosts"
start_token="## start-gsd"
end_token="## end=gsd"
action=$1
case "$action" in
"play")
play $hosts_file; ;;
"work")
work $hosts_file; ;;
*) exit_with_error $E_WEIRD_PARAMS "Some weird params given" ;;
esac