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