]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - lib.sh
Add installed() function to check if a program exists.
[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 > /dev/null
18 }
19
20 # Creates a symbolic link for file $1 in dirname of $2 with name of basenmae
21 # $2.
22 #
23 # `./link.sh example ~/.examplerc` creates a symbolic link to example
24 # (wherever it is located) in ~/ named .examplerc.
25 link() {
26     # Get all necessary paths.
27     pwd=`pwd`
28     base=`dirname "$2"`
29     source=`echo -n "$pwd/$1" | sed "s|$base/||"`
30     target=`basename "$2"`
31
32     # Go to the directory where the link is going to be created.
33     cd "$base"
34
35     # Abort if the target file exists and is no symbolic link. Prevents
36     # overwriting real files.
37     if [ -e "$target" -a ! -h "$target" ]; then
38         echo "link(): target '$target' exists already and is no symbolic link!" >&2
39         exit 1
40     fi
41
42     # Make sure the source exists.
43     if [ ! -e "$source" ]; then
44         echo "link(): source '$source' doesn't exist!" >&2
45         exit 1
46     fi
47
48     # Create the new symbolic link; remove the old one if necessary.
49     echo "link(): linking '$source' to '$target'"
50     rm -f "$target"
51     ln -s "$source" "$target"
52
53     # Go back to the directory where we were before.
54     cd "$pwd"
55
56     unset pwd base source target
57 }
58
59 # m4 wrapper which uses $1.m4 as template file, feeds it to m4 and writes it
60 # to $1 with a warning at the beginning to not edit the generated file.
61 #
62 # All arguments (except the first which is the filename) are passed to m4.
63 m4() {
64     # First argument is file name.
65     file=$1
66     shift
67
68     # Write a warning to the generated file.
69     echo "###################################" > $file
70     echo "# WARNING! DO NOT EDIT THIS FILE! #" >> $file
71     echo "###################################" >> $file
72     echo >> $file
73     echo "# It was generated from $file.m4 on `date`." >> $file
74     echo >> $file
75
76     # Process $1.m4 with m4 using the given options.
77     echo "m4(): generating '$file' from '$file.m4' with options '$*'"
78     $m4 $* $file.m4 >> $file
79
80     unset file
81 }