-
Notifications
You must be signed in to change notification settings - Fork 0
/
lun.sh
executable file
·147 lines (124 loc) · 3.04 KB
/
lun.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
#!/bin/bash
SCRIPT=$( basename "$0" )
CONFIG_DIR="$HOME/.lunch"
CONFIG_FILE="$CONFIG_DIR/config"
UPDATE_URL="https://github.com/patricklucas/lunch.js/raw/master/lun.sh"
if [ "$1" = "-h" -a ! -n "$2" ]
then
cat << EOF
$SCRIPT
List nominations and votes
$SCRIPT -n <username> -s <server:port>
Register
$SCRIPT -a <restaurant>
Nominate a restaurant
$SCRIPT -r <restaurant-prefix>
Remove a nomination
$SCRIPT <restaurant-prefix>
Vote for restaurant
$SCRIPT -u
Unvote
$SCRIPT -d <seats>
Mark yourself as a driver
$SCRIPT -c <comment>
Set your user comment
$SCRIPT -C
Clear your user comment
EOF
exit 0
fi
if [ "$1" = "--update" -a ! -n "$2" ]
then
curl -s "$UPDATE_URL" > "$0"
echo "lun.sh updated successfully."
exit 0
fi
if [ ! -d $CONFIG_DIR ]
then
mkdir $CONFIG_DIR 2> /dev/null
if [ $? -ne 0 ]
then
echo "Could not create config dir $CONFIG_DIR!"
exit 1
fi
fi
CURL="curl -k -s -X POST"
# Setup complete, check if user has a token
if [ -f "$CONFIG_FILE" ]
then
HOST="https://$( head -n 1 $CONFIG_FILE )"
TOKEN=$( tail -n 1 $CONFIG_FILE )
if [ "x$HOST" = "x" -o "x$TOKEN" = "x" ]
then
echo "Config file $CONFIG_FILE malformed." 1>&2
exit 1
fi
else
if [ "$1" = "-n" -a -n "$2" -a "$3" = "-s" -a -n "$4" ]
then
HOST="https://$4"
RESP=$( $CURL $HOST/register.txt -d username="$2" )
if [ $? -eq 7 ]
then
echo "Couldn't connect to server at $HOST" 1>&2
exit 1
fi
if [ "$( echo $RESP | cut -c -6 )" = "Token:" ]
then
touch "$CONFIG_FILE"
chmod 0600 "$CONFIG_FILE"
echo "$4" >> "$CONFIG_FILE"
echo "$2" >> "$CONFIG_FILE"
echo "$( echo $RESP | cut -c 8- )" >> "$CONFIG_FILE"
echo "You are now registered!"
exit 0
else
echo $RESP
exit 1
fi
else
echo "You must register with '$SCRIPT -n <username> -s <server:port>'" 1>&2
exit 1
fi
fi
# Redefine $CURL to pass token
CURL="$CURL -d token=$TOKEN"
if [ $# = 0 ]
then
$CURL $HOST/nominations.txt
elif [ "$1" = "-n" -a -n "$2" ]
then
echo "You are already registered." 1>&2
exit 1
elif [ "$1" = "-a" -a -n "$2" ]
then
shift
$CURL $HOST/nominate.txt -d restaurant="$*"
elif [ "$1" = "-r" -a -n "$2" ]
then
shift
$CURL $HOST/unnominate.txt -d restaurant="$*"
elif [ "$1" = "-u" -a ! -n "$2" ]
then
$CURL $HOST/unvote.txt
elif [ "$1" = "-c" -a -n "$2" ]
then
shift
$CURL $HOST/comment.txt -d comment="$*"
elif [ "$1" = "-C" -a ! -n "$2" ]
then
$CURL $HOST/comment.txt -d comment=""
elif [ "$1" = "-d" -a -n "$2" ]
then
$CURL $HOST/drive.txt -d seats="$2"
elif [ "$1" = "--reset" -a ! -n "$2" ]
then
$CURL $HOST/reset.txt
else
$CURL $HOST/vote.txt -d restaurant="$*"
fi
if [ $? -ne 0 ]
then
echo "Couldn't connect to server at $HOST" 1>&2
exit 1
fi