#!/bin/bash

#####################################################################################################################
 #  Name:       Vehicle Birth Cert Provisioning Tool
 #  Version:    1.0
 #  Description: This script is a provisioning tool that gives vehicles their birth cert. In a typical use-case
 #               There is a provisioner (or operator) machine that connects to a remote vehicle to provide it with
 #               the birth cert. In this use-case, the vehicle should be accessible over ssh (e.g. by having 'zaccess'
 #               run before running this script). There's an option to run the script locally on the vehicle to
 #               'generate' the birth cert. However, locally generated birth certs will have to be 'registered'
 #               separately by an authorized operator.

 # 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 "cleanup; exit" INT TERM EXIT

# Provisioning steps here
check_provisioner_software_prereqs
check_vehicle_software_prereqs
get_token
gen_vehicle_csr
get_vehicle_cert
store_cert_on_vehicle
register_birth_cert
test_vehicle_cert




