]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - lib.sh
lib.sh: Add sed_i() compatibility function.
[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                 echo "$directory/$1"
45                 return 0
46             fi
47         done
48
49         return 1
50     )
51 }
52
53 # Usage: sed_i ... <file>
54 #
55 # Uses <file>.tmp as temporary file. sed -i is not compatible due to different
56 # implementations.
57 sed_i() {
58     # Get last argument.
59     last=
60     for x; do
61         last="$x"
62     done
63
64     sed "$@" >"$last".tmp
65     mv "$last".tmp "$last"
66 }
67
68 # Print the current OS. The following OS are supported at the moment:
69 # - Debian (debian)
70 # - Gentoo (gentoo)
71 # - Mac OS X (darwin)
72 # - Solaris/OpenSolaris (sun)
73 # - FreeBSD (freebsd)
74 # If an unsupported OS is used an error is printed.
75 os() {
76     if test -f /etc/debian_version; then
77         echo debian
78     elif test -f /etc/gentoo-release; then
79         echo gentoo
80     elif test x`uname` = xDarwin; then
81         echo darwin
82     elif test x`uname` = xSunOS; then
83         echo sun
84     elif test x`uname` = xFreeBSD; then
85         echo freebsd
86     else
87         echo unsupported OS! >&2
88         return 1
89     fi
90 }
91
92 # Creates a symbolic link for file $1 in dirname of $2 with name of basename
93 # $2.
94 #
95 # `./link.sh example ~/.examplerc` creates a symbolic link to example
96 # (wherever it is located) in ~/ named .examplerc.
97 link() {
98     local pwd base source target >/dev/null 2>&1 || true
99
100     # Get all necessary paths.
101     pwd=`pwd`
102     base=`echo "$2" | sed "s|\~|$HOME|"` # expand ~, some sh don't do it
103     base=`dirname "$base"`
104     source=`echo "$pwd/$1" | sed "s|$base/||"`
105     target=`basename "$2"`
106
107     # Go to the directory where the link is going to be created.
108     cd "$base"
109
110     # Abort if the target file exists and is no symbolic link. Prevents
111     # overwriting real files.
112     if ( test -f "$target" && test ! -h "$target" ) || \
113             ( test -s "$target" && test ! -h "$target" ); then
114         echo "link(): target '$target' exists already and is no symbolic link!" >&2
115         exit 1
116     fi
117
118     # Make sure the source exists (is file, directory or link).
119     if test ! -f "$source" && test ! -d "$source" && test ! -h "$source"; then
120         echo "link(): source '$source' doesn't exist!" >&2
121         exit 1
122     fi
123
124     # Create the new symbolic link; remove the old one if necessary.
125     echo "link(): linking '$source' to '$target'"
126     rm -f "$target"
127     ln -s "$source" "$target"
128
129     # Go back to the directory where we were before.
130     cd "$pwd"
131 }
132
133 # Write a warning to $1 to make clear it should not be modified. $2 is the
134 # source for the generated file. Also print a message to stdout that the file
135 # $1 was generated from $2 using the command $3 with options $4.
136 warning() {
137     echo "###################################" > $1
138     echo "# WARNING! DO NOT EDIT THIS FILE! #" >> $1
139     echo "###################################" >> $1
140     echo >> $1
141     echo "# It was generated from $2 on `date`." >> $1
142     echo >> $1
143
144     # Display given options if there were any (Zsh has a problem with $options
145     # as variable name).
146     option=
147     if test -n "$4"; then
148         option=" with options '$4'"
149     fi
150     # Write message to stdout.
151     echo "$3: generating '$1' from '$2'$option"
152
153     unset option
154 }
155
156 # Generate a file using several methods. A warning not to edit it is
157 # automatically added to the created file and a message printed to stdout
158 # through warning().
159 #
160 # The following commands are possible; the file extension for the source file
161 # in brackets.
162 #
163 # - m4   (.m4): pipe $2.m4 through m4 then write it to $2
164 # - awk  (.in): pipe $2.in through awk then write it to $2
165 # - perl (.in): pipe $2.in through perl then write it to $2
166 # - cat  ($3):  copy $2$3 to $2
167 generate() {
168     # Get command and target file.
169     command="$1"
170     file="$2"
171     # Remove arguments from list.
172     shift
173     shift
174
175     # Set extension for the used commands. When cat is used $3 is used as
176     # extension.
177     if test x"$command" = xm4; then
178         extension=.m4
179     elif test x"$command" = xawk -o x"$command" = xperl; then
180         extension=.in
181     elif test x"$command" = xcat; then
182         extension="$1" # is $3 in reality, $1 because of shifting
183         shift
184     # Print a warning and exit if an unsupported command is used.
185     else
186         echo "generate(): command '$command' not supported!" >&2
187         exit 1
188     fi
189
190     # Add warning to file and write a message to stdout.
191     warning "$file" "$file$extension" $command "$*"
192     # Generate $file from $file$extension using the given command.
193     cat "$file$extension" | $command "$@" >> "$file"
194
195     unset command file
196 }