﻿#!/bin/bash

# Always ensure the path exists to write the marker files
mkdir -p /usr/local/Somnode/certbot/

# Stop nginx if running
nginxPIDs=`pidof /usr/local/Somnode/nginx/sbin/nginx`
if [[ ! "${nginxPIDs}" == "" ]]; then
    /usr/local/Somnode/nginx/sbin/nginx -s stop
fi

# Run custom pre-hooks
if [[ -f /usr/local/Somnode/hooks/autossl_pre.sh ]]; then /usr/local/Somnode/hooks/autossl_pre.sh; fi

# Check for running services that use port 80
if (systemctl -q is-active nginx)
then
    touch /usr/local/Somnode/certbot/.nginx;
    systemctl -q stop nginx;
fi
if (systemctl -q is-active apache2)
then
    touch /usr/local/Somnode/certbot/.apache2;
    systemctl -q stop apache2;
fi
if (systemctl -q is-active httpd)
then
    touch /usr/local/Somnode/certbot/.httpd;
    systemctl -q stop httpd;
fi
if (systemctl -q is-active lshttpd)
then
    touch /usr/local/Somnode/certbot/.lshttpd;
    systemctl -q stop lshttpd;
fi
if (systemctl -q is-active centovacast)
then
    touch /usr/local/Somnode/certbot/.centovacast;
    systemctl -q stop centovacast;
elif [ -f /usr/local/Somnode/certbot/.centovacast ]; then
    rm -f /usr/local/Somnode/certbot/.centovacast
fi

exit 0;
