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