1#!/usr/bin/env bash
2
3ENGINE_DIR=$1
4VERSION=$2
5
6if [ -z "$ENGINE_DIR" ] || [ -z "$VERSION" ]; then
7    echo 'usage: ./gen-static-ver ${ENGINE_DIR} ${VERSION}'
8    exit 1
9fi
10
11DATE_COMMAND="date"
12if [[ $(uname) -eq "Darwin" ]]; then
13    DATE_COMMAND="docker run --rm alpine date"
14fi
15GIT_COMMAND="git -C $ENGINE_DIR"
16
17staticVersion="$VERSION"
18if [[ "$VERSION" == *-dev ]]; then
19    # based on golang's pseudo-version: https://groups.google.com/forum/#!topic/golang-dev/a5PqQuBljF4
20    #
21    # using a "pseudo-version" of the form v0.0.0-yyyymmddhhmmss-abcdefabcdef,
22    # where the time is the commit time in UTC and the final suffix is the prefix
23    # of the commit hash. The time portion ensures that two pseudo-versions can
24    # be compared to determine which happened later, the commit hash identifes
25    # the underlying commit, and the v0.0.0- prefix identifies the pseudo-version
26    # as a pre-release before version v0.0.0, so that the go command prefers any
27    # tagged release over any pseudo-version.
28    gitUnix="$($GIT_COMMAND log -1 --pretty='%ct')"
29    gitDate="$($DATE_COMMAND --utc --date "@$gitUnix" +'%Y%m%d%H%M%S')"
30    gitCommit="$($GIT_COMMAND log -1 --pretty='%h')"
31    # staticVersion is now something like '0.0.0-20180719213702-cd5e2db'
32    staticVersion="0.0.0-${gitDate}-${gitCommit}"
33fi
34
35echo "$staticVersion"
36