-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount-tars.sh
More file actions
219 lines (187 loc) · 6.46 KB
/
mount-tars.sh
File metadata and controls
219 lines (187 loc) · 6.46 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
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
#!/bin/bash
# https://github.com/placebeyondtheclouds/my-shell-scripts/blob/main/mount-tars.sh
controlc() {
exit 1
}
trap controlc SIGINT
echo "Test if ratarmount is installed..."
if ! ratarmount -v; then
echo -e "\n\n\nratarmount not found. activate conda environment with ratarmount installed first or install it with \nconda install -c conda-forge ratarmount \nor\npip install ratarmount\nquitting."
exit 1
fi
SOURCE_ARCHIVES=$1
MOUNT_DESTINATION=$2
SKIPKEYWORD=".ipynb_checkpoints"
declare -a archivefiles
declare -a alldirs
cat <<"EOF"
__ ______ ______ ____
/'\_/`\ /\ \__ /\__ _\/\ _ \ /\ _`\
/\ \ ___ __ __ ___ \ \ ,_\ ___ __ __ _ __ \/_/\ \/\ \ \L\ \\ \ \L\ \ ____
\ \ \__\ \ / __`\ /\ \/\ \ /' _ `\\ \ \/ / __`\ /\ \/\ \ /\`'__\ \ \ \ \ \ __ \\ \ , / /',__\
\ \ \_/\ \ /\ \L\ \\ \ \_\ \/\ \/\ \\ \ \_ /\ \L\ \\ \ \_\ \\ \ \/ \ \ \ \ \ \/\ \\ \ \\ \ /\__, `\
\ \_\\ \_\\ \____/ \ \____/\ \_\ \_\\ \__\ \ \____/ \ \____/ \ \_\ \ \_\ \ \_\ \_\\ \_\ \_\\/\____/
\/_/ \/_/ \/___/ \/___/ \/_/\/_/ \/__/ \/___/ \/___/ \/_/ \/_/ \/_/\/_/ \/_/\/ / \/___/
EOF
#checks
if [ -z "$SOURCE_ARCHIVES" ] || [ -z "$MOUNT_DESTINATION" ]; then
echo "Usage: $0 <SOURCE_ARCHIVES> <MOUNT_DESTINATION>"
echo "Universal example: $0 /mnt/sams2T_crypt_vg_data/datasets ./source_datasets [--mount | --unmount]"
echo "Quitting."
exit 1
fi
if [ ! -d "$MOUNT_DESTINATION" ]; then
mkdir -p "$MOUNT_DESTINATION"
fi
if [ ! -d "$MOUNT_DESTINATION" ]; then
echo "Failed to create $MOUNT_DESTINATION"
exit 1
fi
if [ ! -d "$SOURCE_ARCHIVES" ]; then
echo "No such directory: $SOURCE_ARCHIVES"
exit 1
fi
# find mount, unmount in following arguments and set the respective flags
for arg in "$@"; do
case $arg in
--mount)
MOUNT="true"
;;
--unmount)
UNMOUNT="true"
;;
esac
done
list_archives() {
#this is a dictionary hahaha, associative array
declare -A archivefiles_map
IFS=$'\n'
# mapfile -d '' -t files < <(find "$SOURCE_ARCHIVES" -type f \( -name "*.tar" -o -name "*.zip" -o -name "*.rar" -o -name "*.tar.gz" \) 2>/dev/null | sort -n)
files=()
while IFS= read -r -d '' file; do
files+=("$file")
done < <(find "$SOURCE_ARCHIVES" -type f \( -name "*.tar" -o -name "*.zip" -o -name "*.rar" -o -name "*.tar.gz" \) -print0 2>/dev/null | sort -zn)
for line in "${files[@]}"; do
case "$line" in
*.tar.gz)
base_name="${line%.tar.gz}"
;;
*.tar)
base_name="${line%.tar}"
;;
*)
base_name="${line%.*}"
;;
esac
if [[ "$line" == *.tar ]]; then
archivefiles_map["$base_name"]="$line"
else
if [[ -z "${archivefiles_map["$base_name"]}" ]]; then
archivefiles_map["$base_name"]="$line"
fi
fi
done
archivefiles=("${archivefiles_map[@]}")
cat <<"EOF"
_ ___ ___ _ _ ___ __ __ ___ ___ __ _ _
/_\ | _ \ / __|| || ||_ _|\ \ / /| __|/ __| / _| ___ _ _ _ _ __| |(_)
/ _ \ | /| (__ | __ | | | \ V / | _| \__ \ | _|/ _ \| || || ' \ / _` | _
/_/ \_\|_|_\ \___||_||_||___| \_/ |___||___/ |_| \___/ \_,_||_||_|\__,_|(_)
EOF
echo "archive count: ${#archivefiles[@]}"
for file in "${archivefiles[@]}"; do
echo "$file"
done
}
list_mounts() {
alldirs=()
IFS=$'\n'
for line in $(find "$MOUNT_DESTINATION" -maxdepth 1 -type d 2>/dev/null | grep -v "$SKIPKEYWORD" | grep -v "^\.$" | grep -v "^\.\.$" | sort -n); do
alldirs+=("$line")
done
# remove first element
alldirs=("${alldirs[@]:1}")
# remove empty dir and the item from array
for onedir in "${alldirs[@]}"; do
if [[ -d "$onedir" ]]; then
if [[ -z "$(ls -A "$onedir")" ]]; then
rm -rf "$onedir"
alldirs=("${alldirs[@]/$onedir/}")
fi
fi
done
cat <<"EOF"
_ __ _ _
_ __ ___ _ _ _ _ | |_ ___ / _| ___ _ _ _ _ __| |(_)
| ' \ / _ \| || || ' \| _|(_-< | _|/ _ \| || || ' \ / _` | _
|_|_|_|\___/ \_,_||_||_|\__|/__/ |_| \___/ \_,_||_||_|\__,_|(_)
EOF
echo "mount points to unmount (or empty dirs deleted): ${#alldirs[@]}"
for dir in "${alldirs[@]}"; do
echo "$dir"
done
}
mount() {
for ((i = 0; i < ${#archivefiles[@]}; i++)); do
basename="${archivefiles[$i]##*/}"
destination="$MOUNT_DESTINATION/${basename}"
if [ -e "$destination" ]; then
echo "Skipping ${basename}, its already mounted in $destination"
read -n 1 -s -r -p "Press any key to continue"
else
ratarmount "${archivefiles[$i]}" "$destination"
fi
done
}
unmount() {
for onedir in "${alldirs[@]}"; do
echo "Unmounting $onedir..."
ratarmount -u "$onedir"
if [[ -d "$onedir" ]]; then
if [[ -z "$(ls -A "$onedir")" ]]; then
rm -rf "$onedir"
fi
fi
done
}
list_archives
list_mounts
if [ "$MOUNT" = "true" ]; then
mount
list_archives
list_mounts
exit 0
fi
if [ "$UNMOUNT" = "true" ]; then
unmount
list_archives
list_mounts
exit 0
fi
cat <<"EOF"
Oo Oo
o O o O
O o O o
ooooooooo ooooooooo
ooooooooo ooooooooo
ooooooooo
EOF
echo "press [m] to mount, [u] to unmount, [q] to quit"
read -n 1 -s -r -p "" key
case $key in
m)
# mount
mount
list_archives
list_mounts
;;
u)
# unmount
unmount
list_archives
list_mounts
;;
q)
exit 0
;;
esac