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