#!/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 device.crt.txt exists, look for SHA1 and copy corresponding openssl.cnf
    printf "${me} openssl.cnf in userdata? ... "
    if [ ! -e /opt/userdata/etc/ssl/openssl.cnf ]; then
      printf "no. Need to copy. Has crt.txt file? ... "
      if [ -e "${EXTPARAM}/device.crt.txt" ]; then
        printf "yes. Is SHA256? ... "
        mkdir -p /opt/userdata/etc/ssl/
        if grep -q "sha1WithRSAEncryption" "${EXTPARAM}/device.crt.txt"; then
          printf "no.\\n"
          cp -v /opt/gira/etc/ssl/openssl_sha1.cnf /opt/userdata/etc/ssl/openssl.cnf
        else
          printf "yes.\\n"
          cp -v /opt/gira/etc/ssl/openssl.cnf /opt/userdata/etc/ssl/openssl.cnf
        fi
      else
        printf "no. Failed to check SHA256. ${EXTPARAM}/device.crt.txt is not present.\\n"
      fi
    else
      printf "yes.\\n"
    fi
    sync
    ;;
  stop)
    ;;
  *)
    printf "Usage: %s (start|stop)\\n" "$0"
    exit 1
esac

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

exit 0
