#!/bin/sh

# Instalação
apt-get update && \
apt-get -y install openssl ca-certificates

dlurl=https://download.sigsolucoes.net.br
cadeia=public/ca-certs-brazil/ca-certificates-brazil.tar.gz
uf=mg

cacertdir=/usr/local/share/ca-certificates
updatecmd="update-ca-certificates -v -f"

for dir in \
    /etc/pki/ca-trust/source/anchors/ \
    /usr/share/pki/ca-trust-source/anchors/ ; do

    if test -d ${dir} ; then
        cacertdir=${dir}
        updatecmd=update-ca-trust
    fi
done

wget -qO - ${dlurl}/${cadeia} | tar -C ${cacertdir}/ -xvzf - \
    && ${updatecmd}

# Teste
echo "Testando webservices da NFCe:"
for srv in nfce hnfce; do
    echo | openssl s_client \
        -connect ${srv}.fazenda.${uf}.gov.br:443 \
        -CApath /etc/ssl/certs 2>&1 \
            | grep -E 'CONNECTED|TLSv1'
done

echo "Testando webservices da NFe:"
for srv in nfe hnfe; do
    echo | openssl s_client \
        -connect ${srv}.fazenda.${uf}.gov.br:443 \
        -CApath /etc/ssl/certs 2>&1 \
            | grep -E 'CONNECTED|TLSv1'
done

echo "Testando webservices em contigência nacional:"
for srv in www hom; do
    echo | openssl s_client \
        -connect ${srv}.nfe.fazenda.gov.br:443 \
        -CApath /etc/ssl/certs 2>&1 \
            | grep -E 'CONNECTED|TLSv1'
done

exit 0
