#!/bin/bash
set -eu
BASEDIR=$(dirname "$0")
cd ${BASEDIR}
. ${BASEDIR}/config.sh
sleep $[ ( $RANDOM % 30 ) + 1 ]s
touch lastaddr.txt
OLDADDR=$(cat lastaddr.txt)
NEWADDR=$(curl -s -f https://httpbin.org/ip | jq -r .origin)
if [ "${OLDADDR}" = "${NEWADDR}" ]; then
exit 0
fi
if [ "${NEWADDR}" = "null" ]; then
exit 1
fi
echo "Address changed: ${OLDADDR} -> ${NEWADDR}"
echo ${NEWADDR} > lastaddr.txt
/usr/local/bin/sacloudns rset --zone=${DDNS_ZONE} --ttl=300 --name=${DDNS_DOMAIN} --type=A --data=${NEWADDR}