#!/bin/bash

domain=$1

fullchain_file="/home/certbot/certs/$domain/fullchain.pem"
key="/home/certbot/certs/${domain}/${domain}.key"
combined_file="/etc/haproxy/ssl/${domain}.pem"
dhparam="/home/certbot/dhparam"

if [ ! -f $key ]; then
  key="/home/certbot/letsencrypt.key"
fi

if [ -f $fullchain_file -a -f $key -a -f $dhparam ]
then

  if [ -f $combined_file ]
  then
    cp $combined_file "${combined_file}.old"
  fi

  cat $fullchain_file $key $dhparam > $combined_file
  chmod 0400 $combined_file
fi