]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - lib.sh
Minor cleanup.
[config/dotfiles.git] / lib.sh
1 # Setup functions and settings used in subdirectories.
2 #
3 # Their setup.sh script sources this file.
4
5 # Copyright (C) 2009-2013  Simon Ruderich
6 #
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.
11 #
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.
16 #
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/>.
19
20
21 # csh gives the error "Unknown colorls variable `su'." when used with newer
22 # options supported by Zsh or GNU ls.
23 unset LS_COLORS
24
25
26 # Check if the given program is installed. `type` is portable, `which` is not.
27 installed() {
28     type "$1" >/dev/null 2>&1
29 }
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.
33 #
34 # [1]: http://unix.stackexchange.com/questions/4988/how-do-i-test-to-see-if-an-application-exists-in-path/4991
35 installed_path() {
36     test -z "$1" && return 1
37
38     # Keep IFS change local.
39     (
40         IFS=:
41         # Walk PATH.
42         for directory in $PATH; do
43             if test -x "$directory/$1"; then
44                 printf '%s\n' "$directory/$1"
45                 return 0
46             fi
47         done
48
49         return 1
50     )
51 }
52
53 # Usage: cmd_i <cmd> ... <file>
54 #
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.
58 cmd_i() {
59     # Get last argument.
60     last=
61     for x; do
62         last="$x"
63     done
64
65     "$@" >"$last".tmp
66     mv "$last".tmp "$last"
67 }
68
69 # Usage: sed_i ... <file>
70 #
71 # sed -i is not compatible due to different implementations. See cmd_i.
72 sed_i() {
73     cmd_i sed "$@"
74 }
75 grep_i() {
76     cmd_i grep "$@"
77 }
78
79 # Usage: perl_line_filter <cmd> ...
80 #
81 # Run the perl command cmd on each line before printing it.
82 perl_line_filter() {
83     cmd="$1"
84     shift
85
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; }" "$@"
88 }
89
90 # Usage: simple_cpp <FIRST> <SECOND> .. -- <replacement-for-first> ...
91 #
92 # Replaces each FIRST (on word boundaries) with <replacement-for-first> like a
93 # simple cpp replacement.
94 simple_cpp() {
95     cmd='my $i = 0;'
96
97     for x; do
98         shift
99
100         if test x"$x" = x--; then
101             break
102         fi
103
104         cmd="$cmd s/\b$x\b/\$ARGV[\$i]/g; \$i++;"
105     done
106
107     perl_line_filter "$cmd" -- "$@"
108 }
109
110 # Print the current OS. The following OS are supported at the moment:
111 #
112 # - Debian (debian)
113 # - Gentoo (gentoo)
114 # - Mac OS X (darwin)
115 # - Solaris/OpenSolaris (sun)
116 # - FreeBSD (freebsd)
117 #
118 # If an unsupported OS is used an error is printed.
119 os() {
120     if test -f /etc/debian_version; then
121         echo debian
122     elif test -f /etc/gentoo-release; then
123         echo gentoo
124     elif test x`uname` = xDarwin; then
125         echo darwin
126     elif test x`uname` = xSunOS; then
127         echo sun
128     elif test x`uname` = xFreeBSD; then
129         echo freebsd
130     else
131         echo 'unsupported OS!' >&2
132         return 1
133     fi
134 }
135
136 # Creates a symbolic link for file $1 in dirname of $2 with name of basename
137 # $2.
138 #
139 # `./link.sh example ~/.examplerc` creates a symbolic link to example
140 # (wherever it is located) in ~/ named .examplerc.
141 link() {
142     local pwd base source target >/dev/null 2>&1 || true
143
144     # Get all necessary paths.
145     pwd=`pwd`
146     base=`printf '%s' "$2" | sed "s|\~|$HOME|"` # expand ~, some sh don't do it
147     base=`dirname "$base"`
148     source=`printf '%s' "$pwd/$1" | sed "s|$base/||"`
149     target=`basename "$2"`
150
151     # Go to the directory where the link is going to be created.
152     cd "$base" || return 1
153
154     # Abort if the target file exists and is no symbolic link. Prevents
155     # overwriting real files.
156     if ( test -f "$target" && test ! -h "$target" ) || \
157             ( test -s "$target" && test ! -h "$target" ); then
158         printf "link(): target '%s' exists already and is no symbolic link!" \
159                "$target" >&2
160         exit 1
161     fi
162
163     # Make sure the source exists (is file, directory or link).
164     if test ! -f "$source" && test ! -d "$source" && test ! -h "$source"; then
165         printf "link(): source '%s' doesn't exist!" "$source" >&2
166         exit 1
167     fi
168
169     # Create the new symbolic link; remove the old one if necessary.
170     printf "link(): linking '%s' to '%s'\n" "$source" "$target"
171     rm -f "$target"
172     ln -s "$source" "$target"
173
174     # Go back to the directory where we were before.
175     cd "$pwd"
176 }
177
178 # Generate a file from a source file using a given command. A warning not to
179 # edit it is automatically added to the created file.
180 #
181 # Usage: generate <file> <extension> <cmd..>
182 #
183 # If an empty extension is provided, the file is modified in-place (through a
184 # temporary file).
185 generate() {
186     local file      >/dev/null 2>&1 || true
187     local file_tmp  >/dev/null 2>&1 || true
188     local extension >/dev/null 2>&1 || true
189
190     # Get command and target file.
191     file="$1"
192     extension="$2"
193     shift
194     shift
195
196     if test -z "$extension"; then
197         file_tmp="$file.tmp"
198     else
199         # We only need this message if we generate a new file.
200         printf "%s: generating from '%s' (%s)\n" \
201             "$file" "$file$extension" "$1"
202
203         echo '###################################'  >"$file"
204         echo '# WARNING! DO NOT EDIT THIS FILE! #' >>"$file"
205         echo '###################################' >>"$file"
206         echo >>"$file"
207         printf "# It was generated from '%s' on %s.\n" \
208             "$file$extension" "`date`" >>"$file"
209         echo >>"$file"
210
211         file_tmp="$file"
212     fi
213
214     # Generate $file from $file$extension using the given command.
215     "$@" <"$file$extension" >>"$file_tmp"
216
217     if test -z "$extension"; then
218         mv "$file_tmp" "$file"
219     fi
220 }