#!/bin/sh -e

if [ "$1" = "configure" ]; then
  if ! getent group zorp >/dev/null 2>&1; then
    addgroup --quiet --system zorp
  fi

  if ! id zorp >/dev/null 2>&1; then
    adduser --quiet --system --no-create-home --ingroup zorp --home /var/lib/zorp zorp
  fi

  chown -R root:zorp /etc/zorp
  chmod 0750 /etc/zorp

  if [ ! -d /var/lib/zorp/keybridge-cache ]; then
    mkdir -p /var/lib/zorp/keybridge-cache
  fi

  chown -R zorp.zorp /var/lib/zorp/keybridge-cache
  chmod 0770 /var/lib/zorp/keybridge-cache

  ZORP_DHFILE="/etc/zorp/dh.pem"
  if [ ! -f $ZORP_DHFILE ]; then
    echo "Generating dh.pem for ephemeral Diiffie-Hellman connections"
    openssl dhparam -out $ZORP_DHFILE 2048
  fi
fi

#DEBHELPER#
