#!/bin/sh

me="[$(printf $0 | xargs basename)]"

printf "${me} Enter.\\n"

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars

case "$1" in
  start)
   # If ssl_cert exists, extract the device.crt and device.key from it that are required by nginx config.
  if [ -e "${EXTPARAM}/ssl_cert" ]; then
    printf "${me} Does device certificate file exist? ... "
    if [ ! -e "${EXTPARAM}/device.crt" ]; then
      printf "no. We need to create it now.\\n"
      openssl pkcs12 -in "${EXTPARAM}/ssl_cert" -out "${EXTPARAM}/device.crt" -clcerts -nokeys -passin 'pass:'
    else
      printf "yes.\\n"
    fi
    printf "${me} Does device key file exist? ... "
    if [ ! -e "${EXTPARAM}/device.key" ]; then
      printf "no. We need to create it now.\\n"
      openssl pkcs12 -in "${EXTPARAM}/ssl_cert" -out "${EXTPARAM}/device.key" -nocerts -nodes -passin 'pass:'
    else
      printf "yes.\\n"
    fi
  else
    printf "${me} The device pkcs12 in ${EXTPARAM}/ssl_cert does not exist. Therefore we cannot create the crt and key file for nginx.\\n"
  fi
  sync
  ;;
  stop)
    ;;
  *)
    printf "Usage: %s (start|stop)\\n" "$0"
    exit 1
esac

printf "${me} Exit.\\n"

exit 0
