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