1#!/bin/sh -u
2
3# Copyright (c) 2020 Yubico AB. All rights reserved.
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file.
6# SPDX-License-Identifier: BSD-2-Clause
7
8sort_by_id() {
9	awk '{ printf "%d\n", $3 }' | sort -Cnu
10}
11
12if ! grep '^vendor' "$1" | sort_by_id; then
13	echo unsorted vendor section 1>&2
14	exit 1
15fi
16
17VENDORS=$(grep '^vendor' "$1" | awk '{ print $2 }')
18PRODUCTS=$(grep '^product' "$1" | awk '{ print $2 }' | uniq)
19
20if [ "${VENDORS}" != "${PRODUCTS}" ]; then
21	echo vendors: "$(echo "${VENDORS}" | tr '\n' ',')" 1>&2
22	echo products: "$(echo "${PRODUCTS}" | tr '\n' ',')" 1>&2
23	echo vendors and products in different order 1>&2
24	exit 2
25fi
26
27for v in ${VENDORS}; do
28	if ! grep "^product ${v}" "$1" | sort_by_id; then
29		echo "${v}": unsorted product section 1>&2
30		exit 3
31	fi
32done
33