-
Notifications
You must be signed in to change notification settings - Fork 0
/
negoto-ctl.sh
executable file
·139 lines (108 loc) · 2.33 KB
/
negoto-ctl.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
#!/bin/sh
set -uf
HANDLES_TABLE=uw_File_handle_Ids
CSS_FILES_TABLE=uw_File_Css_files
CSS_HANDLES_TABLE=uw_File_Css_handles
THEMES_TABLE=uw_Layout_themes
KV_TABLE=uw_KeyVal_store
BOARDS_TABLE=uw_Data_boards
ADMINS_TABLE=uw_Account_admins
DB_FILE=
die() {
echo "$1" 1>&2
exit 1
}
usage() {
die "Usage: $0 -f <negoto.db> <command> <args>
Commands:
add-theme <name> <file> <color> <css-dir>
add-board <id> <name>
add-admin <name> owner|admin|moderator"
}
next_css_handle() {
sqlite3 "$DB_FILE" <<EOF
PRAGMA foreign_keys=TRUE;
INSERT INTO $HANDLES_TABLE VALUES (NULL);
SELECT id FROM $HANDLES_TABLE;
DELETE FROM $HANDLES_TABLE;
EOF
}
file_md5() {
md5sum "$1" | cut -d ' ' -f 1
}
add_theme() {
set -e
name=$1
file=$2
color=$3
css_dir=$4
hash=$(file_md5 "$file")
handle=$(next_css_handle)
sqlite3 "$DB_FILE" <<EOF
PRAGMA foreign_keys=TRUE;
INSERT INTO $CSS_FILES_TABLE VALUES ('$hash', 'text/css');
INSERT INTO $CSS_HANDLES_TABLE VALUES ('$hash', $handle);
INSERT INTO $THEMES_TABLE VALUES ('$name', '/static/css/$hash.css', $handle, '$color');
INSERT INTO $KV_TABLE VALUES ('defaultTheme', $handle) ON CONFLICT DO NOTHING;
EOF
cp "$file" "$css_dir/$hash.css"
echo "Created theme '$name' with hash $hash"
}
add_board() {
set -e
id=$1
name=$2
sqlite3 "$DB_FILE" <<EOF
PRAGMA foreign_keys=TRUE;
INSERT INTO $BOARDS_TABLE VALUES ('$id', '$name');
EOF
echo "Created board /$id/ - $name"
}
add_admin() {
set -e
name=$1
role=$2
password_hash='$2b$10$pZl/2fqRmnP.LCmE8K22/.MO29m64l/jRmUjpsYEF0IlO.kRSKHsm'
role_id=
case $role in
owner) role_id=2 ;;
admin) role_id=1 ;;
moderator) role_id=0 ;;
*) die "Invalid role: $role" ;;
esac
sqlite3 "$DB_FILE" <<EOF
PRAGMA foreign_keys=TRUE;
INSERT INTO $ADMINS_TABLE VALUES ('$name', $role_id, '$password_hash');
EOF
echo "Created admin '$name' with role '$role' and default password 'password'"
}
getopts 'f:' f
case $f in
f) DB_FILE=$OPTARG ;;
esac
if [ -z "$DB_FILE" ]; then
usage
fi
shift; shift
if [ -z "$*" ]; then
usage
fi
subcmd=$1
shift
case $subcmd in
add-theme)
if [ $# -ne 4 ]; then usage; fi
add_theme "$1" "$2" "$3" "$4"
;;
add-board)
if [ $# -ne 2 ]; then usage; fi
add_board "$1" "$2"
;;
add-admin)
if [ $# -ne 2 ]; then usage; fi
add_admin "$1" "$2"
;;
*)
usage
;;
esac