#!/bin/bash

homeip=/home/jaileduser/nginx/homeip
recordip=/home/jaileduser/nginx/recordip
#define where you want the bashddns log to live
bashddnslog=/var/log/bashddns.log

changetime() {

local newip=$(cat "$homeip")

#here i use @ because i want to update the root of my domain and a TTL of 3600 (standard for A records)
pdnsutil replace-rrset yourdomain.name @ A 3600 "$newip"

#increasing the serial number in the SOA so the slave nameserver is notified of changes to the domain
pdnsutil increase-serial yourdomain.name

#updating the record ip file with the new ip
cat "$homeip" > "$recordip"

#the following last line is optional
#it uses ssmtp to send an email notification when changes to the A record are made
#ssmtp or any other command line compatible mail sending utility will work

echo -e "Subject: domain.name updated A record\nYour home ip has changed!" | ssmtp mail@site.name

}

#though we made sure on the nginx server not to send the file if it was empty
#we will implement a double check here for redundancy
#only run the following *if* homeip is not empty 

if ! [ -z "$(cat "$homeip")" ] ; then

#if homeip/recordip files are the same do nothing, else run changetime
#log all actions to a centralized file for auditing

diff "$homeip" "$recordip" && \
echo "A record matches homeip at $(date -u) no action taken" >> "$bashddnslog" \
&& exit 0 \
|| changetime && echo "changed A record at $(date -u) to "$(cat "$recordip")"" >> "$bashddnslog"

else
echo "no action taken due to homeip file being empty at $(date -u)" >> "$bashddnslog"
fi