osi-tools/bin/osi-extract

82 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-08-10 00:22:41 +00:00
#!/usr/bin/env bash
2018-08-18 18:42:42 +00:00
# Copyright (C) 2018 Luke Shumaker
# Copyright (C) 2023-2023 Umorpha Systems
2018-08-18 18:42:42 +00:00
# SPDX-License-Identifier: AGPL-3.0-or-later
2023-10-23 22:22:04 +00:00
declare -r NAME=osi-extract
declare -r VERSION=20231023
2018-08-11 18:57:24 +00:00
2023-10-23 22:22:04 +00:00
set -euE
install_prefix="$(realpath --logical --canonicalize-missing -- "${BASH_SOURCE[0]}/../..")"
readonly install_prefix
source "${install_prefix}/lib/osi.sh"
2018-08-10 00:22:41 +00:00
2023-10-23 22:22:04 +00:00
main() {
local arg_mode=copy
local arg_image=
local arg_src=
local arg_dst=
2018-08-10 00:22:41 +00:00
2023-10-23 22:22:04 +00:00
local args
if ! args="$(getopt -n "${0##*/}" -o 'hV' -l 'help,version' -- "$@")"; then
arg_mode=error
else
eval "set -- $args"
while true; do
case "$1" in
-V|--version) shift; arg_mode=version;;
-h|--help) shift; arg_mode=usage;;
--) shift; break;;
*) error $EXIT_FAILURE 'Internal error. The programmer writing this tool screwed up.';;
2023-10-23 22:22:04 +00:00
esac
done
case "$arg_mode" in
copy)
if (( $# != 3 )); then
if (( $# == 0 )); then
error 0 "Expected 1 positional argument, got none"
else
error 0 "Expected 1 positional argument, got %d: %s" "$#" "${*@Q}"
fi
arg_mode=error
2024-01-05 02:13:25 +00:00
else
arg_image=$1
arg_src=$2
arg_dst=$(realpath -ms -- "$3")
2023-10-23 22:22:04 +00:00
fi
;;
esac
fi
case "$arg_mode" in
error)
print "Try '%q --help' for more information" "${0##*/}" >&2
return $EXIT_INVALIDARGUMENT
;;
2023-10-23 22:22:04 +00:00
version)
print "%s (osi-tools) %s" "$NAME" "$VERSION"
return $EXIT_SUCCESS
2023-10-23 22:22:04 +00:00
;;
usage)
print 'Usage: sudo %s [OPTIONS] FILENAME.img IMAGE_SRC_DIR HOST_DST_DIR' "${0##*/}"
print 'Operating System Image: Extract Files'
echo
print 'OPTIONS:'
print ' -h, --help display this help'
print ' -V, --version output version information'
return $EXIT_SUCCESS
2023-10-23 22:22:04 +00:00
;;
# main code starts here
copy)
local mountpoint
mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX")
trap "rmdir -- ${mountpoint@Q}" EXIT
2024-01-26 01:43:54 +00:00
_sudo -- "${install_prefix}/bin/osi-mount" --user -- "$arg_image" "$mountpoint" cp -aT -- "$mountpoint/$arg_src" "$arg_dst"
2023-10-23 22:22:04 +00:00
;;
esac
2018-08-10 00:22:41 +00:00
}
2023-10-23 22:22:04 +00:00
main "$@"