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