#!/usr/bin/env bash
#
# lib/lvpn-unknown-peers
#
# Copyright (c) 2011-2013 LibreVPN <vpn@hackcoop.com.ar>
#
# See AUTHORS for a list of contributors
#
# 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
# 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/>.
#
#
# Lista los pares desconocidos

. "${LVPN_LIBDIR}"/common

requires sort uniq grep tr

while getopts "hc" arg; do
    case $arg in
        h) help ${self} ; exit 0;;
        c) count="-c" ;;
    esac
done
let OPTIND--; shift ${OPTIND}

log="${1:-/var/log/tinc.${NETWORK}.log}"

if [ ! -f "${log}" ]; then
    fatal_error "El archivo de log no existe.  Tip: hay que iniciar tinc con --logfile"
fi

test ! -z "${count}" && msg "Pares desconocidos"
grep "Peer .* had unknown identity " ${log} 2>/dev/null | \
    grep -o "([^)]\+)" | sort | tr -d "[()]" | uniq ${count}

exit $?
