-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoaurum
executable file
·351 lines (331 loc) · 11.6 KB
/
goaurum
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
# a 300+ line bodge.
_start() {
if [ -z `grep "NAME=\"$1\"" $HOME/.aurumfile` ] ; then echo "$1 not found." ; exit 1 ; fi
pid=`ps -eo "pid,ucomm" | grep "[0-9]\s.$1" | perl -pe 's/^[\s]+//g' | perl -pe 's/[\s].+//g'`
if [ ! -z $pid ] ; then echo "$1 appears to be already running at $pid." ; exit 0 ; fi
path=`grep "NAME=\"$1\"" $HOME/.aurumfile | perl -pe "s/^NAME=\"$1\":PATH=\"|\"$//g"`
cd $path
rm './dat/logs/nohup.out' >> /dev/null 2>&1
nohup ./.$1 $path >./dat/logs/nohup.out 2>&1 &
if [ $? -ne 0 ] ; then echo "Failed to start up $1" ; else echo "Started up $1." ; fi
cd -
}
_stop() {
if [ -z `grep "NAME=\"$1\"" $HOME/.aurumfile` ] ; then echo "$1 not found." ; exit 0 ; fi
pid=`ps -eo "pid,ucomm" | grep "[0-9]\s\.$1" | perl -pe 's/^[\s]+//g' | perl -pe 's/[\s].+//g'`
if [ -z $pid ] ; then echo "$1 is not running." ; exit 0 ; fi
kill -2 $pid
if [ "$?" -ne "0" ] ; then echo "An error code $? was encountered when attempting to kill the bot."; exit 1 ; fi
exit 0
}
_index() {
n=`grep "NAME=\"$1\"" $HOME/.aurumfile`
if [ ! -z $n ] ; then
echo "it appears that $1 already exists within the aurumfile manager."
echo "would you like to overwrite it?"
read -e -p "[y/N] " answer
case "$answer" in
[yY]*) perl -pi.original -e "s#NAME=\"$1\":PATH=\".*\"#NAME=\"$1\":PATH=\"$2\"#g" $HOME/.aurumfile ; rm $HOME/.aurumfile.original ;;
*) echo "Exiting Program" ; exit 0 ;;
esac
echo "Overwrote $1. PATH=$2."
else
echo "NAME=\"$1\":PATH=\"$2\"" >> $HOME/.aurumfile
fi
}
_remove() {
read -e -p "Preparing to remove $1. Are you sure?\n[y/N] " answer
case $1 in
[yY]*) perl -pi.bck -e "s/^NAME=\"$1\":PATH=\".*\"$//g" $HOME/.aurumfile ; rm $HOME/.aurumfile.bck;;
*) echo "Exiting Program" ; exit 0 ;;
esac
echo "Successfully removed $1 from the database."
}
_update() {
if [ "$EUID" -ne 0 ] ; then
echo "Goaurum's updater needs to run as root. Please use sudo."
exit 1
fi
mkdir $HOME/.autmp
cd $HOME/.autmp
if [ ! -z "$2" ] ; then
git clone --branch $2 https://github.com/aurumbot/core.git
else
git clone https://github.com/aurumbot/core.git
fi
if [ "$1" = "--goaurum" ] ; then
touch auupdate ; chmod +x auupdate
cat > auupdate << EOF
#!/bin/bash
sleep 5
rm /usr/local/bin/goaurum
mv ./core/goaurum /usr/local/bin/
chmod +x /usr/local/bin/goaurum
/usr/local/bin/goaurum goaurumupdatefinalize
EOF
echo "Preparing to update goaurum. Note that this may take a bit"
echo "It also appears to stop the updater, this is normal. Please wait for"
echo "a short while (30 seconds?) for it to update, then try goaurum again."
./auupdate & >> /dev/null 2>&1
exit 0
fi
path=`grep "NAME=\"$1\"" $HOME/.aurumfile | perl -pe "s/^NAME=\"$1\":PATH=\"|\"$//g"`
if [ -z "$path" ] ; then
echo "$1 not found."
exit 1
fi
_stop $1 > /dev/null 2>&1
rm $path/.$1
env CGO_ENABLED=1 GO111MODULE=on go build -o $path/.$1 ./main.go
rm -rf $HOME/.autmp
if [ "$?" -ne "0" ] ; then exit 1 ; else echo "Successfully updated $1." ; fi
}
_goaurumupdatefinalize() {
rm -rf $HOME/.autmp > /dev/null 2>&1
echo "Updates installed! (I hope)"
}
_installplugin() {
cd $2
path=`grep "NAME=\"$1\"" $HOME/.aurumfile | perl -pe "s/^NAME=\"$1\":PATH=\"|\"$//g"`
./build
if [ $? -ne 0 ] ; then exit 1 ; fi
pluginname=`ls ./bin | perl -pe "s/\.so$//g"`
mv ./bin/* $path/plugins/
if [ -d ./dat ] ; then
mkdir $path/dat/cfg/$pluginname
mv ./dat/* $path/dat/cfg/$pluginname/
fi
echo "Compiled and installed plugin to $1."
cd - >> /dev/null
}
_list() {
name=`grep "^NAME=\".*\"" $HOME/.aurumfile | perl -pe "s/^NAME=\"|\":PATH=\".*\"$//g" ; printf "\n"`
echo $name
exit 0
}
_init() {
echo "You will be guided through the process of installing aurum's dependancies."
echo "Install?"
read -e -p "[Y/n] " answer
if [[ "$answer" = "no" ]] | [[ "$answer" = "n" ]] ; then
echo "Quitting program"
exit 0
fi
if [[ "$OS" = "Darwin" ]] ; then
while [[ `which gcc` = "" ]] ; do
echo "installing necessary command line tools, please follow the on-screen"
echo "dialogue boxes to install xcode tools."
xcode-select --install
done
if [[ `which brew` = "" ]] ; then
echo "installing homebrew."
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
if [[ `which greadlink` = "" ]] ; then
echo "installing coreutils"
brew install coreutils
fi
if [[ `which go` = "" ]] ; then
echo "installing golang"
brew install go
fi
if [[ `which perl` = "" ]] ; then
echo "installing perl"
brew install perl
fi
else
echo "installing core dependencies"
if [[ `which apt` != "" ]] ; then
sudo apt update
if [[ `which git` = "" ]] ; then
sudo apt install git
fi
if [[ `which git` = "perl" ]] ; then
sudo apt install perl
fi
if [[ `which wget` = "" ]] & [[ `which curl` = "" ]] ; then
sudo apt install wget
fi
elif [[ `which pacman` != "" ]] ; then
if [[ `which git` = "" ]] ; then
pacman -S git
fi
if [[ `which perl` = "perl" ]] ; then
pacman -S perl
fi
if [[ `which wget` = "" ]] & [[ `which curl` = "" ]] ; then
pacman -S wget
fi
elif [[ `which pkg` != "" ]] ; then
if [[ `which git` = "" ]] ; then
pkg install git
fi
if [[ -z `which perl` ]] ; then
pkg install perl
fi
if [[ `which wget` = "" ]] & [[ `which curl` = "" ]] ; then
pkg install wget
fi
elif [[ `which yum` != "" ]] ; then
if [[ `which git` = "" ]] ; then
yum install git
fi
if [[ `which perl` = "" ]] ; then
yum install perl
fi
if [[ `which wget` = "" ]] & [[ `which curl` = "" ]] ; then
yum install wget
fi
else
echo "Your package manager could not be found. Please make sure you have:"
echo "● git"
echo "● perl"
echo "● wget or curl"
if [[ `which wget` = "" ]] | [[ `which curl` = "" ]] & [[ `which git` = "" ]] & [[ `which perl` = "" ]] ; then
exit 1
fi
fi
if [[ `which go` = "" ]] ; then
echo "downloading go..."
echo "what architecture are you using?"
printf "● \033[1;31mamd64\033[0m (most likely)\n"
printf "● \033[1;31m386\033[0m (32 bit intel systems)\n"
printf "● \033[1;31marmv6l\033[0m (arm systems such as rasperry pis)\n"
read -e -p "> " answer
if [ ! -z `which wget` ] ; then
wget https://dl.google.com/go/go1.11.linux-$answer.tar.gz
elif [ ! -z `which curl` ] ; then
curl https://dl.google.com/go/go1.11.linux-$answer.tar.gz -o go1.11.linux-$answer.tar.gz
else
echo "You don't appear to have wget or curl. how does that work?"
exit 1
fi
echo "installing go"
tar -C /usr/local -xzf go1.11.linux-$answer.tar.gz
rm go1.11.linux-$answer.tar.gz
echo "installs complete. use goaurum init to configure a new aurum bot"
fi
fi
}
#---------------------------------
_install() {
echo "You will be guided through the steps to create a new aurum instance."
echo "What would you like to name the bot?"
read -e -p "> " au_name
au_name=`echo $au_name | perl -pe 's/ /_/g' | perl -pe 's#/$##g'`
echo "Name set ($au_name)"
echo "Where would you like to setup the bot? (use ./ for current directory)"
read -e -p "> " au_path
if [ "$OS" = "Darwin" ] ; then
au_path=`greadlink -f $au_path | perl -pe 's!/$!!g'`
else
au_path=`readlink -f $au_path | perl -pe 's!/$!!g'`
fi
if [ -d $au_path/$au_name ] ; then
echo "It appears the path $au_path/$au_name already exists. Would you like to overwrite it?"
read -e -p "[y/N]: " answer
case "$answer" in
[yY]*) rm -rf $au_path/$au_name ; mkdir -p $au_path/$au_name ; cd $au_path/$au_name ;;
*) echo "Exiting Program" ; exit 0 ;;
esac
else
mkdir -p $au_path/$au_name
cd $au_path/$au_name
fi
_index $au_name $au_path/$au_name
echo "Setting up directory heirarchy in ${au_path}/${au_name}."
mkdir plugins dat dat/logs dat/cfg dat/cfg/aurum
touch ./dat/cfg/aurum/preferences.json
echo "Heirarchy created."
echo "Go to https://discordapp.com/developers/applications/ and create a bot application."
read -e -p "Enter the Client ID : " au_cid
read -e -p "Enter the Client Secret : " au_sec
read -e -p "Enter the Bot Token : " au_tok
echo "Authentication info set."
read -e -p "Set a command prefix (like \"!\") : " au_prf
echo "Prefrences set."
echo "Fetching most recent version of aurum"
mkdir ./.tmp
if [ ! -z `which git` ] ; then
git clone https://github.com/aurumbot/core.git ./.tmp/
if [ "$?" -ne "0" ] ; then echo "Error $? encountered when cloning latest version. Panicing." ; exit 1 ; fi
else
echo "You don't appear to have git. how does that work?" ; exit 1
fi
mv ./.tmp/main.go ./
mv ./.tmp/README.md ./
mv ./.tmp/go.mod ./
mv ./.tmp/go.sum ./
mv ./.tmp/handler ./handler
mv ./.tmp/LICENSE ./
echo "aurum/core downloaded. fetching dependencies and compiling."
export PATH=$PATH:/usr/local/go/bin
env CGO_ENABLED=1 GO111MODULE=on go build ./...
env CGO_ENABLED=1 GO111MODULE=on go build -v -o .$au_name main.go
echo "$au_name has been compiled. Finishing up."
# I'm sorry.
cat > ./dat/cfg/aurum/preferences.json << EOF
{
"clientID": "$au_cid",
"secret": "$au_sec",
"token": "$au_tok",
"prefix": "$au_prf",
"channels": [
""
],
"roles": [
""
],
"admins": [
""
]
}
EOF
# ok its over.
rm -rf ./handler
rm go.mod
rm go.sum
rm main.go
rm -rf ./.tmp
echo "installing $au_name"
printf "\033[1;31m$au_name successfully installed! Use the link below to put it on your server.\n"
printf "\033[0;36mhttps://discordapp.com/api/oauth2/authorize?client_id=${au_cid}&permissions=8&scope=bot\n"
printf "\033[0mTo start the bot, use \033[0;36mgoaurum start $au_name\033[0m\n"
cd - >> /dev/null
}
_help() {
printf "\033[1;31mUsage: \033[1;33mgoaurum <option>
\033[0mThe config tool and managment tool for aurum discord bots.
\n\033[1;31mOptions:
\033[1;32minit \033[0mfetch system dependancies (golang, perl)
\033[1;32minstall \033[0mcreate an instance of aurum
\033[1;32mstart <name> \033[0mstart the aurum bot called <name>
\033[1;32mstop <name> \033[0mstop the aurum bot called <name>
\033[1;32minstallplugin <name> <path> \033[0minstall a plugin at <path> to the <name> bot.
\033[1;32mlist \033[0mlist all aurum instances
\033[1;32mremove <name> \033[0mremove the bot <name> from the database
\033[1;32mindex <name> <path> \033[0madd a pre-existing instance of aurum to
the manager. Also used to fix broken paths.
\033[1;32mupdate <bot> [version] \033[0mupdate the <bot> to version [version].
use --goaurum to update goaurum, leave [version]
blank for the most recent version.\n"
}
#---------------------------------
touch $HOME/.aurumfile
if [ 0 -ne `ls -l /usr/local/bin/goaurum > /dev/null 2>&1 ; echo $?` ] ; then
echo "goaurum needs to be in /usr/local/bin. Move it there and try again."
exit 1
fi
case "$1" in
"init") _init ;;
"install") _install ;;
"start") _start $2 ;;
"stop") _stop $2 ;;
"list") _list ;;
"index") _index $2 $3 ;;
"update") _update $2 $3 ;;
"installplugin") _installplugin $2 $3 ;;
"goaurumupdatefinalize") _goaurumupdatefinalize ;;
*) _help ;;
esac