mirror of
https://github.com/samuelncui/yatm.git
synced 2026-11-08 12:26:33 +00:00
40 lines
1.0 KiB
Bash
Executable File
40 lines
1.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
mt -f "${DEVICE}" load
|
|
deadline=$(($(date +%s) + 60))
|
|
while true; do
|
|
now=$(date +%s)
|
|
if ((now >= deadline)); then
|
|
break
|
|
fi
|
|
remaining=$((deadline - now))
|
|
if info=$(timeout "${remaining}s" ./yatm-lto-info -f "${DEVICE}"); then
|
|
# A present, empty field is readable MAM without an assigned barcode.
|
|
# The prefix distinguishes it from a missing field or failed probe.
|
|
barcode=$(awk '$1 == "Barcode" && $2 == ":" {
|
|
value = $3
|
|
if (length(value) == 8) {
|
|
value = substr(value, 1, 6)
|
|
}
|
|
print ":" value
|
|
exit
|
|
}' <<< "${info}")
|
|
if [[ -n ${barcode} ]]; then
|
|
printf '{"barcode":"%s"}\n' "${barcode#:}" > "${OUT}"
|
|
exit 0
|
|
fi
|
|
fi
|
|
now=$(date +%s)
|
|
if ((now >= deadline)); then
|
|
break
|
|
fi
|
|
delay=$((deadline - now))
|
|
if ((delay > 2)); then
|
|
delay=2
|
|
fi
|
|
sleep "${delay}"
|
|
done
|
|
echo "Tape barcode is unavailable: ${DEVICE}" >&2
|
|
exit 1
|