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.
42fix_file_list() {
43    # TODO(jzern): this could be more generic and take the array as a param.
44    files=$(fix_path "${file_list[@]}")
45    local IFS=$'\n'
46    file_list=($files)
47}
48
49generate_uuid() {
50    local hex="0123456789ABCDEF"
51    local i
52    local uuid=""
53    local j
54    #93995380-89BD-4b04-88EB-625FBE52EBFB
55    for ((i=0; i<32; i++)); do
56        (( j = $RANDOM % 16 ))
57        uuid="${uuid}${hex:$j:1}"
58    done
59    echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}"
60}
61
62indent1="    "
63indent=""
64indent_push() {
65    indent="${indent}${indent1}"
66}
67indent_pop() {
68    indent="${indent%${indent1}}"
69}
70
71tag_attributes() {
72    for opt in "$@"; do
73        optval="${opt#*=}"
74        [ -n "${optval}" ] ||
75            die "Missing attribute value in '$opt' while generating $tag tag"
76        echo "${indent}${opt%%=*}=\"${optval}\""
77    done
78}
79
80open_tag() {
81    local tag=$1
82    shift
83    if [ $# -ne 0 ]; then
84        echo "${indent}<${tag}"
85        indent_push
86        tag_attributes "$@"
87        echo "${indent}>"
88    else
89        echo "${indent}<${tag}>"
90        indent_push
91    fi
92}
93
94close_tag() {
95    local tag=$1
96    indent_pop
97    echo "${indent}</${tag}>"
98}
99
100tag() {
101    local tag=$1
102    shift
103    if [ $# -ne 0 ]; then
104        echo "${indent}<${tag}"
105        indent_push
106        tag_attributes "$@"
107        indent_pop
108        echo "${indent}/>"
109    else
110        echo "${indent}<${tag}/>"
111    fi
112}
113
114