#!/bin/sh

###
# @author areazero <dev@areazero.de>
# @version 2017.08.01.01
# @copyright 2014 - 2017 areazero
###

TYPE=$1
FILE=$2

if [ -z "${FILE}" ] ; then
  TYPE='*'
  MESSAGE="no filename given"
fi

case "${TYPE}" in
  cert|cert-pem)
    OPTIONS="x509 -text -noout -in ${FILE}"
  ;;
  cert-der)
    OPTIONS="x509 -text -noout -inform der -in ${FILE}"
  ;;
  cert-pkcs12)
    OPTIONS="pkcs12 -in ${FILE} -clcerts -nokeys"
    PIPE="x509 -text -noout"
  ;;
  csr)
    OPTIONS="req -text -noout -in ${FILE}"
  ;;
  fingerprint)
    OPTIONS="x509 -fingerprint -noout -in ${FILE}"
  ;;
  hash)
    OPTIONS="x509 -noout -hash -in ${FILE}"
  ;;
  key)
    OPTIONS="rsa -text -noout -in ${FILE}"
  ;;
  *)
    echo "usage: $0 cert|cert-[der|pem|pkcs12]|csr|fingerprint|hash|key FILENAME" >&2

    [ -z "${MESSAGE}" ] || echo "${MESSAGE}" >&2

    exit 1
  ;;
esac

if [ -z "${PIPE}" ] ; then
  openssl ${OPTIONS}
else
  openssl ${OPTIONS} | openssl ${PIPE}
fi

exit $?
