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