-
Notifications
You must be signed in to change notification settings - Fork 4
/
make_zip.sh
executable file
·78 lines (67 loc) · 1.93 KB
/
make_zip.sh
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
#!/bin/bash
s=${1:?"missing arg 1 for configuration file"}
FROM=${2:?"missing arg 2 for source directory"}
TO=$(mktemp -d)
ZIP=${3:?"missing arg 3 for outputed zip filename"}
if [ -d "$TO" ]; then
rm -r "$TO"
fi
if [ -f "$ZIP" ]; then
rm "$ZIP"
fi
occurances=""
sed_cmd=""
# Iterate over each line in s
while IFS= read -r line; do
# Skip lines that contain no "|", contain "#", or start with "@"
if [[ $line != *"|"* || $line == *"#"* || $line == "@"* ]]; then
# Skip lines that not start with "@"
if [[ $line == "@"* ]]; then
# Split the line into "a" and "b" variables exluding first "@"
IFS='|' read -ra parts <<< "${line#*@}"
# Remove leading and trailing spaces
a=$(echo "${parts[0]}" | xargs)
b=$(echo "${parts[1]}" | xargs)
occurances+="$a\n"
sed_cmd+="s|$a|$b|g;"
fi
continue
fi
# Split the line into "from" and "to" variables
IFS='|' read -ra parts <<< "$line"
# Remove leading and trailing spaces
from=$(echo "${parts[0]}" | xargs)
to=$(echo "${parts[1]}" | xargs)
# Extract basenames from "from" and "to"
from_basename=$(basename "$from")
to_basename=$(basename "$to")
# If the basename of "to" is "*", replace it with the basename from "from"
if [[ $to_basename == "*" ]]; then
to=${to%\*}${from_basename}
fi
# prepend "to" with "TO"
to="$TO/$to"
# Ensure the directory exists or create it
mkdir -p -v "$(dirname "$to")"
# Check if "from" is a URL
if [[ $from =~ ^http[s]?:// ]]; then
# If "from" is a link, download it using wget and name as "to"
curl -s "$from" -o "$to"
else
# prepend "from" with "FROM"
from="$FROM/$from"
# If "from" is not a link, copy it to "to"
cp "$from" "$to"
fi
done < "$s"
cd "$TO"
# Remove the trailing semicolon
sed_cmd=${sed_cmd%?}
# Replace in files every occurance of "a" with "b"
grep -RlFf <(printf $occurances) | xargs -t sed -i -e "$sed_cmd"
# Archive everything
zip -r "$ZIP" *
cd -
# Delete temporary folder
mv "$TO/$ZIP" "$ZIP"
rm -rf "$TO"