Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
freedombone-freedns 2.41 KiB
#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# freedns update command for use in cron or a daemon

# License
# =======
#
# Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-freedns
export TEXTDOMAINDIR="/usr/share/locale"

VERBOSE=
CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
FREEDNS_WGET='wget --read-timeout=0.0 --waitretry=5 --tries=4 https://freedns.afraid.org/dynamic/update.php?'

if [[ "$1" == "--verbose" || "$1" == "-v" ]]; then
    VERBOSE=1
fi

if [ ! -f "$CONFIGURATION_FILE" ]; then
    exit 0
fi

function item_in_array {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
}

detected_codes=()
codelines=$(grep "_CODE=" "$CONFIGURATION_FILE" | uniq)
while read -r line; do
    code=$(echo "$line" | awk -F '=' '{print $2}')
    # shellcheck disable=SC2068
    if ! item_in_array "$code" ${detected_codes[@]}; then
        detected_codes+=("$code")
    fi
done <<< "$codelines"

if [ ! -d "$HOME/.freedns-update" ]; then
    mkdir "$HOME/.freedns-update"
fi
cd "$HOME/.freedns-update" || exit 2467824684
# shellcheck disable=SC2068
for code in ${detected_codes[@]}
do
    if [ $VERBOSE ]; then
        echo $"command: $FREEDNS_WGET${code}="
        # shellcheck disable=SC1066,SC1067
        $FREEDNS_WGET${code}=
    else
        if [ -f /tmp/freedns ]; then
            rm /tmp/freedns
        fi
        # shellcheck disable=SC1066,SC1067,SC1007
        $FREEDNS_WGET${code}= >> /tmp/freedns 2>&1
    fi
done

if [ -f /tmp/freedns ]; then
    rm /tmp/freedns
fi

exit 0