#!/bin/sh

if [ -z $1 ];
then
    exit 1;
fi

acme-client $1

case $? in
    0)  
        if [ "$2" == "xmpp" ];
        then
            echo on passe la;
            install -o _prosody -g _prosody /etc/ssl/private/"$1".key /etc/prosody/certs/"$1".key;
            install -o _prosody -g _prosody /etc/ssl/"$1".crt /etc/prosody/certs/"$1".crt;
        fi

        rcctl reload nginx prosody dovecot;
        rcctl restart smtpd;    
        ;;
    1)
        echo "acme-client a rencontré un problème dans le renouvellement des certificat https pour le domaine $1 " |  mail -s "`hostname` Problème de certificat HTTPS " root;
        ;;
    *)
        exit 0;
        ;;
esac