#!/usr/bin/bash
set -eu

## maximal age of file in minutes:
age="15"

pbook="lmn-client"
logdir="/tmp/collector"
debug=false

## date of latest git commit in ansible repository:
git_date="$(date --iso-8601=seconds --date="$(git log --date=iso-strict | \
                                 head -3 | sed -nE "s/^Date:\s+(.+)$/\1/p")")"

echo "Latest commit in git at: $git_date."
#dir="$(mktemp -d)"
dir="/tmp/emitter"
mkdir -vp "$dir"
touch "$dir/${git_date//T*/}"

hlist=""
n=0
running=0

find_outdated(){
    hlist=""
    n=0
    running=0
    while IFS= read -r -d '' file ; do
        running=$(( running + 1 ))
        $debug && echo -n "Processing host '$file' with IP address "
        d="$(sed -nE "s/^2\s+(\S.+)$/\1/p" "$file")"
        if [[ -z "$d" ]] || \
               [[ $(date --date="$d" +%s) -lt $(date --date="$git_date" +%s) ]] ; then
            r='([0-9]{1,3}\.){3}[0-9]{1,3}'
            ipa="$(sed -nE "s/^3\s+default via.+ src ($r) metric.+/\1/p" "$file")"
            if [[ -z "$ipa" ]] ; then
                # FIXME: Outdated report format, try fallback:
                ipa="$(sed -nE "s|^.+default via.+ src ($r) metric.+|\1|p" "$file" | head -1)"
            fi
            $debug && echo "'$ipa'."
            if ! grep -q "$ipa" "$dir/${git_date//T*/}" ; then
                echo "$ipa" >> "$dir/${git_date//T*/}"
                hlist="$hlist,$ipa"
                n=$(( n + 1 ))
            else
                $debug && echo "Host already processed before."
            fi
        fi
    done <  <(find "$logdir" -maxdepth 1 -type f -mmin -$age -print0)
    hlist="${hlist//^,/}"
    echo -n "Running hosts: $running, to be upgraded: $n. "
}

run_ansible(){
    local hsts="$1"
    if [[ -n "$hsts" ]] ; then
        if ! echo | ANSIBLE_RETRY_FILES_ENABLED=1 \
                        ANSIBLE_RETRY_FILES_SAVE_PATH="$dir" \
                        ansible-playbook --vault-password-file ~/.vaultpwd \
                        -bi inventory.yml "$pbook.yml" -l "$hsts" ; then
            while IFS= read -r ipa ; do
                sed -i "/$ipa/d" "$dir/${git_date//T*/}"
                echo "IP address '$ipa' removed from '$dir/${git_date//T*/}'."
            done < "$dir/$pbook.retry"
        fi
    fi
}

#################
while true ; do
    date --rfc-3339=seconds
    find_outdated
    run_ansible "$hlist"
    t=$(( 600/(n*n+1) ))
    echo -n "Sleeping for $t seconds now ... "
    sleep $t
done