1#!/bin/bash
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# gen_example_text.sh
14
15self=$0
16
17die_usage() {
18    echo "Usage: $self <example.txt>"
19    exit 1
20}
21
22die() {
23    echo "$@"
24    exit 1
25}
26
27include_block() {
28    local on_block
29    while IFS=$'\n' read -r t_line; do
30        case "$t_line" in
31            \~*\ ${block_name})
32                if [ "x$on_block" == "xyes" ]; then
33                    return 0;
34                else
35                    on_block=yes
36                fi
37                ;;
38            *)
39                if [ "x$on_block" == "xyes" ]; then
40                    echo "$t_line"
41                fi
42                ;;
43        esac
44    done
45    echo "WARNING: failed to find text for block $block_name" >&2
46    return 1
47}
48
49txt=$1
50[ -f "$txt" ] || die_usage
51read -r template < "$txt"
52case "$template" in
53    @TEMPLATE*) template=${txt%/*}/${template##@TEMPLATE } ;;
54    *) die "Failed to parse template name from '$template'" ;;
55esac
56
57fence="~~~~~~~~~"
58fence="${fence}${fence}"
59fence="${fence}${fence}"
60fence="${fence}${fence}"
61while IFS=$'\n' read -r line; do
62    case "$line" in
63        @TEMPLATE*)
64            template=${template##@TEMPLATE }
65            template=${template%.c}.txt
66            ;;
67        @DEFAULT)
68            include_block < "$template"
69            ;;
70        ~~~*)
71            block_name=${line##~* }
72            [ "$block_name" == "INTRODUCTION" ] || echo "$fence"
73            ;;
74        *)  echo "$line"
75            ;;
76    esac
77done < "$txt"
78
79echo
80echo "Putting It All Together"
81echo "======================="
82echo "${fence}"
83${self%/*}/gen_example_code.sh "${txt}"
84echo "${fence}"
85