#!/usr/bin/env bash


STATE=$(head -1 vars/state)
COUNTRY=$(head -1 vars/country)
LOCALITY=$(head -1 vars/locality)
ORGANIZATION=$(head -1 vars/organization)
LOCALITRY=$(head -1 vars/locality)
CN=$(head -1 vars/cn)

SUBJECT_ALTERNATIVES=$( python3 gen-subject-alternatives.py )
#sed -e "s/###CN##/commonName = $COMMON_NAME/g" \
#	-e "s/###COUNTRY###/countryName = $COUNTRY/g" \
#	-e "s/###STATE###/stateOrProvinceName = $STATE/g" \
#	-e  "s/###LOCALITY###/localityName = $LOCALITY/g" \
#	-e "s/###ORGANIZATION###/organizationName = $ORGANIZATION/g" \
#	templates/csrconfig.txt.in

jinja2 templates/csrconfig.txt.j2 -D cn="${CN}" -D country="${COUNTRY}" -D state="${STATE}" -D locality="${LOCALITY}" \
	-D org="${ORGANIZATION}" -D subj_alternative="${SUBJECT_ALTERNATIVES}" > tmp/csrconfig.txt


#generate the RSA private key
openssl genpkey -outform PEM -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out output/priv.key

#Create the CSR
openssl req -new -nodes -key output/priv.key -config tmp/csrconfig.txt -out output/cert.csr
