Update configuration file based on dynamic ip address

I’ve had a IPsec VPN server for a while now. But I havn’t bothered to get the configuration automatically updated when my dynamic ip address changes. This doesn’t change very often, but if my router reboots or if there is a power outage my ip address usually changes when my server comes back up again.
So when that happens, I would want to have access to my home-network from my VPN server. And that requires configuration update to my IPsec config.
This is how I do it, and some bits of the script is taken from another related script: Dynamically update DNS entries via API calls
The differences are that there is more checks here. I don’t want to restart or update the server if ipsec verify fails. So checking the status before activating the new configuration is a must.