177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org#!/bin/bash
277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##
377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##
577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##  Use of this source code is governed by a BSD-style license
677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##  that can be found in the LICENSE file in the root of the source
777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##  tree. An additional intellectual property rights grant can be found
877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##  in the file PATENTS.  All contributing project authors may
977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##  be found in the AUTHORS file in the root of the source tree.
1077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org##
1177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
1277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] \
1377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org   && cygpath --help >/dev/null 2>&1; then
1477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    FIXPATH='cygpath -m'
1577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgelse
1688b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    FIXPATH='echo_path'
1777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgfi
1877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
1977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgdie() {
2077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    echo "${self_basename}: $@" >&2
2177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    exit 1
2277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
2377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
2477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgdie_unknown(){
2577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    echo "Unknown option \"$1\"." >&2
2677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    echo "See ${self_basename} --help for available options." >&2
2777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    exit 1
2877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
2977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
3088b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.orgecho_path() {
3188b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    for path; do
3288b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org        echo "$path"
3388b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    done
3488b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org}
3588b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org
3688b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org# Output one, possibly changed based on the system, path per line.
3777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgfix_path() {
3888b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    $FIXPATH "$@"
3988b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org}
4088b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org
4188b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org# Corrects the paths in file_list in one pass for efficiency.
4288b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.orgfix_file_list() {
4388b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    # TODO(jzern): this could be more generic and take the array as a param.
4488b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    files=$(fix_path "${file_list[@]}")
4588b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    local IFS=$'\n'
4688b47b29cc274dd19cddc37c1ce1834d97df282efgalligan@chromium.org    file_list=($files)
4777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
4877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
4977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orggenerate_uuid() {
5077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local hex="0123456789ABCDEF"
5177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local i
5277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local uuid=""
5377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local j
5477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    #93995380-89BD-4b04-88EB-625FBE52EBFB
5577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    for ((i=0; i<32; i++)); do
5677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        (( j = $RANDOM % 16 ))
5777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        uuid="${uuid}${hex:$j:1}"
5877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    done
5977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}"
6077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
6177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
6277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgindent1="    "
6377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgindent=""
6477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgindent_push() {
6577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    indent="${indent}${indent1}"
6677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
6777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgindent_pop() {
6877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    indent="${indent%${indent1}}"
6977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
7077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
7177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgtag_attributes() {
7277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    for opt in "$@"; do
7377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        optval="${opt#*=}"
7477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        [ -n "${optval}" ] ||
7577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org            die "Missing attribute value in '$opt' while generating $tag tag"
7677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}${opt%%=*}=\"${optval}\""
7777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    done
7877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
7977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
8077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgopen_tag() {
8177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local tag=$1
8277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    shift
8377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    if [ $# -ne 0 ]; then
8477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}<${tag}"
8577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        indent_push
8677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        tag_attributes "$@"
8777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}>"
8877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    else
8977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}<${tag}>"
9077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        indent_push
9177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    fi
9277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
9377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
9477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgclose_tag() {
9577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local tag=$1
9677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    indent_pop
9777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    echo "${indent}</${tag}>"
9877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
9977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
10077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.orgtag() {
10177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    local tag=$1
10277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    shift
10377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    if [ $# -ne 0 ]; then
10477496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}<${tag}"
10577496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        indent_push
10677496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        tag_attributes "$@"
10777496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        indent_pop
10877496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}/>"
10977496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    else
11077496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org        echo "${indent}<${tag}/>"
11177496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org    fi
11277496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org}
11377496404dc182c2f4a5f86ebabffe1d1ceb81e7ejohannkoenig@chromium.org
114