#!/usr/bin/env bash
#
# Symbol version checking OpenConnect
#
# Copyright © David Woodhouse <dwmw2@infradead.org>
#
# Author: David Woodhouse <dwmw2@infradead.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# version 2.1, as published by the Free Software Foundation.
#
# 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
# Lesser General Public License for more details.


# Consider a command line like the following:
#
# openconnect -c --authenticate\ -k -k "'"'"'.pem --authgroup 'foo
# bar' --o\s linux-64 myserver


OPENCONNECT_H="${OPENCONNECT_H:-${top_srcdir}/openconnect.h}"
MAPFILE="${MAPFILE:-${top_srcdir}/libopenconnect.map.in}"
SEDFILE="${SEDFILE:-${top_srcdir}/gensymbols.sed}"
SYMBOLSFILE="${SYMBOLESFILE:-${top_srcdir}/libopenconnect5.symbols}"


TMPSYMBOLS=symbols.$$.tmp

function cleanup {
    rm -f ${TMPSYMBOLS}
}

trap cleanup EXIT

( sed -Enf ${SEDFILE} ${OPENCONNECT_H} | \
      sed -Enf- ${MAPFILE} ) > $TMPSYMBOLS

SYMSBAD=no

while read SYM OCVER; do
    if ! grep -q "$SYM $OCVER" "$TMPSYMBOLS"; then
	echo "Missing symbol ${SYM}"
	SYMSBAD=yes
    fi
done < "$SYMBOLSFILE"

while read SYM OCVER; do
    if ! grep -q "$SYM $OCVER" "$SYMBOLSFILE"; then
	echo "New symbol ${SYM}"
	SYMSBAD=yes
    fi
done < "$TMPSYMBOLS"

if [ "$SYMSBAD" = "yes" ]; then
    echo "Symbols from *released* versions of OpenConnect have retrospectively changed!"
    exit 1
fi

APIMAJOR="$(sed -n 's/^#define OPENCONNECT_API_VERSION_MAJOR \(.*\)/\1/p' ${OPENCONNECT_H})"
APIMINOR="$(sed -n 's/^#define OPENCONNECT_API_VERSION_MINOR \(.*\)/\1/p' ${OPENCONNECT_H})"

LASTVER="$(sed -En "/^ \* API version [0-9]+.[0-9]+.*/{p;q;}" ${OPENCONNECT_H})"
if ! grep -q "API version $APIMAJOR.$APIMINOR" <<< "$LASTVER"; then
   echo "API $APIMAJOR.$APIMINOR is not the latest?"
   exit 1
fi

exit 0
