-
Notifications
You must be signed in to change notification settings - Fork 12
/
ghr-compress
executable file
·51 lines (46 loc) · 1.23 KB
/
ghr-compress
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
#!/usr/bin/env bash
set -e -u -o pipefail
shopt -s nullglob
if [ "$GHR_COMPRESS" = "gz" ]; then
tar_option=z
extension=".tar.gz"
elif [ "$GHR_COMPRESS" = "bz2" ]; then
tar_option=j
extension=".tar.bz2"
elif [ "$GHR_COMPRESS" = "xz" ]; then
tar_option=J
extension=".tar.xz"
elif [ "$GHR_COMPRESS" = "zip" ]; then
extension=".zip"
else
echo "ghr-compress: error: unknown extension: ${extension}" >&2
exit 1
fi
ghr-compress-one-tar() {
local file="$1"
tar c "-${tar_option}" -f "${file}${extension}" "$file"
}
ghr-compress-one-zip() {
local file="$1"
zip -r "${file}${extension}" "$file"
}
ghr-compress-one() {
local file="$1"
local file_extension=".${file#*.}" # get extension from the file
if [ "$file_extension" != "$extension" ]; then # only compress if the extensions dont match to avoid recompressing
if [ "$GHR_COMPRESS" = "zip" ]; then
ghr-compress-one-zip "$file" >&2
else
ghr-compress-one-tar "$file"
fi
rm -r -- "$file"
fi
}
if [ "$GHR_PATH" = "." ]; then
for file in *; do
ghr-compress-one "$file"
done
else
ghr-compress-one "$GHR_PATH"
echo "export GHR_PATH=$(printf '%q' "${GHR_PATH}${extension}")"
fi