#!/bin/sh
#
# Copyright (c) 2017-2019 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-or-later

: ${YEAR_FILE:=$1}
: ${DEFAULT_YEAR:=$2}
LC_TIME=C; export LC_TIME

year=

[ -n "${YEAR_FILE}" ] || {
	echo >&2 "$0 $(dirname "$0")/.year [DEFAULT_YEAR]"
	exit 1
}

[ -f "${YEAR_FILE}" ] && year="$(cat "${YEAR_FILE}")"

[ -n "${year}" ] ||
	year="$(date -u +%Y -d "$(git show -s --format=format:%cD)")"

[ -n "${year}" ] ||
	year="${DEFAULT_YEAR}"

[ -n "${year}" ] ||
	[ -z "${SOURCE_DATE_EPOCH-}" ] ||
	year="$(date -u +%Y -d "@${SOURCE_DATE_EPOCH}")"

[ -n "${year}" ] ||
	year="$(date -u +%Y)"

[ -n "${year}" ] || {
	printf >&2 "%s: Undefined year.\n" "$0"
	exit 1
}

printf "%s" "${year}"
