summaryrefslogblamecommitdiffstats
path: root/server.sh
blob: 2824d07ebb87d328abcef88ef20e27588502d56f (plain) (tree)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620











































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                  
#!/bin/bash

# functions
function find_directory {
	find "$directory_home" -maxdepth 3 -mount -type d -name "$1" 2>/dev/null
}
function find_remote {
	rclone listremotes | awk -v remote="$1" '$0 ~ remote {print $0;exit}'
}
function check_root {
	if [ "$EUID" -ne 0 ]; then
		echo "Please run as root"
		exit 0
	fi
}
function check_not_root {
	if [ "$EUID" -eq 0 ]; then
		echo "Don't run this function as root"
		exit 0
	fi
}
function check_depends {
	dependencies=(aria2c awk bash bc docker docker-compose ffmpeg git gnuplot journalctl logname media-sort mp3val opustags phockup pip3 python3 qpdf rbw rclone scour sed seq sort uniq vnstat we-get yt-dlp)
	echo "Checking dependencies..."
	for i in "${dependencies[@]}"; do
		echo -n "$i: "
		if [[ $(command -v "$i") ]]; then
			echo -e "\e[32mpresent\e[39m"
		else
			echo -e "\e[31mmissing\e[39m"
		fi
	done
	exit 1
}
function umount_remote {
	if [ -z "$2" ]; then
		working_directory="$(find_directory "$1")"
	else
		working_directory="$(find_directory "$2")"
	fi
	umount "$working_directory"
	fusermount -uz "$working_directory" 2>/dev/null
	find "$working_directory" -maxdepth 1 -mount -type d -not -path "*/\.*" -empty -delete
}
function password_manager {
	check_not_root
	case "$1" in
	addr) rbw get --full "$2" | awk '/URI:/ {print $2}' ;;
	full) rbw get --full "$2" ;;
	pass) rbw get "$2" ;;
	sync) rbw sync ;;
	user) rbw get --full "$2" | awk '/Username:/ {print $2}' ;;
	*) rbw get "$2" ;;
	esac
}
function duolingo_streak {
	check_not_root
	# check api is installed
	[[ -d "$(find_directory $directory_config)/duolingo" ]] || git clone https://github.com/KartikTalwar/Duolingo.git "$(find_directory $directory_config)/duolingo"
	# cd to git dir to include module
	cd "$(find_directory $directory_config)/duolingo" || return
	# write script
	password_manager sync
	{
		printf "#!/usr/bin/env python3\\n\\n"
		printf "import duolingo\\n"
		printf "lingo  = duolingo.Duolingo('%s', password='%s')\\n" "$(password_manager user duolingo)" "$(password_manager pass duolingo)"
		printf "lingo.buy_streak_freeze()"
	} >"streak-freeze.py"
	# run and remove script
	python "streak-freeze.py"
	rm "streak-freeze.py"
}
function blog_duolingo_rank {
	duo_username="$(awk -F'[/()]' '/Duolingo/ {print $5}' "$(find_directory blog."$domain")"/content/about.md)"
	rank_filename="$(find_directory blog."$domain")/content/posts/logging-duolingo-ranks-over-time.md"
	echo -n "Fetching data for $duo_username... "
	page_source="$(curl -s https://duome.eu/"$duo_username")"
	rank_lingot="$(printf %s "$page_source" | awk -F"[#><]" '/icon lingot/ {print $15}')"
	rank_streak="$(printf %s "$page_source" | awk -F"[#><]" '/icon streak/{getline;print $15}')'"
	echo -e "$i \e[32mdone\e[39m"
	echo -n "Appending ranks to page... "
	echo "| $(date +%F) | $(date +%H:%M) | $rank_streak | $rank_lingot |" | tr -d \' >>"$rank_filename"
	echo -e "$i \e[32mdone\e[39m"
	lastmod "$rank_filename"
}
function docker_build {
	cd "$directory_script" || exit
	# write env file, overwriting any existing
	password_manager sync
	{
		printf "DOMAIN=%s\\n" "$domain"
		printf "PUID=%s\\n" "$(id -u)"
		printf "PGID=%s\\n" "$(id -g)"
		printf "TZ=%s\\n" "$(timedatectl status | awk '/Time zone/ {print $3}')"
		printf "DOCKDIR=%s\\n" "$(find_directory docker)"
		printf "SYNCDIR=%s\\n" "$(find_directory vault)"
		printf "TANKDIR=%s\\n" "$directory_tank"
		printf "DBPASSWORD=%s\\n" "$(password_manager pass postgresql)"
		printf "VPNUSER=%s\\n" "$(password_manager user transmission-openvpn)"
		printf "VPNPASS=%s\\n" "$(password_manager pass transmission-openvpn)"
		printf "HTPASSWD=%s\\n" "$(docker exec -it caddy caddy hash-password --plaintext "$(password_manager pass htpasswd)" | base64 -w0)"
		printf "TODOSECRET=%s\\n" "$(password_manager full vikunja | awk '/secret:/ {print $2}')"
	} >"$directory_script/.env"
	# make network, if not existing
	if ! printf "%s" "$(docker network ls)" | grep -q "proxy"; then
		echo Creating docker network
		docker network create proxy
	fi
	# start containers
	echo Starting docker containers
	docker-compose up -d --remove-orphans
	# delete temporary env file
	if [[ -f "$directory_script/.env" ]]; then
		echo Deleting detected env file
		rm "$directory_script/.env"
	fi
	# clean up existing stuff
	echo Cleaning up existing docker files
	for i in volume image system network; do
		docker "$i" prune -f
	done
	docker system prune -af
}
function media_logger {
	# specify directories
	git_directory="$(find_directory logger)"
	file_git_log="$git_directory/media.log"
	log_remote="$(find_remote nas)"
	git_logger="git --git-dir=$git_directory/.git --work-tree=$git_directory"
	# git configuruation
	if [ ! -e "$git_directory" ]; then
		printf "Logger directory not found, quitting...\n"
		exit 1
	fi
	if [ ! -e "$git_directory/.git" ]; then
		printf "Initialising blank git repo...\n"
		$git_logger init
	fi
	if [ -e "$file_git_log.xz" ]; then
		printf "Decompressing existing xz archive...\n"
		xz -d "$file_git_log.xz"
	fi
	if [ -e "$file_git_log" ]; then
		printf "Removing existing log file...\n"
		rm "$file_git_log"
	fi
	printf "Creating log...\n"
	rclone ls "$log_remote" | sort -k2 >"$file_git_log"
	printf "Appending size information...\n"
	rclone size "$log_remote" >>"$file_git_log"
	printf "Commiting log file to repository...\n"
	$git_logger add "$file_git_log"
	$git_logger commit -m "Update: $(date +%F)"
	if [ -e "$file_git_log.xz" ]; then
		printf "Removing xz archive...\n"
		rm "$file_git_log.xz"
	fi
	printf "Compressing log file...\n"
	xz "$file_git_log"
	printf "Compressing repository...\n"
	$git_logger config pack.windowMemory 10m
	$git_logger config pack.packSizeLimit 20m
	$git_logger gc --aggressive --prune
	printf "Log complete!\n"
}
function parse_magnets {
	# sources and destinations
	cd "