• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2##
3##  Copyright (c) 2014 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
12if [ "$(uname -o 2>/dev/null)" = "Cygwin" ] \
13   && cygpath --help >/dev/null 2>&1; then
14    FIXPATH='cygpath -m'
15else
16    FIXPATH='echo_path'
17fi
18
19die() {
20    echo "${self_basename}: $@" >&2
21    exit 1
22}
23
24die_unknown(){
25    echo "Unknown option \"$1\"." >&2
26    echo "See ${self_basename} --help for available options." >&2
27    exit 1
28}
29
30echo_path() {
31    for path; do
32        echo "$path"
33    done
34}
35
36# Output one, possibly changed based on the system, path per line.
37fix_path() {
38    $FIXPATH "$@"
39}
40
41# Corrects the paths in file_list in one pass for efficiency.
42# $1 is the name of the array to be modified.
43fix_file_list() {
44    if [ "${FIXPATH}" = "echo_path" ] ; then
45      # When used with echo_path, fix_file_list is a no-op. Avoid warning about
46      # unsupported 'declare -n' when it is not important.
47      return 0
48    elif [ "${BASH_VERSINFO}" -lt 4 ] ; then
49      echo "Cygwin path conversion has failed. Please use a version of bash"
50      echo "which supports nameref (-n), introduced in bash 4.3"
51      return 1
52    fi
53    declare -n array_ref=$1
54    files=$(fix_path "${array_ref[@]}")
55    local IFS=$'\n'
56    array_ref=($files)
57}
58
59generate_uuid() {
60    local hex="0123456789ABCDEF"
61    local i
62    local uuid=""
63    local j
64    #93995380-89BD-4b04-88EB-625FBE52EBFB
65    for ((i=0; i<32; i++)); do
66        (( j = $RANDOM % 16 ))
67        uuid="${uuid}${hex:$j:1}"
68    done
69    echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}"
70}
71
72indent1="    "
73indent=""
74indent_push() {
75    indent="${indent}${indent1}"
76}
77indent_pop() {
78    indent="${indent%${indent1}}"
79}
80
81tag_attributes() {
82    for opt in "$@"; do
83        optval="${opt#*=}"
84        [ -n "${optval}" ] ||
85            die "Missing attribute value in '$opt' while generating $tag tag"
86        echo "${indent}${opt%%=*}=\"${optval}\""
87    done
88}
89
90open_tag() {
91    local tag=$1
92    shift
93    if [ $# -ne 0 ]; then
94        echo "${indent}<${tag}"
95        indent_push
96        tag_attributes "$@"
97        echo "${indent}>"
98    else
99        echo "${indent}<${tag}>"
100        indent_push
101    fi
102}
103
104close_tag() {
105    local tag=$1
106    indent_pop
107    echo "${indent}</${tag}>"
108}
109
110tag() {
111    local tag=$1
112    shift
113    if [ $# -ne 0 ]; then
114        echo "${indent}<${tag}"
115        indent_push
116        tag_attributes "$@"
117        indent_pop
118        echo "${indent}/>"
119    else
120        echo "${indent}<${tag}/>"
121    fi
122}
123
124