-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·266 lines (233 loc) · 5.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·266 lines (233 loc) · 5.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/sh
set -eu
REPO="vchilikov/takeout-fix"
API_URL="https://api.github.com/repos/${REPO}/releases/latest"
CWD=$(pwd)
OS=""
ARCH=""
TAG=""
ASSET_NAME=""
TMP_DIR=""
ASSET_PATH=""
CHECKSUM_PATH=""
RUN_BIN=""
log() {
printf '%s\n' "$*"
}
fail() {
printf 'Error: %s\n' "$*" >&2
exit 1
}
cleanup() {
if [ -n "$RUN_BIN" ] && [ -f "$RUN_BIN" ]; then
rm -f "$RUN_BIN"
fi
if [ -n "$TMP_DIR" ] && [ -d "$TMP_DIR" ]; then
rm -rf "$TMP_DIR"
fi
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
require_cmd() {
if ! command_exists "$1"; then
fail "required command not found: $1"
fi
}
install_exiftool_macos() {
if ! command_exists brew; then
fail "exiftool is missing and Homebrew is not installed. Install exiftool manually and rerun."
fi
brew install exiftool
}
install_exiftool_linux() {
if command_exists apt-get; then
if [ "$(id -u)" -eq 0 ]; then
apt-get install -y libimage-exiftool-perl
elif command_exists sudo; then
sudo apt-get install -y libimage-exiftool-perl
else
fail "sudo is required to install exiftool via apt-get."
fi
return
fi
if command_exists dnf; then
if [ "$(id -u)" -eq 0 ]; then
dnf install -y perl-Image-ExifTool
elif command_exists sudo; then
sudo dnf install -y perl-Image-ExifTool
else
fail "sudo is required to install exiftool via dnf."
fi
return
fi
if command_exists pacman; then
if [ "$(id -u)" -eq 0 ]; then
pacman -S --noconfirm perl-image-exiftool
elif command_exists sudo; then
sudo pacman -S --noconfirm perl-image-exiftool
else
fail "sudo is required to install exiftool via pacman."
fi
return
fi
fail "no supported package manager found (apt-get, dnf, pacman). Install exiftool manually and rerun."
}
ensure_exiftool() {
if command_exists exiftool; then
return
fi
log "ExifTool is required. Installing it now..."
case "$OS" in
darwin)
install_exiftool_macos
;;
linux)
install_exiftool_linux
;;
*)
fail "unsupported OS for automatic exiftool installation: $OS"
;;
esac
if ! command_exists exiftool; then
fail "exiftool installation did not make it available in PATH."
fi
}
detect_platform() {
uname_s=$(uname -s)
case "$uname_s" in
Darwin)
OS="darwin"
;;
Linux)
OS="linux"
;;
*)
fail "unsupported OS: $uname_s"
;;
esac
uname_m=$(uname -m)
case "$uname_m" in
x86_64 | amd64)
ARCH="amd64"
;;
arm64 | aarch64)
ARCH="arm64"
;;
*)
fail "unsupported architecture: $uname_m"
;;
esac
}
resolve_tag() {
release_json=$(curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "User-Agent: takeoutfix-installer" \
"$API_URL")
TAG=$(printf '%s\n' "$release_json" | sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)
if [ -z "$TAG" ]; then
fail "failed to resolve latest release tag from GitHub API."
fi
}
download_assets() {
ASSET_NAME="takeoutfix_${TAG}_${OS}_${ARCH}.tar.gz"
checksums_name="checksums.txt"
base_url="https://github.com/${REPO}/releases/download/${TAG}"
require_cmd mktemp
TMP_DIR=$(mktemp -d "${CWD}/.takeoutfix-installer.XXXXXX")
ASSET_PATH="${TMP_DIR}/${ASSET_NAME}"
CHECKSUM_PATH="${TMP_DIR}/${checksums_name}"
log "Downloading TakeoutFix (${ASSET_NAME})..."
curl -fsSL -o "$ASSET_PATH" "${base_url}/${ASSET_NAME}" || fail "failed to download ${ASSET_NAME}"
curl -fsSL -o "$CHECKSUM_PATH" "${base_url}/${checksums_name}" || fail "failed to download ${checksums_name}"
}
sha256_file() {
file="$1"
if command_exists sha256sum; then
sha256sum "$file" | awk '{print $1}'
return
fi
if command_exists shasum; then
shasum -a 256 "$file" | awk '{print $1}'
return
fi
fail "sha256 tool not found. Install sha256sum or shasum."
}
verify_checksum() {
expected=$(awk -v asset="$ASSET_NAME" '
{
hash = $1
name = $2
if (name == "") {
next
}
if (substr(name, 1, 1) == "*") {
name = substr(name, 2)
}
if (name == asset) {
print tolower(hash)
exit
}
}
' "$CHECKSUM_PATH")
if [ -z "$expected" ]; then
fail "checksum entry for ${ASSET_NAME} not found."
fi
actual=$(sha256_file "$ASSET_PATH" | tr 'A-F' 'a-f')
if [ "$actual" != "$expected" ]; then
fail "checksum mismatch for ${ASSET_NAME}."
fi
}
extract_binary() {
if [ ! -f "$ASSET_PATH" ]; then
fail "archive not found: $ASSET_PATH"
fi
case "$ASSET_NAME" in
*.tar.gz)
require_cmd tar
tar -xzf "$ASSET_PATH" -C "$TMP_DIR"
;;
*.zip)
require_cmd unzip
unzip -q "$ASSET_PATH" -d "$TMP_DIR"
;;
*)
fail "unsupported archive extension: $ASSET_NAME"
;;
esac
src_bin="${TMP_DIR}/takeoutfix"
if [ ! -f "$src_bin" ]; then
matches=$(find "$TMP_DIR" -type f -name takeoutfix -print || true)
if [ -z "$matches" ]; then
fail "takeoutfix binary was not found in downloaded archive."
fi
match_count=$(printf '%s\n' "$matches" | awk 'END { print NR }')
if [ "$match_count" -gt 1 ]; then
fail "multiple takeoutfix binaries found in archive; refusing ambiguous selection."
fi
src_bin="$matches"
fi
if [ -z "$src_bin" ] || [ ! -f "$src_bin" ]; then
fail "takeoutfix binary was not found in downloaded archive."
fi
RUN_BIN="${CWD}/.takeoutfix-run-$$-$(date +%s)"
cp "$src_bin" "$RUN_BIN"
chmod +x "$RUN_BIN"
}
run_takeoutfix() {
log "Starting TakeoutFix in: ${CWD}"
set +e
"$RUN_BIN" --workdir "$CWD"
status=$?
set -e
return "$status"
}
trap cleanup EXIT INT TERM
require_cmd curl
detect_platform
ensure_exiftool
resolve_tag
download_assets
verify_checksum
extract_binary
run_takeoutfix