#!/bin/bash

set -eoux pipefail

if [ "$#" != "1" ]; then
  echo "Write a name"
  exit 1
fi

PEMPATH=$(realpath "$(dirname $0)/$1.pem")
P12PATH=$(realpath "$(dirname $0)/$1.p12")

TMPDIR="${HOME}/tmp"

cd "${TMPDIR}"
openssl genpkey -algorithm ED25519 -out server.key
openssl req -x509 -new -key server.key -nodes -out server.crt -days 5000 -subj "/C=US/ST=State/L=City/O=Organization/CN=example.com"
cat server.crt server.key > "${PEMPATH}"
echo "Created ${PEMPATH}"
openssl x509 -in "${PEMPATH}" -noout -fingerprint -sha256
openssl pkcs12 -export -out "${P12PATH}" -inkey "${PEMPATH}" -in "${PEMPATH}"
