1 # Shell functions useful to all shells.
4 # Helper function to print debug information if $DEBUG is not empty.
6 # Doesn't fit perfectly in this file, but this is the best place to make it
7 # available everywhere.
9 if [ x$DEBUG != x ]; then
14 # Sources a configuration file if it exists; loads a fallback .local file if
15 # it doesn't. If the .local files doesn't exist nothing is sourced.
17 # $1: Path to directory where configuration files are located.
18 # $2: Directory name in $1 where the non .local files are stored, can be
19 # empty. If empty both configuration files (normal and .local) are stored
21 # $3: Base name of file in $2, for example "rc" or "env".
22 # $4: Extension for $3, if this file doesn't exist "$1/$3.local" is sourced.
23 # Can be empty, then no extension is used.
24 # $5: Additional options, set to nolocal to prevent loading of "$1/$3.local"
25 # if "$1/$2/$3.$4" doesn't exist.
27 # Example with the following directory/file structure, $os is Darwin and
28 # $hostname is marvin.
35 # ~/.zsh/host/rc.marvin
38 # To load additional rc files from within ~/.zsh/rc use the following:
40 # source_config ~/.zsh os rc $os # loads os/rc.Darwin
41 # source_config ~/.zsh host rc $hostname # loads host/rc.marvin
43 # To load additional env files from within ~/.zsh/env use the following (note
44 # nolocal to prevent loading env.local two times if os and host files don't
47 # source_config zsh os env $os nolocal # loads os/rc.Darwin
48 # source_config zsh host env $hostname # loads env.local
50 # By letting $4 empty normal configuration files can be sourced. A .local can
53 # source_config zsh "" file # loads zsh/file if it exists
55 # Doesn't fit perfectly in this file, but this is the best place to make it
56 # available everywhere.
58 # If DEBUG is set to a non empty value additional debug output is printed.
60 # Path to the file to source and its local counterpart.
62 source_file=$1/$2/$3.$4
63 # If $4 is empty don't append a trailing dot. This allows source_config()
64 # to load normal configuration files.
68 source_file_local=$1/$3.local
70 # Additional debug output.
71 source_debug "source_config(): checking if $source_file exists"
72 source_debug "source_config(): checking if $source_file_local exists"
74 # If the file does exist then source it.
75 if [ -f $source_file ]; then
76 source_debug "source_config(): -> sourcing $source_file"
79 # Otherwise load the .local file if it exists and .local files are
81 elif [ -f $source_file_local -a x$5 != xnolocal ]; then
82 source_debug "source_config(): -> sourcing $source_file_local"
85 source_debug "source_config(): -> neither exists"
88 unset source_file source_file_local