#!/bin/sh

# Generates a self-signed certificate.
# Edit dovecot-openssl.conf before running this.

OPENSSL=${OPENSSL-openssl}
SSLDIR=/etc/dovecot
OPENSSLCONFIG=${OPENSSLCONFIG-dovecot-openssl.conf}

CERTFILE=$SSLDIR/dovecot-cert.pem
KEYFILE=$SSLDIR/dovecot-key.pem

if [ ! -d $SSLDIR ]; then
  echo "$SSLDIR directory doesn't exist"
fi

if [ ! -d $SSLDIR ]; then
  echo "$SSLDIR directory doesn't exist"
fi

if [ -f $CERTFILE ]; then
  echo "$CERTFILE already exists, won't overwrite"
  exit 1
fi

if [ -f $KEYFILE ]; then
  echo "$KEYFILE already exists, won't overwrite"
  exit 1
fi

$OPENSSL req -new -x509 -nodes -config $OPENSSLCONFIG -out $CERTFILE -keyout $KEYFILE -days 365 || exit 2
chmod 0600 $KEYFILE
echo 
$OPENSSL x509 -subject -fingerprint -noout -in $CERTFILE || exit 2

