#!/bin/bash

#####################################################################################################################
 #  Name:       Provisioner Tool
 #  Version:    1.0
 #  Description: This script is a provisioning tool that gives certain machines (e.g. imaging machines, operator
 # 				 laptops) their provisioning cert. In a typical use-case, an admin first logs into Vault to get a
 # 				 token. (S)he then uses the token with this tool to get a 'provisioning certificate'. This certificate
 # 				 will allow the owner to perform certain operator tasks, including providing vehicles with 'vehicle
 # 				 birth certificates' (using a separate tool).

 # For further information about this script and all its options, run it with "--help" option.

 #  Notes for Developers
 #
 #  * Do not rely on path or any other environmental variable when modifying this script.
 #  * For stronger security, the user who runs this script should not be able to modify it
 #    (or they should be strongly trusted).

#####################################################################################################################

set -euo pipefail

# sourcing related variables and functions
source ./settings.sh    # variables and default settings
source ./lib.sh         # library of provisioning functions
source ./usage.sh       # basic argument processing and usage

# Traping signals to ensure intermediate artifacts are cleaned up.
# This will not be a full clean up of state, as the script may have left the tpm (and/or the store_dir in case of having no tpm) in a 'bad' state. Such states require reprovisioning anyways and there's no point to revert back to the last known good state.
trap "stty echo; cleanup; exit" INT TERM EXIT

# Provisioning steps here
check_provisioner_software_prereqs
gen_provisioning_csr
get_token
get_provisioning_cert
store_provisioning_cert_on_provisioner
register_provisioning_cert
test_provisioning_cert




