1#! /bin/sh
2
3# Output the contents of a shell script with sourced files inlined.
4# Written by Gary V. Vaughan, 2012
5
6# Copyright (C) 2012-2015 Free Software Foundation, Inc.
7# This is free software; see the source for copying conditions.  There is NO
8# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
10# Source required external libraries:
11. `echo "$0" |${SED-sed} 's|[^/]*$||'`"funclib.sh"
12. `echo "$0" |${SED-sed} 's|[^/]*$||'`"options-parser"
13
14# Set a version string for *this* script.
15scriptversion=2014-01-03.01; # UTC
16
17
18# This program is free software: you can redistribute it and/or modify
19# it under the terms of the GNU General Public License as published by
20# the Free Software Foundation, either version 3 of the License, or
21# (at your option) any later version.
22
23# This program is distributed in the hope that it will be useful,
24# but WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26# GNU General Public License for more details.
27
28# You should have received a copy of the GNU General Public License
29# along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
31# Please report bugs or propose patches to bug-libtool@gnu.org.
32
33
34## ------ ##
35## Usage. ##
36## ------ ##
37
38# Run 'build-aux/inline-source --help' for help with using this script
39# from the command line.
40
41# Recursively scan through a FILE passed on the command line, replacing
42# either of the following:
43#   . "relative/file"
44#   . `echo "$0" |edit`"relative/file"
45# with the contents of the referenced files.
46
47
48## ---------------- ##
49## Options parsing. ##
50## ---------------- ##
51
52usage='$progpath [OPTION]... FILE'
53
54# Short help message in response to '-h'.
55usage_message='Options:
56       --debug        enable verbose shell tracing
57       --version      print version information and exit
58   -h, --help         print help message and exit
59'
60
61long_help_message="\
62Report bugs to <bug-libtool@gnu.org>
63General help using GNU software: <http://www.gnu.org/gethelp/>."
64
65func_options ${1+"$@"}
66eval set dummy "$func_options_result"; shift
67
68
69## -------------------- ##
70## Resource management. ##
71## -------------------- ##
72
73# require_AWK
74# -----------
75# Search for a "not hopeless" awk.
76require_AWK=func_require_AWK
77func_require_AWK ()
78{
79    $debug_cmd
80
81    test -n "$AWK" || {
82      # Find the first executable in the list.
83      for _G_prog in gawk mawk nawk awk
84      do
85        require_AWK_IFS=$IFS
86	IFS=${PATH_SEPARATOR-:}
87	for _G_dir in $PATH
88        do
89	  IFS=$require_AWK_IFS
90          if test -f "$_G_dir/$_G_prog" && test -x "$_G_dir/$_G_prog"
91	  then
92	    AWK=$_G_dir/$_G_prog
93            break 2
94          fi
95        done
96	IFS=$require_AWK_IFS
97      done
98    }
99
100    test -n "$AWK" || func_fatal_error "\
101Please install GNU Awk, or 'export AWK=/path/to/gnu/awk'."
102
103    func_verbose "found '$AWK'."
104
105    require_AWK=:
106}
107
108
109## --------------- ##
110## Core functions. ##
111## --------------- ##
112
113# func_include LINE
114# -----------------
115# Output the contents of file included by LINE.
116func_include ()
117{
118    $require_AWK
119
120    test -f "$1" \
121        || func_fatal_error "file '$1' not found"
122
123    _G_scriptdir=`echo "$1" |$SED 's|[^/]*$||'`
124    test -n "$_G_scriptdir" || _G_scriptdir="./"
125
126    $AWK '
127        BEGIN { magic = '${_RECURSE_MAGIC-0}'; }
128
129        /^#!/ && magic == 0 {
130            print $0;
131            print "## DO NOT EDIT - This file generated from '$1'";
132            print "##               by '$progname' v'$scriptversion'";
133            magic++;
134            next;
135        }
136
137        /^\. ['\''"].*['\''"]$/ {
138            file = substr ($2, 2, length ($2) -2);
139	    system (sprintf ("env _RECURSE_MAGIC=%d '$progpath' %s", magic, file));
140            next;
141        }
142
143        /^\. `echo [^`]*`['\''"][^'\''"]*['\''"]$/ {
144            tail = substr ($0, match ($0, /`['\''"]/));
145	    file = substr (tail, 3, length (tail) -3);
146	    system (sprintf ("env _RECURSE_MAGIC=%d '$progpath' '"$_G_scriptdir"'%s", magic, file));
147            next;
148        }
149
150        { print; }
151    ' < "$1"
152}
153
154func_include "$1"
155
156exit 0
157
158# Local variables:
159# mode: shell-script
160# sh-indentation: 2
161# eval: (add-hook 'before-save-hook 'time-stamp)
162# time-stamp-pattern: "20/scriptversion=%:y-%02m-%02d.%02H; # UTC"
163# time-stamp-time-zone: "UTC"
164# End:
165