#!/bin/bash -x

echo "Enter PFX Password"
read PASSWORD

PFX=$1

if [[ "$PFX" == "" ]]
then
    echo "No PFX file provided."
fi

rm -fr cert_$PFX
mkdir cert_$PFX
echo "PASS IS $PASSWORD"

# add -legacy if needed
openssl pkcs12 -legacy -passin pass:"$PASSWORD" -passout pass:"$PASSWORD" -in $PFX -nocerts -out cert_$PFX/privatekey-$PFX.pem && \
openssl pkcs12 -legacy -passin pass:"$PASSWORD" -passout pass:"$PASSWORD" -in $PFX -clcerts -nokeys -out cert_$PFX/cert-file-$PFX.pem && \
openssl pkcs12 -legacy -passin pass:"$PASSWORD" -passout pass:"$PASSWORD" -in $PFX -cacerts -nokeys -chain -out cert_$PFX/ca-chain-$PFX.pem && \
openssl rsa -passin pass:"$PASSWORD"  -in cert_$PFX/privatekey-$PFX.pem -out cert_$PFX/withoutpw-privatekey-$PFX.pem && \
cat cert_$PFX/cert-file-$PFX.pem  cert_$PFX/ca-chain-$PFX.pem >  cert_$PFX/nginx-$PFX.crt && \

echo "CERT INFORMATION : " && \
openssl x509 -in cert_$PFX/cert-file-$PFX.pem -text

