-
Notifications
You must be signed in to change notification settings - Fork 0
/
.profile
executable file
·243 lines (191 loc) · 8.17 KB
/
.profile
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# --------------------
# beginning indication
# --------------------
printf "Beginning execution of \"${HOME}/.profile\"\n\n"
# -------------------------------------------
# print shell version and license information
# -------------------------------------------
printf "$(${SHELL} --version)\n\n"
# ----------------------
# BEGIN Set permissions.
# ----------------------
# TODO: Find out whether it is possible to run this command only when the user has permission to do so. Currently it appears not to run on The University of Adelaide systems.
# chmod u=rwx -R "${HOME}"
# --------------------
# END Set permissions.
# --------------------
# --------------------------------
# BEGIN Set environment variables.
# --------------------------------
# set the default text editor
export EDITOR="vim"
export VISUAL="${EDITOR}"
export GIT_EDITOR="${EDITOR}"
# ------------------------------
# END Set environment variables.
# ------------------------------
# --------------------------
# BEGIN Set prompt variable.
# --------------------------
# Set variable identifying the chroot you work in (used in the prompt below).
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]
then
debian_chroot="$(cat /etc/debian_chroot)"
fi
# If the terminal supports colour.
if [ -x /usr/bin/tput ] && tput setaf 1 > /dev/null 2>&1
then
# assume color support is compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
PS1_CALCULATE_PROMPT()
{
# get exit code of command prior to PS1 prompt
_exit_code=${?}
# last output no newline fix, with additional newline.
printf "%$(($(tput cols)-1))s\r\n"
# line 1, user info
# -----------------
printf "\e[7m "
if [ ! -z "${SSH_CLIENT}" ]
then
printf "${SSH_CLIENT} \xe2\x87\x94 "
fi
printf "${debian_chroot:+($debian_chroot)}"
# user@hostname (PS1 equivelent: \u@\H)
printf "$(whoami)@$(hostname) "
printf "\e[0m "
# shell name
# TODO: prepend shell version
printf "\e[7m $(ps -p $$ -oargs=) \e[0m "
# time and date
printf "\e[7m $(date +%H:%M:%S) \e[0m "
printf "\e[7m $(date +%d/%m/%Y) \e[0m "
# exit code of command prior to prompt
if [ "${_exit_code}" != "0" ]
then
printf "\e[0;31m\e[7m ${_exit_code} \e[0m "
else
printf "\e[0;32m\e[7m ${_exit_code} \e[0m "
fi
printf ">\n"
# line 2, working dir
# -------------------
printf "\033[01;34m\e[96m\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 $(pwd)\033[00m"
printf "\n"
# line 3 (potentially), repository information
# --------------------------------------------
_git_branch_name="$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"
_git_origin_name="$(git config --get remote.origin.url)"
# if the current dir is a git repository
if [ ! -z "${_git_branch_name}" ]
then
printf "\e[96m\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ${_git_origin_name}\e[0m\n"
printf "\e[96m\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ${_git_branch_name}\e[0m\n"
# TODO: \x01\xF5\xA7
fi
# update the values of LINES and COLUMNS regardless of whether the terminal has been physically resized
# ref: https://www.mankier.com/1/resize
printf "$(resize > /dev/null 2>&1)"
# TODO: support svn and other remotes
# TODO: colourise user@hostname and neighbouring sections
# TODO: support non-colour terminals, and have no-colour variable optionally skip colour, use same name as default bashrc, this variable could be set manually before resourceing the bashrc, or set my the process invoking bash
# TODO: consider adding bash job count, ie background jobs / subproccesses running
# TODO: consider adding current repo revision number, or information relative either to the repo, or the dir in the repo
}
PS1="\$(PS1_CALCULATE_PROMPT)\n\n\[\e[34m\]$\[\e[0m\] "
else
PS1="\n${debian_chroot:+($debian_chroot)}\u@\h:\n\w\$(resize > /dev/null 2>&1)\n\n$ "
fi
# if this is an xterm set the title to user@host:dir
case "$TERM"
in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]${PS1}"
;;
esac
# ------------------------
# END Set prompt variable.
# ------------------------
# ------------------------------------------
# BEGIN Sourcing shell-specific *rc scripts.
# ------------------------------------------
# If running bash, source the users `.bashrc` if it exists.
[ -n "${BASH_VERSION}" ] && [ -f "${HOME}/.bashrc" ] && . "${HOME}/.bashrc"
# ----------------------------------------
# END Sourcing shell-specific *rc scripts.
# ----------------------------------------
# -------------------------------------
# BEGIN Sourcing host-specific scripts.
# -------------------------------------
# TODO: Consider `.sh` extensions.
if [ -f "${HOME}/.profile-hosts/$(hostname)" ]
then
printf "Executing host-specific profile script ...\n"
. "${HOME}/.profile-hosts/$(hostname)"
else
printf "Host-specific profile shell script not found,\n"
printf "Creating shell script \`${HOME}/.profile-hosts/$(hostname)\` ...\n"
cp "${HOME}/.profile-hosts/template.txt" "${HOME}/.profile-hosts/$(hostname)"
fi
# -----------------------------------
# END Sourcing host-specific scripts.
# -----------------------------------
# ---------------------------
# BEGIN User script sourcing.
# ---------------------------
printf "Looking for ${HOME}/cmd/*/index.sh\n"
printf "Function \"*\" executes contained script \`index.sh\`.\n\n"
printf "Executable function commands:\n\n"
for _dir in $(cd "${HOME}/cmd/" && echo ./* | sed -E "s/\.\///g")
do
if [ -d "${HOME}/cmd/${_dir}" ]
then
eval "${_dir}() { . \"${HOME}/cmd/${_dir}/index.sh\" ; }" \
&& printf "${_dir}\n"
fi
done
# -------------------------
# END User script sourcing.
# -------------------------
# -------------------------------
# BEGIN Setting `PATH` variables.
# -------------------------------
# TODO: consider checking for the dirs existance per dir, consider edge cases, and other dynamic factors of influence
export PATH="${PATH}" \
# include user bin if the dir exists
"$([ -d "${HOME}/bin" ] && printf ":${HOME}/bin")" \
"$([ -d "${HOME}/.local/bin" ] && printf ":${HOME}/.local/bin")" \
# metabox laptop : wsl ubuntu
":${HOME}/uni/nand2tetris/tools" \
# CAT suite : redhat linux
# note: CAT suite: /usr/local/nand2tetris/tools:/usr/java/jdk1.8.0_181-amd64/bin
":/usr/local/nand2tetris/tools" \
":/usr/java/jdk1.8.0_181-amd64/bin" \
# homebrew/linuxbrew
":/home/linuxbrew/.linuxbrew/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" \
# include user dynamically linked libs if the dir exists
"$([ -d "${HOME}/.local/lib" ] && printf ":${HOME}/.local/lib")"
export MANPATH="${MANPATH}" \
# homebrew/linuxbrew
":/home/linuxbrew/.linuxbrew/share/man"
export INFOPATH="${INFOPATH}" \
# homebrew/linuxbrew
":/home/linuxbrew/.linuxbrew/share/info"
# TODO: dynamically check host names from script names under, for example, `./user-hosts`, and move mechine specific or unique logic there. This folders contents should be fully encrypted, either including the file name, of me move entirely, which is cleaner. Consider the case in which the host name might change, and how that case should be handled, whether by a method to asertain the name, or manual user input. A default script should run if the mechine does not match a file, in which a file should be created along with any other possible actions.
if [ "$(hostname)" = "DESKTOP-DCHFQSJ" ]
then
emacs()
{
"/mnt/e/d/User/emacs-26.2-x86_64/runemacs.exe"
}
fi
# -----------------------------
# END Setting `PATH` variables.
# -----------------------------