1 # Setup functions and settings used in subdirectories.
3 # Their setup.sh script sources this file.
5 # Copyright (C) 2009-2018 Simon Ruderich
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # csh gives the error "Unknown colorls variable `su'." when used with newer
22 # options supported by Zsh or GNU ls.
26 # Check if the given program is installed. `type` is portable, `which` is not.
28 type "$1" >/dev/null 2>&1
30 # Get the path of the given program. Thanks to Gilles on [1] (read on
31 # 2013-03-10) for the PATH-walking idea. `which` is not portable and `type`
32 # has no well-formed output format.
34 # [1]: http://unix.stackexchange.com/questions/4988/how-do-i-test-to-see-if-an-application-exists-in-path/4991
36 test -z "$1" && return 1
38 # Keep IFS change local.
42 for directory in $PATH; do
43 if test -x "$directory/$1"; then
44 printf '%s\n' "$directory/$1"
53 # Usage: cmd_i <cmd> ... <file>
55 # Run <cmd> with all arguments (including the last file) and write the result
56 # to the temporary file <file>.tmp and then rename that file to <file>. This
57 # can't be done in-place (e.g. cmd <file >file) because it truncates the file.
66 mv "$last".tmp "$last"
69 # Usage: sed_i ... <file>
71 # sed -i is not portable due to different implementations. See cmd_i.
79 # Usage: perl_line_filter <cmd> ...
81 # Run the perl command cmd on each line before printing it.
86 # Can't use -pe because it uses <> which treats the arguments as files.
87 perl -e "use strict; use warnings; while (<STDIN>) { $cmd; print; }" "$@"
90 # Usage: simple_cpp <FIRST> <SECOND> .. -- <replacement-for-first> ...
92 # Replaces each FIRST (on word boundaries) with <replacement-for-first> like a
93 # simple cpp replacement.
100 if test x"$x" = x--; then
104 cmd="$cmd s/\\b$x\\b/\$ARGV[\$i]/g; \$i++;"
107 perl_line_filter "$cmd" -- "$@"
110 # Print the current OS. The following OS are supported at the moment:
114 # - Solaris/OpenSolaris (sun)
115 # - FreeBSD (freebsd)
117 # If an unsupported OS is used an error is printed.
119 if test -f /etc/debian_version; then
121 elif test -f /etc/gentoo-release; then
123 elif test x"`uname`" = xSunOS; then
125 elif test x"`uname`" = xFreeBSD; then
128 echo 'unsupported OS!' >&2
133 # Creates a symbolic link for file $1 in dirname of $2 with name of basename
136 # `./link.sh example ~/.examplerc` creates a symbolic link to example
137 # (wherever it is located) in ~/ named .examplerc.
139 local pwd base source target >/dev/null 2>&1 || true
141 # Get all necessary paths.
143 base=`printf '%s' "$2" | sed "s|~|$HOME|"` # expand ~, some sh don't do it
144 base=`dirname "$base"`
145 source=`printf '%s' "$pwd/$1" | sed "s|$base/||"`
146 target=`basename "$2"`
148 # Go to the directory where the link is going to be created.
149 cd "$base" || return 1
151 # Abort if the target file exists and is no symbolic link. Prevents
152 # overwriting real files.
153 if test -e "$target" && test ! -h "$target"; then
154 printf 'link(): target "%s" exists already and is no symbolic link!\n' \
159 # Make sure the source exists.
160 if test ! -e "$source"; then
161 printf 'link(): source "%s" does not exist!\n' "$source" >&2
165 # Create the new symbolic link; remove the old one if necessary.
166 printf 'link(): linking "%s" to "%s"\n' "$source" "$target"
168 ln -s "$source" "$target"
170 # Go back to the directory where we were before.
171 cd "$pwd" || return 1
174 # Generate a file from a source file using a given command. A warning not to
175 # edit it is automatically added to the created file.
177 # Usage: generate <file> <extension> <cmd..>
179 # If an empty extension is provided, the file is modified in-place (through a
182 local file >/dev/null 2>&1 || true
183 local file_tmp >/dev/null 2>&1 || true
184 local extension >/dev/null 2>&1 || true
186 # Get command and target file.
192 if test -z "$extension"; then
195 # We only need this message if we generate a new file.
196 printf '%s: generating from "%s" (%s)\n' \
197 "$file" "$file$extension" "$1"
200 echo '###################################'
201 echo '# WARNING! DO NOT EDIT THIS FILE! #'
202 echo '###################################'
204 printf '# It was generated from "%s" on %s.\n' \
205 "$file$extension" "`date`"
212 # Generate $file from $file$extension using the given command.
213 "$@" <"$file$extension" >>"$file_tmp"
215 if test -z "$extension"; then
216 mv "$file_tmp" "$file"