-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtrx
executable file
·93 lines (83 loc) · 2.54 KB
/
dtrx
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
#!/bin/bash
# shellcheck source=lib/archive.sh
source "$(dirname "$0")/lib/archive.sh"
# glob hidden files
shopt -s dotglob
cleanup () {
[ -d "$EXTDIR" ] && rm -r "$EXTDIR"
exit "$1"
}
FILE=$(readlink -f "$1")
FILENAME=$(basename "$1")
DIR=$(dirname "$FILE")
EXTDIR=$(mktemp -d -p "$PWD" .dtrx-XXX)
DEST=$PWD
cd "$EXTDIR"
case $FILE in
*.tar.*) DESTDIR="${FILENAME%.tar.*}";;
*) DESTDIR="${FILENAME%.*}";;
esac
archive_extract "$FILE"
DIR=(*) # Glob all in extraced dir
if [ "${#DIR[@]}" -eq 1 ]; then
echo "single something"
NODENAME="${DIR[0]}"
# Single directory
if [ -d "$NODENAME" ]; then
# echo "single dir"
if [ "$NODENAME" == "$DESTDIR" ]; then
mv -i "$NODENAME" "$DEST"/
else
echo "$1 contains one directory but its name doesn't match."
echo " Expected: $DESTDIR"
echo " Actual: $NODENAME"
echo "You can:"
echo " * extract the directory _I_nside a new directory named $DESTDIR"
echo " * extract the directory and _R_ename it $DESTDIR"
echo " * extract the directory _H_ere"
echo -n "What do you want to do? (i/r/H) "
read -r answer
case $answer in
i|I)
if [ -d "$DEST"/"$DESTDIR" ]; then
echo "$DESTDIR already exists. Aborting!"
cleanup 1
else
mv -iT "$NODENAME" "$DEST"/"$DESTDIR"/"$NODENAME"
fi
;;
r|R)
if [ -d "$DEST"/"$DESTDIR" ]; then
echo "$DESTDIR already exists. Aborting!"
cleanup 1
else
mv -iT "$NODENAME" "$DEST"/"$DESTDIR"
fi
;;
h|H|*)
if [ -d "$DEST"/"$NODENAME" ]; then
echo "$NODENAME already exists. Aborting!"
cleanup 1
else
mv -iT "$NODENAME" "$DEST"/"$NODENAME"
fi
;;
esac
fi
# Single file?
else
# echo "single file"
mv -i "$NODENAME" "$DEST"/
fi
# Multiple files
else
# echo "multiple files"
if [ -d "$DEST"/"$DESTDIR" ]; then
echo "$DESTDIR already exists. Aborting!"
cleanup 1
else
mv -iT "$EXTDIR" "$DEST"/"$DESTDIR"
fi
fi
# Guess nothing failed
cleanup 0