#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later

#########################################
### Deployment System Profile Manager ###
#########################################

# Safety first
set -o errexit
set -o noglob
set -o nounset
set -o errtrace

# Full path to this script
#
# shellcheck disable=SC2155
readonly scriptname="$(realpath -- "$0")"

# Short name of this program
readonly progname="${scriptname##*/}"

# Location of sources: requires on-site launch capability
if [ "$scriptname" = "/usr/bin/$progname" ]; then
	readonly libdir=/usr/libexec/system-restore
else
	readonly libdir="${scriptname%/*}"/sysrest
fi

# Internal variables
#
action=
baremetal=
catalog=
classid=
comment=
cpuarch=
datetime=
newname=
parent=
pci_bus=1
instance=
profile=
search=

# Backup directory on local file system
# (default is current working directory)
#
backup="$(realpath .)"

# Remote backup storage account information
backup_proto="file"

# Default comment in shared profiles
shared_comment="This is a shared profile"

# shellcheck disable=SC1091
[ ! -s /etc/system-restore/profile.conf ] ||
	. /etc/system-restore/profile.conf

# Bootstrap
. "$libdir"/profile/hardware.sh
. "$libdir"/profile/common.sh
. "$libdir"/profile/parser.sh
. "$libdir"/profile/output.sh
. "$libdir"/profile/create.sh
. "$libdir"/profile/update.sh
. "$libdir"/profile/remove.sh
. "$libdir"/profile/search.sh

# Catch all unexpected errors
trap 'unexpected_error "${BASH_SOURCE[0]##*/}" "$LINENO"' ERR

# Entry point
parse_cmdline "$@"
detect_profile
umask 0022
"$action"

