Summary Files Commits Branches @commit @diff @stats
path: root / scripts / bin / rclone.sh
blob: c8e0e16730a104930b8fb429d1d420ce400f23f4 (plain) (blame)
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
#!/bin/sh

: ${PREFIX:=$HOME/mnt} ${TITLE:=rclone helper} ${ICON=rclone-browser}
FIELDS_BEFORE=1

KDBX_MOUNTED="$PREFIX/FTP marduk.int kamen"
: ${KDBX_REMOTE_DIR:="Secret"}
: ${KDBX_LOCAL_DIR:='/mnt/Svalbard/Doc/Adulthood/Vault'}

yad_ok() {
	echo "$2" | yad --title="$TITLE" --window-icon="$ICON" --borders=10 --text-info \
		--text="$1"  \
		--button="yad-ok:0" &
	disown
}

# Run window
if [ $# -eq 0 ]
then
	eval "yad --title='$TITLE' --window-icon='$ICON' --borders=10 --fixed --no-buttons --form \
		--field='<span size=\"x-large\" weight=\"bold\">rclone mount</span>:LBL' '' \
		$(rclone listremotes | awk -v ORS=' ' '
			{
				sub(/:$/,"");sub(/^[ \t\v]*/,"");sub(/[ \t\v]*$/,"")
				print "--field=\"" $0 ":SW\" \"" (system("mount | grep \"" $0 "\" >/dev/null")==0?"TRUE":"FALSE") "\""
			}') \
		--changed-action='$0' \
		--field='<span size=\"x-large\" weight=\"bold\">KDBX sync</span>:LBL' '' \
		--field='Download KDBX:FBTN' '$0 kdbx download' \
		--field='Upload KDBX:FBTN' '$0 kdbx upload' \
		"
# KDBX sync
elif [ "$1" = 'kdbx' ]
then
	if ! mount | grep "$KDBX_MOUNTED" >/dev/null
	then
		yad_ok "Directory not mounted!" "$KDBX_MOUNTED"
		exit 1
	fi

	if [ "$2" = 'download' ]
	then
		if [ ! -f "$KDBX_MOUNTED/$KDBX_REMOTE_DIR/hash_md5.txt" ]
		then
			yad_ok "Missing file!" "$KDBX_MOUNTED/$KDBX_REMOTE_DIR/hash_md5.txt"
			exit 2
		fi

		yad --title="$TITLE" --window-icon="$ICON" --borders=10 \
			--image=dialog-warning --text="Are you sure you want to <b>download</b> KDBX files?\nThis will overwrite <b>local</b> copies!"  \
			--button="yad-yes:0" --button="yad-no:1" || exit 0

		cd "$KDBX_MOUNTED/$KDBX_REMOTE_DIR"

		if ! md5sum -c ./hash_md5.txt >/dev/null 2>&1
		then
			yad_ok "Checksum check failed!" "$(md5sum -c --quiet ./hash_md5.txt)"
			exit 3
		fi

		while read -r kdbx
		do
			kdbx_hash="$(echo "$kdbx" | cut -c-32)"
			kdbx_path="$(echo "$kdbx" | cut -c35-)"

			cp -f "$kdbx_path" "$KDBX_LOCAL_DIR/$kdbx_path"
		done <"$KDBX_MOUNTED/$KDBX_REMOTE_DIR/hash_md5.txt"
	else
		yad --title="$TITLE" --window-icon="$ICON" --borders=10 \
			--image=dialog-question --text="Are you sure you want to <b>upload</b> KDBX files?\nThis will overwrite <b>remote</b> copies!"  \
			--button="yad-yes:0" --button="yad-no:1" || exit 0

		mkdir -p "$KDBX_MOUNTED/$KDBX_REMOTE_DIR"
		cd "$KDBX_MOUNTED/$KDBX_REMOTE_DIR"

		cp -f "$KDBX_LOCAL_DIR"/*.kdbx .
		md5sum *.kdbx >hash_md5.txt
	fi
# Mount/unmount
else
	remote_info="$(rclone listremotes --long | awk -v FIELD="$1" -v OFFSET="$FIELDS_BEFORE" 'BEGIN{row=FIELD-OFFSET}NR==row{gsub(/:/,"");print;exit}')"

	if [ -z "$remote_info" ]
	then
		yad_ok 'Invalid field data!' "$*"
		exit 1
	fi

	remote="$(echo "$remote_info" | awk '{$NF="";sub(/^[ \t\v]*/,"");sub(/[ \t\v]$/,"");print}')"
	remote_type="$(echo "$remote_info" | awk '{print toupper($NF)}')"

	dir="$PREFIX/$remote_type $remote"
	mkdir -p "$dir"

	if [ 'FALSE' = "$2" ]
	then
		umount "$dir" >/dev/tty
	elif mount | grep "$dir" >/dev/null
	then
		yad_ok 'Directory is already mounted!' "$dir"
	else
		rclone mount "$remote:/" "$dir" --daemon &>/dev/null
	fi
fi