forked from josephwecker/bashrc_dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc_dispatch
executable file
·109 lines (84 loc) · 3.12 KB
/
bashrc_dispatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Launch different bash configuration for Linux vs OSX, interactive vs batch
#
# More info at https://github.com/gaelicWizard/bashrc_dispatch
#
# License: Public Domain.
# Configuration
# -------------
#
# BASHRC_DISPATCH_EXPORT_FUNCTIONS: export SHELL_PLATFORM and shell_is_* functions for use in other scripts.
# BASHRC_DISPATCH_PREFIX: set location for scripts to import
: "${BASHRC_DISPATCH_EXPORT_FUNCTIONS:=true}"
: "${BASHRC_DISPATCH_PREFIX:="${XDG_CONFIG_HOME:-$HOME/.config}/bash/"}"
# Code
# ----
# Avoid recursive invocation
[[ -n "${BASHRC_DISPATCH_PID:-}" && $$ -eq "$BASHRC_DISPATCH_PID" ]] && return
BASHRC_DISPATCH_PID=$$
# Setup the main shell variables and functions
if [[ "${SHELL_PLATFORM:=OTHER}" == "OTHER" ]]
then
case "$OSTYPE" in
*'linux'* ) SHELL_PLATFORM='LINUX' ;;
*'darwin'* ) SHELL_PLATFORM='OSX' ;;
*'freebsd'* ) SHELL_PLATFORM='BSD' ;;
*'cygwin'* ) SHELL_PLATFORM='CYGWIN' ;;
esac
fi
if ! type -p shell_is_login
then
shell_is_linux () { false ; }
shell_is_osx () { false ; }
shell_is_bsd () { false ; }
shell_is_cygwin () { false ; }
shell_is_login () { shopt -q login_shell ; }
shell_is_interactive () { [[ "$-" == *'i'* ]] ; }
shell_is_script () { [[ "$-" != *'i'* ]] ; }
case "$OSTYPE" in
*'linux'* ) shell_is_linux () { true ; } ;;
*'darwin'* ) shell_is_osx () { true ; } ;;
*'freebsd'* ) shell_is_bsd () { true ; } ;;
*'cygwin'* ) shell_is_cygwin () { true ; } ;;
esac
fi
# Make $BASH_ENV the same in interactive and non-interactive scripts
: ${BASH_ENV:=$BASH_SOURCE}
export BASH_ENV
# Make these available to the potentially convoluted bashrc_* startup scripts
if ${BASHRC_DISPATCH_EXPORT_FUNCTIONS:=true}
then
typeset -rx SHELL_PLATFORM
typeset -frx shell_is_linux
typeset -frx shell_is_osx
typeset -frx shell_is_bsd
typeset -frx shell_is_cygwin
typeset -frx shell_is_login
typeset -frx shell_is_interactive
typeset -frx shell_is_script
fi
# Now dispatch special files
if [[ -f "${BASHRC_DISPATCH_PREFIX}bashrc_once" && -z "${BRCD_RANONCE:-}" ]]
then
export BRCD_RANONCE=true
. "${BASHRC_DISPATCH_PREFIX}bashrc_once"
fi
[[ -f "${BASHRC_DISPATCH_PREFIX}bashrc_all" ]] && . "${BASHRC_DISPATCH_PREFIX}bashrc_all"
[[ -f "${BASHRC_DISPATCH_PREFIX}bashrc_script" ]] && shell_is_script && . "${BASHRC_DISPATCH_PREFIX}bashrc_script"
[[ -f "${BASHRC_DISPATCH_PREFIX}bashrc_interactive" ]] && shell_is_interactive && . "${BASHRC_DISPATCH_PREFIX}bashrc_interactive"
[[ -f "${BASHRC_DISPATCH_PREFIX}bashrc_login" ]] && shell_is_login && . "${BASHRC_DISPATCH_PREFIX}bashrc_login"
# Unset variables if necessary to avoid env polution
if ! $BASHRC_DISPATCH_EXPORT_FUNCTIONS
then
unset SHELL_PLATFORM
unset -f shell_is_linux
unset -f shell_is_osx
unset -f shell_is_bsd
unset -f shell_is_cygwin
unset -f shell_is_login
unset -f shell_is_interactive
unset -f shell_is_script
fi
# Unset local variables
unset BASHRC_DISPATCH_EXPORT_FUNCTIONS
unset BASHRC_DISPATCH_PREFIX