]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - lib.sh
lib.sh: Add Gentoo support to os().
[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 # Get path to m4 because it's later redefined as function.
11 m4=`which m4`
12
13
14 # Check if the given program is installed. Returns 0 if it exists, 1
15 # otherwise; so it can be used in if.
16 installed() {
17     which $1 | grep -E '^/' > /dev/null
18 }
19
20 # Prints the current OS. Supported are Debian (debian), Gentoo (gentoo) and
21 # Mac OS X (darwin) at the moment. If an unsupported OS is used an error is
22 # printed.
23 os() {
24     if [ -f /etc/debian_version ]; then
25         echo debian
26     elif [ -f /etc/gentoo-release ]; then
27         echo gentoo
28     elif [ x`uname` = xDarwin ]; then
29         echo darwin
30     else
31         echo unsupported OS! >&2
32         return 1
33     fi
34 }
35
36 # Creates a symbolic link for file $1 in dirname of $2 with name of basenmae
37 # $2.
38 #
39 # `./link.sh example ~/.examplerc` creates a symbolic link to example
40 # (wherever it is located) in ~/ named .examplerc.
41 link() {
42     # Get all necessary paths.
43     pwd=`pwd`
44     base=`dirname "$2"`
45     source=`echo -n "$pwd/$1" | sed "s|$base/||"`
46     target=`basename "$2"`
47
48     # Go to the directory where the link is going to be created.
49     cd "$base"
50
51     # Abort if the target file exists and is no symbolic link. Prevents
52     # overwriting real files.
53     if [ -e "$target" -a ! -h "$target" ]; then
54         echo "link(): target '$target' exists already and is no symbolic link!" >&2
55         exit 1
56     fi
57
58     # Make sure the source exists.
59     if [ ! -e "$source" ]; then
60         echo "link(): source '$source' doesn't exist!" >&2
61         exit 1
62     fi
63
64     # Create the new symbolic link; remove the old one if necessary.
65     echo "link(): linking '$source' to '$target'"
66     rm -f "$target"
67     ln -s "$source" "$target"
68
69     # Go back to the directory where we were before.
70     cd "$pwd"
71
72     unset pwd base source target
73 }
74
75 # m4 wrapper which uses $1.m4 as template file, feeds it to m4 and writes it
76 # to $1 with a warning at the beginning to not edit the generated file.
77 #
78 # All arguments (except the first which is the filename) are passed to m4.
79 #
80 # The following macros are defined: IF and FI. Example:
81 #     IF(OS, debian)
82 #         ...
83 #     FI
84 m4() {
85     # First argument is file name.
86     file=$1
87     shift
88
89     # Write a warning to the generated file.
90     echo "###################################" > $file
91     echo "# WARNING! DO NOT EDIT THIS FILE! #" >> $file
92     echo "###################################" >> $file
93     echo >> $file
94     echo "# It was generated from $file.m4 on `date`." >> $file
95     echo >> $file
96
97     # Process $1.m4 with m4 using the given options.
98     echo "m4(): generating '$file' from '$file.m4' with options '$*'"
99     # Add useful macros.
100     (echo "define(\`IF', \`ifelse(\`\$1', \`\$2',dnl')dnl
101 define(\`FI', \`)dnl')dnl";
102         # Run the file (and the default macros) through m4.
103         cat $file.m4) | $m4 $* >> $file
104
105     unset file
106 }