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