#!/bin/bash
#set -x
# Simple shell script to set DynDNS for a zone.
#
# Author: Ikasp Nätverkslösningar AB, lennart@ikasp.se
# License: MIT License (Free for everyone to use, modify, and distribute)
#
# Copyright (c) Ikasp Nätverkslösningar AB
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Run this script hourly from cron on a Linux/MacOS computer
# Can be run on windows computer with WSL installed: https://docs.microsoft.com/en-us/windows/wsl/
#
# Documentation: https://faq.webbmotell.se/?action=search&tagging_id=26
#
#
# INSTALL:
#
# Option 1:
# Install as root. Copy script to /etc/cron.hourly.
#
# Option 2:
#
# Install as user. Add script to users cron:
#
#             create script in home directory
#             crontab -e
#             30 * * * * /bin/sh $HOME/dyndns.sh
#
# Please change 30 to something else, 0-60 to not overload the server.
###################################################################
#
# Edit here.
#
root_domain=exempel.se
# if root domain is to be set (exempel.se in this example), set hostname to empty
hostname=hemma

token=xxxxxxxxxxx

# Must be rw for the user running the script.
# Put in home dir if script is run as non-root
workdir=$HOME
#workdir=/usr/local/etc
##################################################################
#
# Do not change anything below
#
##################################################################
zone="${root_domain}."
dnsipfile=$workdir/dnsip.dyndns-$hostname.$root_domain
logfile=$workdir/dnsip.dyndns-$hostname.$root_domain.log
tmpf=/tmp/dyndns.$$

#
# Check for curl/wget
#
if type curl >/dev/null 2>&1
then
    mycurl="curl -s"
elif type wget >/dev/null 2>&1
then
    mycurl="wget -qO-"
else
    echo "Error: Neither curl nor wget found"
    exit 1
fi

# Get current IP address
curip=$($mycurl https://kunder.support/whatismyipaddress.php)

# Validate IP address was retrieved
if [ -z "$curip" ]; then
    echo "Error: Failed to retrieve current IP address" >&2
    exit 1
fi

# Check if IP file exists and compare
if [ -f "$dnsipfile" ]; then
    # file exists and hence ip has been set
    dnsip=$(cat "$dnsipfile")
    if [ "$curip" = "$dnsip" ]; then
        # No change, exit
        exit 0
    fi
fi

# IP has changed, update DNS
$mycurl "https://ic.etableraweb.com/ddns/update.php?zone=$zone&type=A&record=$hostname&token=$token" >$tmpf 2>&1

# Log the update
if [ -s "$tmpf" ]; then
    date >>$logfile
    cat $tmpf >>$logfile
fi

# Save new IP
echo -n "$curip" >"$dnsipfile"
rm -f $tmpf

exit 0
