-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolybasectl
More file actions
executable file
·173 lines (150 loc) · 6.27 KB
/
polybasectl
File metadata and controls
executable file
·173 lines (150 loc) · 6.27 KB
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
#!/bin/sh
die() {
echo "$*" >&2
exit 1
}
POLYBASE_CONF="${POLYBASE_CONF=/etc/polybase.conf}"
[ -f "$POLYBASE_CONF" ] || die "Can't find configuration file $POLYBASE_CONF"
. "$POLYBASE_CONF"
[ -f "$POLYBASE_DB" ] || die "Database not found"
CURRENT_SEMESTER() {
if [ $(echo "select julianday('now') between julianday('$(date +%Y)-09-01') and julianday('$(( $(date +%Y) + 1))-01-01');" | sqlite3) -eq 1 ]; then
echo S1
else
echo S2
fi
}
COMMAND() { sqlite3 "$POLYBASE_DB" "$@"; }
COURSEADD_USAGE="
$(basename "$0") courseadd <CODE> [-n NAME] [-k KIND] [-P PART] [-p PARTS] [-q QUANTITY] [-t TOTAL] [-S SEMESTER] [-s] [-d]:
-d: dry run, show SQL commands that would be executed without actually doing them
-k: overrides default kind of $DEFAULT_KIND
-n: will be asked interactively if not specified
-P: overrides current part, default 1; if greater than PARTS, PARTS will be updated to PART across all courses satisfying CODE and KIND
-p: overrides amount of parts, default 1
-q: default quantity is 0, can be updated afterwards via Gemini
-S: override semester, default is $(CURRENT_SEMESTER), based on the current date
-s: show COURSE publicly immediately
-t: overrides total quantity, default is QUANTITY
"
COURSEMOD_USAGE="
$(basename "$0") coursemod <CODE> [-n NAME] [-k KIND] [-P PART|-p PARTS] [-q QUANTITY] [-t TOTAL] [-S SEMESTER] [-s] [-d]:
-d: dry run, show SQL commands that would be executed without actually doing them
-k: specify kind if query is ambiguous
-n: update name
-P: specify part if query is ambiguous, mutually exclusive with -p
-p: update amount of parts, mutually exclusive with -P
-q: update quantity where QUANTITY can be an absolute value or prefixed with +/- to increment/decrement by QUANTITY, can also be updated via Gemini
-S: update semester
-s: toggle whether COURSE is shown publicly
-t: update total quantity
"
USERADD_USAGE="
$(basename "$0") useradd <USERNAME> <FINGERPRINT>: add user USERNAME with fingerprint FINGERPRINT to Gemini write access
"
USERDEL_USAGE="
$(basename "$0") userdel <FINGERPRINT>: remove user with FINGERPRINT from users with Gemini write access
"
USERS_USAGE="
$(basename "$0") users: show all users with Gemini write access
"
database_write() {
COMMAND 'PRAGMA user_version=0' || die "No write access to database: $POLYBASE_DB, if needed override with the environment variable POLYBASE_DB"
}
add_user() {
database_write
COMMAND "INSERT INTO users VALUES ('$1', '$2');"
}
show_users() {
COMMAND -noheader -tabs "SELECT username, hash FROM users ORDER BY username;"
}
del_user() {
database_write
if [ -z "$1" ]; then
echo -n "Specify fingerprint: "
read -r 1
fi
# COMMAND "SELECT hash FROM users WHERE hash = '$1';" > /dev/null 2>&1 || die "Fingerprint not found" # XXX: invalid fingerprint detection
echo -n "User "
COMMAND "SELECT username FROM users WHERE hash = '$1';" | tr -d '\n'
echo -n " will be deleted. Continue? [y/N]"
read -r confirmation
case "$confirmation" in
y|Y) COMMAND "DELETE FROM users WHERE hash = '$1';" ;;
*) ;;
esac
}
add_course() {
database_write
if [ -z "$name" ]; then
echo -n "Specify course name: "
read -r name
fi
COMMAND "INSERT INTO courses VALUES ('$code', '${kind=$DEFAULT_KIND}', ${part=1}, ${parts=1}, '$name', ${quantity=0}, ${total=$quantity}, ${shown=0}, '${semester=$(CURRENT_SEMESTER)}');"
}
mod_course() {
database_write
query=$(COMMAND "SELECT code,kind,part,parts,name FROM courses WHERE code = '$code'${kind:+ AND kind = '$kind'}${part:+ AND part = $part};")
case "$quantity" in
'+'*|'-'*) quantity="quantity$quantity" ;;
*) ;;
esac
{ [ $(echo "$query" | wc -l) -gt 1 ] && [ -z "$total" ]; } && die "Ambiguous query, please refine with -k and/or -P:\n$query"
COMMAND "UPDATE courses SET code = '$code'${quantity:+, quantity = $quantity}${name:+, name = '$name'}${parts:+, parts = $parts}${total:+, total = $total}${shown:+, shown = NOT shown}${semester:+, semester = '$semester'} WHERE code = '$code'${kind:+ AND kind = '$kind'}${part:+ AND part = $part};"
}
case "$1" in
courseadd)
code="$2"
shift 2
while getopts "hdk:n:P:p:q:S:st:" opt; do
case "$opt" in
h) echo "Usage for courseadd:$COURSEADD_USAGE" && exit ;;
d) COMMAND() { echo "$@"; } ;;
k) kind="$OPTARG" ;;
n) name="$OPTARG" ;;
P) [ "$OPTARG" -gt 0 ] && part="$OPTARG" || die "Part must be a positive integer$COURSEADD_USAGE" ;;
p) [ "$OPTARG" -gt 0 ] && parts="$OPTARG" || die "Parts must be a positive integer$COURSEADD_USAGE" ;;
q) [ "$OPTARG" -gt 0 ] && quantity="$OPTARG" || die "Quantity must be a positive integer$COURSEADD_USAGE" ;;
S) semester="$OPTARG" ;;
s) shown=1 ;;
t) [ "$OPTARG" -gt 0 ] && total="$OPTARG" || die "Total must be a positive integer$COURSEADD_USAGE" ;;
*) die "Usage for courseadd:$COURSEADD_USAGE" ;;
esac
done
[ $OPTIND -ne 1 ] && add_course || die "Usage for courseadd:$COURSEADD_USAGE" ;;
coursemod)
code="$2"
shift 2
while getopts "hdk:n:P:p:q:S:st:" opt; do
case "$opt" in
h) echo "Usage for coursemod:$COURSEMOD_USAGE" && exit ;;
d) COMMAND() { echo "$@"; } ;;
k) kind="$OPTARG" ;;
n) name="$OPTARG" ;;
P) ( [ "$OPTARG" -gt 0 ] && [ -z "$parts" ] ) && part="$OPTARG" || die "Part must be a positive integer and -p must not be set$COURSEMOD_USAGE" ;;
p) ( [ "$OPTARG" -gt 0 ] && [ -z "$part" ] ) && parts="$OPTARG" || die "Parts must be a positive integer and -P must not be set$COURSEMOD_USAGE" ;;
q) case "$OPTARG" in
*[!0123456789+-]*|-|?*-*|0?*|-0|'') die "Quantity must be an integer or increment/decrement value$COURSEMOD_USAGE" ;;
*) quantity="$OPTARG" ;;
esac ;;
S) semester="$OPTARG" ;;
s) shown=1 ;;
t) [ "$OPTARG" -gt 0 ] && total="$OPTARG" || die "Total must be a positive integer$COURSEMOD_USAGE" ;;
*) die "Usage for coursemod:$COURSEMOD_USAGE" ;;
esac
done
[ $OPTIND -ne 1 ] && mod_course || die "Usage for coursemod:$COURSEMOD_USAGE" ;;
useradd)
shift
[ "$#" -eq 2 ] && add_user "$1" "$2" || die "Usage for useradd:$USERADD_USAGE" ;;
userdel)
shift
[ "$#" -eq 1 ] && del_user "$1" || die "Usage for userdel:$USERDEL_USAGE" ;;
users)
shift
show_users ;;
-h|help)
echo "Usage:$COURSEADD_USAGE$COURSEMOD_USAGE$USERADD_USAGE$USERDEL_USAGE$USERS_USAGE" ;;
*)
die "Usage:$COURSEADD_USAGE$COURSEMOD_USAGE$USERADD_USAGE$USERDEL_USAGE$USERS_USAGE" ;;
esac