1#!/bin/sh
2##
3##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4##
5##  Use of this source code is governed by a BSD-style license
6##  that can be found in the LICENSE file in the root of the source
7##  tree. An additional intellectual property rights grant can be found
8##  in the file PATENTS.  All contributing project authors may
9##  be found in the AUTHORS file in the root of the source tree.
10##
11
12
13
14for opt in "$@"; do
15    optval="${opt#*=}"
16    case "$opt" in
17    --bare) bare=true ;;
18    *) break ;;
19    esac
20    shift
21done
22source_path=${1:-.}
23out_file=${2}
24id=${3:-VERSION_STRING}
25
26git_version_id=""
27if [ -e "${source_path}/.git" ]; then
28    # Source Path is a git working copy. Check for local modifications.
29    # Note that git submodules may have a file as .git, not a directory.
30    export GIT_DIR="${source_path}/.git"
31    git_version_id=`git describe --match=v[0-9]* 2>/dev/null`
32fi
33
34changelog_version=""
35for p in "${source_path}" "${source_path}/.."; do
36    if [ -z "$git_version_id" -a -f "${p}/CHANGELOG" ]; then
37        changelog_version=`head -n1 "${p}/CHANGELOG" | awk '{print $2}'`
38        changelog_version="${changelog_version}"
39        break
40    fi
41done
42version_str="${changelog_version}${git_version_id}"
43bare_version=${version_str#v}
44major_version=${bare_version%%.*}
45bare_version=${bare_version#*.}
46minor_version=${bare_version%%.*}
47bare_version=${bare_version#*.}
48patch_version=${bare_version%%-*}
49bare_version=${bare_version#${patch_version}}
50extra_version=${bare_version##-}
51
52#since they'll be used as integers below make sure they are or force to 0
53for v in major_version minor_version patch_version; do
54    if eval echo \$$v |grep -E -q '[^[:digit:]]'; then
55        eval $v=0
56    fi
57done
58
59if [ ${bare} ]; then
60    echo "${changelog_version}${git_version_id}" > $$.tmp
61else
62    cat<<EOF>$$.tmp
63// This file is generated. Do not edit.
64#define VERSION_MAJOR  $major_version
65#define VERSION_MINOR  $minor_version
66#define VERSION_PATCH  $patch_version
67#define VERSION_EXTRA  "$extra_version"
68#define VERSION_PACKED ((VERSION_MAJOR<<16)|(VERSION_MINOR<<8)|(VERSION_PATCH))
69#define ${id}_NOSP "${version_str}"
70#define ${id}      " ${version_str}"
71EOF
72fi
73if [ -n "$out_file" ]; then
74diff $$.tmp ${out_file} >/dev/null 2>&1 || cat $$.tmp > ${out_file}
75else
76cat $$.tmp
77fi
78rm $$.tmp
79