#!/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")")"
#dir="$(mktemp -d)"
dir="/tmp/emitter"
mkdir -vp "$dir"
touch "$dir/${git_date//T*/}"

hlist=""
n=0

find_outdated(){
    hlist=""
    n=0
    while IFS= read -r -d '' file ; do
        $debug && echo -n "Processing host '$file' with IP address "
        d="$(sed -nE "s/\s+drwx.+([0-9]{4}-[0-9]{2}-[0-9]{2} \S+).*/\1/p" "$file" | head -1)"
        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|^\s+default via.+ src ($r) metric.+|\1|p" "$file")"
            if [[ -z "$ipa" ]] ; then
                # FIXME: Outdated report format, trying fallback:
                ipa="$(grep -E "\s+2: en" -A3 "$file" | \
                        sed -nE "s|.+inet (10\.190\.[[:digit:]]{1,3}\.[[:digit:]]{1,3})/.+|\1|p")"
            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//^,/}"
}

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