45a85c247b06b392b76af90f421e3a075a6afdab
[cfd] / confsubst
1 #! /bin/sh
2 ### -*-sh-*-
3 ###
4 ### Make autoconf-like substitutions in files
5 ###
6 ### (c) 2008 Mark Wooding
7 ###
8
9 ###----- Licensing notice ---------------------------------------------------
10 ###
11 ### This file is part of the Common Files Distribution (`common').
12 ###
13 ### `Common' is free software; you can redistribute it and/or modify
14 ### it under the terms of the GNU General Public License as published by
15 ### the Free Software Foundation; either version 2 of the License, or
16 ### (at your option) any later version.
17 ###
18 ### `Common' is distributed in the hope that it will be useful,
19 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ### GNU General Public License for more details.
22 ###
23 ### You should have received a copy of the GNU General Public License
24 ### along with `common'; if not, write to the Free Software Foundation,
25 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 set -e
28
29 VERSION="@VERSION@"
30
31 ###--------------------------------------------------------------------------
32 ### Parse command line arguments.
33
34 while [ $# -gt 0 ]; do
35 case $1 in
36 -h | --h | --he | --hel | --help)
37 cat <<EOF
38 Usage: confsubst FILE TAG=VALUE...
39
40 Replaces occurrences of @TAG@ in FILE with VALUE, and writes the result to
41 standard output.
42 EOF
43 exit 0
44 ;;
45 -v | --v | --ve | --ver | --vers | --versi | --versio | --version)
46 echo "confsubst: Common Files Distribution version $VERSION"
47 exit 0
48 ;;
49 --)
50 shift
51 break
52 ;;
53 -)
54 break
55 ;;
56 -*)
57 echo "confsubst: unknown option \`$1'" >&2
58 exit 1
59 ;;
60 *)
61 break
62 ;;
63 esac
64 shift
65 done
66
67 if [ $# -lt 1 ]; then
68 echo >&2 "Usage: confsubst FILE TAG=VALUE..."
69 exit 1
70 fi
71 file=$1; shift
72
73 ###--------------------------------------------------------------------------
74 ### Main code.
75
76 subst=""
77 for fixup; do
78 case "$fixup" in
79 *?=*?) ;;
80 *) echo >&2 "$0: bad substitution: $fixup"; exit 1 ;;
81 esac
82 tag=$(echo "$fixup" | sed 's/=.*$//') && \
83 value=$(echo "$fixup" | sed 's/^[^=]*=//') && \
84 subst="$subst s\a@$tag@\a$value\ag;"
85 done
86
87 sed "$subst" $file || exit $?
88
89 ###----- That's all, folks --------------------------------------------------