osi-tools/osi-extract

78 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2018 Luke Shumaker
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
declare -r NAME=osi-extract
declare -r VERSION=20231023
set -euE
source "$(dirname -- "${BASH_SOURCE[0]}")/lib/osi.sh"
main() {
local arg_mode=copy
local arg_image=
local arg_src=
local arg_dst=
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.';;
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
fi
arg_image=$1
arg_src=$2
arg_dst=$(realpath -ms -- "$3")
;;
esac
fi
case "$arg_mode" in
error)
print "Try '%q --help' for more information" "${0##*/}" >&2
return $EXIT_INVALIDARGUMENT
;;
version)
print "%s (osi-tools) %s" "$NAME" "$VERSION"
return $EXIT_SUCCESS
;;
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
;;
# main code starts here
copy)
local mountpoint
mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX")
trap "rmdir -- ${mountpoint@Q}" EXIT
sudo -- "$(dirname -- "${BASH_SOURCE[0]}")/osi-mount" --user -- "$arg_image" "$mountpoint" cp -aT -- "$mountpoint/$arg_src" "$arg_dst"
;;
esac
}
main "$@"