#!/bin/sh
# Helper script which auto-gens a new SSL key for each start
# of the sysadm server

ssl_keygen()
{
  #Determine where the files should be placed (based on user)
  if [ `id -u` != "0" ] ; then
   DIR="${XDG_CONFIG_HOME}/sysadm"
  else
    DIR="/usr/local/etc/sysadm"
  fi
  #Now ensure the dir exists
  if [ ! -d "${DIR}" ] ; then
    mkdir -p ${DIR}
  fi
  #Now create the key/crt files
  openssl req -x509 -nodes -newkey rsa:2048 \
    -keyout ${DIR}/bridgeserver.key \
    -out ${DIR}/bridgeserver.crt -days 1024 \
    -subj "/C=US/ST=MY/L=NULL/O=SysAdm/OU=SysAdm/CN=SysAdm/emailAddress=none@example.org" 2>/dev/null
}

export PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
ssl_keygen "$1"
command="/usr/local/bin/sysadm-bridge"
flags=""

if [ -n "$1" ] ; then
  flags="$1"
fi

exec $command $flags
