1#!/usr/bin/env bash
2
3ENGINE_DIR=$1
4VERSION=$2
5
6[[ $# < 2 ]] && echo 'not enough args' && exit 1
7
8DATE_COMMAND="date"
9if [[ $(uname) -eq "Darwin" ]]; then
10    DATE_COMMAND="docker run --rm alpine date"
11fi
12
13GIT_COMMAND="git -C $ENGINE_DIR"
14origVersion="$VERSION"
15rpmVersion="$VERSION"
16rpmRelease=3
17
18# rpmRelease versioning is as follows
19# Docker 18.01.0-ce:  version=18.01.0.ce, release=3
20# Docker 18.01.0-ce-tp1: version=18.01.0.ce, release=0.1.tp1
21# Docker 18.01.0-ce-beta1: version=18.01.0.ce, release=1.1.beta1
22# Docker 18.01.0-ce-rc1: version=18.01.0.ce, release=2.1.rc1
23# Docker 18.01.0-ce-cs1: version=18.01.0.ce.cs1, release=1
24# Docker 18.01.0-ce-cs1-rc1: version=18.01.0.ce.cs1, release=0.1.rc1
25# Docker 18.01.0-ce-dev nightly: version=18.01.0.ce, release=0.0.YYYYMMDD.HHMMSS.gitHASH
26
27if [[ "$rpmVersion" =~ .*-tp[0-9]+$ ]]; then
28    tpVersion=${rpmVersion#*-tp}
29    rpmVersion=${rpmVersion%-tp*}
30    rpmRelease="0.${tpVersion}.tp${tpVersion}"
31elif [[ "$rpmVersion" =~ .*-beta[0-9]+$ ]]; then
32    betaVersion=${rpmVersion#*-beta}
33    rpmVersion=${rpmVersion%-beta*}
34    rpmRelease="1.${betaVersion}.beta${betaVersion}"
35elif [[ "$rpmVersion" =~ .*-rc[0-9]+$ ]]; then
36    rcVersion=${rpmVersion#*-rc}
37    rpmVersion=${rpmVersion%-rc*}
38    rpmRelease="2.${rcVersion}.rc${rcVersion}"
39fi
40
41DOCKER_GITCOMMIT=$($GIT_COMMAND rev-parse --short HEAD)
42if [ -n "$($GIT_COMMAND status --porcelain --untracked-files=no)" ]; then
43    DOCKER_GITCOMMIT="$DOCKER_GITCOMMIT-unsupported"
44fi
45
46# if we have a "-dev" suffix or have change in Git, let's make this package version more complex so it works better
47if [[ "$rpmVersion" == *-dev ]] || [ -n "$($GIT_COMMAND status --porcelain)" ]; then
48    # based on golang's pseudo-version: https://groups.google.com/forum/#!topic/golang-dev/a5PqQuBljF4
49    #
50    # using a "pseudo-version" of the form v0.0.0-yyyymmddhhmmss-abcdefabcdef,
51    # where the time is the commit time in UTC and the final suffix is the prefix
52    # of the commit hash. The time portion ensures that two pseudo-versions can
53    # be compared to determine which happened later, the commit hash identifes
54    # the underlying commit, and the v0.0.0- prefix identifies the pseudo-version
55    # as a pre-release before version v0.0.0, so that the go command prefers any
56    # tagged release over any pseudo-version.
57    gitUnix="$($GIT_COMMAND log -1 --pretty='%ct')"
58    gitDate="$($DATE_COMMAND --utc --date "@$gitUnix" +'%Y%m%d%H%M%S')"
59    gitCommit="$($GIT_COMMAND log -1 --pretty='%h')"
60    # rpmVersion is now something like '0.0.0-20180719213702-cd5e2db'
61    rpmVersion="0.0.0-${gitDate}-${gitCommit}"
62    rpmRelease="0"
63    origVersion=$rpmVersion
64fi
65
66# Replace any other dashes with periods
67rpmVersion="${rpmVersion//-/.}"
68echo $rpmVersion $rpmRelease $DOCKER_GITCOMMIT $origVersion
69