#!/bin/bash

if [[ -z $1 ]]; then
	echo "Usage: $0 <certificate file> [PEM|DER]"
	exit 1
fi

if [[ -z $2 ]]; then
	format=PEM
else
	format=$2
fi

# Friendly Name
openssl x509 -inform $format -in $1 -text -noout | sed -n -e '/^[ ]\+Subject:/{s/^.*CN=\([^,]*\).*/\1/;p}'

# Underline Friendly Name with equal signs
openssl x509 -inform $format -in $1 -text -noout | sed -n -e '/^[ ]\+Subject:/{s/^.*CN=\([^,]*\).*/\1/;p}' | sed -e 's/./=/g'

# Output Fingerprint and swap = for :
openssl x509 -inform $format -in $1 -noout -fingerprint | sed -e 's/=/: /'

# Output PEM Data:
echo 'PEM Data:'

# Output Certificate
openssl x509 -inform $format -in $1

# Output Cettificate text swapping Certificate with Certificate Ingredients
openssl x509 -inform $format -in $1 -text -noout | sed -e 's/^Certificate:/Certificate Ingredients:/'
