1 # Setup functions and settings used in subdirectories.
3 # Their setup.sh script sources this file.
6 # csh gives the error "Unknown colorls variable `su'." when used with newer
7 # options supported by Zsh or GNU ls.
11 # Check if the given program is installed. Returns 0 if it exists, 1
12 # otherwise; so it can be used in if.
14 # Thanks to Rado in #awk on Freenode (2010-03-29 01:34) for the END tip to
15 # also work with empty stdin, and to prince_jammys in #awk on Freenode
16 # (2010-03-29 02:20) for a shorter syntax for "$0 !~".
17 which $1 2>&1 | awk '{ if (!/^\//) exit 1 }
18 END { if (NR == 0) exit 1 }'
21 # Print the current OS. The following OS are supported at the moment:
25 # - Solaris/OpenSolaris (sun)
26 # If an unsupported OS is used an error is printed.
28 if [ -f /etc/debian_version ]; then
30 elif [ -f /etc/gentoo-release ]; then
32 elif [ x`uname` = xDarwin ]; then
34 elif [ x`uname` = xSunOS ]; then
37 echo unsupported OS! >&2
42 # Creates a symbolic link for file $1 in dirname of $2 with name of basename
45 # `./link.sh example ~/.examplerc` creates a symbolic link to example
46 # (wherever it is located) in ~/ named .examplerc.
48 # Get all necessary paths.
50 base=`echo "$2" | sed "s|\~|$HOME|"` # expand ~, some sh don't do it
51 base=`dirname "$base"`
52 source=`echo "$pwd/$1" | sed "s|$base/||"`
53 target=`basename "$2"`
55 # Go to the directory where the link is going to be created.
58 # Abort if the target file exists and is no symbolic link. Prevents
59 # overwriting real files.
60 if [ \( -f "$target" -a ! -h "$target" \) -o \
61 \( -s "$target" -a ! -h "$target" \) ]; then
62 echo "link(): target '$target' exists already and is no symbolic link!" >&2
66 # Make sure the source exists (is file, directory or link).
67 if [ ! -f "$source" -a ! -d "$source" -a ! -h "$source" ]; then
68 echo "link(): source '$source' doesn't exist!" >&2
72 # Create the new symbolic link; remove the old one if necessary.
73 echo "link(): linking '$source' to '$target'"
75 ln -s "$source" "$target"
77 # Go back to the directory where we were before.
80 unset pwd base source target
83 # Write a warning to $1 to make clear it should not be modified. $2 is the
84 # source for the generated file. Also print a message to stdout that the file
85 # $1 was generated from $2 using the command $3 with options $4.
87 echo "###################################" > $1
88 echo "# WARNING! DO NOT EDIT THIS FILE! #" >> $1
89 echo "###################################" >> $1
91 echo "# It was generated from $2 on `date`." >> $1
94 # Display given options if there were any (Zsh has a problem with $options
98 option=" with options '$4'"
100 # Write message to stdout.
101 echo "$3: generating '$1' from '$2'$option"
106 # Generate a file using several methods. A warning not to edit it is
107 # automatically added to the created file and a message printed to stdout
110 # The following commands are possible; the file extension for the source file
113 # - m4 (.m4): pipe $2.m4 through m4 then write it to $2
114 # - awk (.in): pipe $2.in through awk then write it to $2
115 # - perl (.in): pipe $2.in through perl then write it to $2
116 # - cat ($3): copy $2$3 to $2
118 # Get command and target file.
121 # Remove arguments from list.
125 # Set extension for the used commands. When cat is used $3 is used as
127 if [ x"$command" = xm4 ]; then
129 elif [ x"$command" = xawk -o x"$command" = xperl ]; then
131 elif [ x"$command" = xcat ]; then
132 extension="$1" # is $3 in reality, $1 because of shifting
134 # Print a warning and exit if an unsupported command is used.
136 echo "generate(): command '$command' not supported!" >&2
140 # Add warning to file and write a message to stdout.
141 warning "$file" "$file$extension" $command "$*"
142 # Generate $file from $file$extension using the given command.
143 cat "$file$extension" | $command "$@" >> "$file"