1#!/bin/sh
2
3export LC_ALL=C
4
5version_h="version.h"
6print=yes
7
8for ac_option do
9  ac_arg=$(echo $ac_option | cut -d '=' -f 2-)
10  case "$ac_option" in
11  --extra=*)
12    extra="-$ac_arg"
13    ;;
14  --versionh=*)
15    version_h="$(pwd)/$ac_arg"
16    print=no
17    ;;
18  --cwd=*)
19    cwd="$ac_arg"
20    ;;
21  *)
22    echo "Unknown parameter: $ac_option" >&2
23    exit 1
24    ;;
25
26  esac
27done
28
29if test "$cwd" ; then
30  cd "$cwd"
31fi
32
33# Extract revision number from file used by daily tarball snapshots
34# or from "git describe" output
35git_revision=$(cat snapshot_version 2> /dev/null)
36test "$git_revision" || test ! -e .git || git_revision="$(git describe \
37    --match "v[0-9]*" --always --tags --dirty | sed 's/^v//')"
38version="$git_revision"
39
40# other tarballs extract the version number from the VERSION file
41if test ! "$version"; then
42    version="$(cat VERSION 2> /dev/null)"
43fi
44
45test "$version" || version=UNKNOWN
46
47VERSION="${version}${extra}"
48
49if test "$print" = yes ; then
50    echo "$VERSION"
51    exit 0
52fi
53
54NEW_REVISION="#define VERSION \"${VERSION}\""
55OLD_REVISION=$(head -n 1 "$version_h" 2> /dev/null)
56BUILDDATE="#define BUILDDATE \"$(date)\""
57MPVCOPYRIGHT="#define MPVCOPYRIGHT \"Copyright © 2000-2021 mpv/MPlayer/mplayer2 projects\""
58
59# Update version.h only on revision changes to avoid spurious rebuilds
60if test "$NEW_REVISION" != "$OLD_REVISION"; then
61    cat <<EOF > "$version_h"
62$NEW_REVISION
63$BUILDDATE
64$MPVCOPYRIGHT
65EOF
66fi
67