#!/bin/sh

# Check that all files that should have the current version agree on it

# Makefile has the version that other sources must match
MASTER_PATH=Makefile
MASTER_VERS=$(grep '^VERSION.*:=' $MASTER_PATH |sed -e 's/^.*:=[[:space:]]*//')

# Assume success
RES=0

# Check debian/changelog (only warn on mismatch)
DEB_PATH=debian/changelog
DEB_VERS=$(head -1 $DEB_PATH | sed -e 's/^.*(//' -e 's/).*$//')

if [ "$DEB_VERS" != "$MASTER_VERS" ]; then
  echo >&2 "$MASTER_PATH $MASTER_VERS vs $DEB_PATH $DEB_VERS (ignored)"
fi

# Check rpm/libdsme.rpm
RPM_PATH=${RPM_SOURCE_DIR:-rpm}/${RPM_PACKAGE_NAME:-libdsme}.spec
RPM_VERS=$(grep '^Version:' $RPM_PATH |sed -e 's/^.*:[[:space:]]*//')
# Remove initial part of rpm version  that equals with version from Makefile
RPM_XTRA=${RPM_VERS#$MASTER_VERS}
# From that remove initial part that equals with version from spec-file
RPM_XTRA=${RPM_XTRA#$RPM_VERS}
# If the result is non-empty string, then OBS is doing
# delta-after-matching-tag kind of build, which is ok.
# But empty string means that spec and Makefile are completely
# out of sync, which is bad.

if [ "$RPM_VERS" != "$MASTER_VERS" ]; then
  if [ -z "$RPM_XTRA" ]; then
    echo >&2 "$MASTER_PATH $MASTER_VERS vs $RPM_PATH $RPM_VERS"
    RES=1
  else
    echo "(ignoring patch level in spec file: $RPM_XTRA)"
  fi
fi

# Check pkgconfig files
for PC_PATH in dsme.pc dsme_dbus_if.pc thermalmanager_dbus_if.pc; do
  PC_VERS=$(grep '^Version:' $PC_PATH |sed -e 's/^.*:[[:space:]]*//')
  if [ "$PC_VERS" != "$MASTER_VERS" ]; then
    echo >&2 "$MASTER_PATH $MASTER_VERS vs $PC_PATH $PC_VERS"
    RES=1
  fi
done

# Done
if [ $RES -ne 0 ]; then
  echo >&2 "Conflicting package versions"
fi

exit $RES
