6780da6b0717979d282be522c1cba33a44d02eca
[distro-setup] / .bashrc
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 # to debug
6 #set -x
7 # redirect output to log file. this doesn't work. todo figure out why
8 #exec 1>>/a/tmp/bashlog
9 #exec 2>>/a/tmp/bashlog
10
11
12 # By default this file is sourced for ALL ssh commands. This is wonky.
13 # Normally, this file is not sourced when a script is run, but we can
14 # override that by having #!/bin/bash -l. I want something similar for ssh
15 # commands. when a local script runs an ssh command, this file should not be
16 # sourced by default, but we should be able to override that.
17 #
18 # So here we test for conditions of a script under ssh and return if so.
19 # And we don't keep the rest of the code in this file, because even
20 # though we return, we already parsed the whole code, and as I develop
21 # the code, the parsing can have errors, which can screw up cronjobs
22 # etc.
23 #
24 # To test for an overriding condition, we have a few options. one is to
25 # use an environment variable. env variables sent across ssh are
26 # strictly limited. ssh -t which sets $SSH_TTY, but within a script that
27 # won't work because tty allocation will fail. However, I do use -t for
28 # strange hosts, so we consider it an indicator. We could override an
29 # obscure unused LC_var, like telephone, but I don't want to run into
30 # some edge case where that messes things up. we could transfer a file
31 # which we could test for, but I can't think of a way to make that
32 # inherently limited to a single ssh command. I choose to set SendEnv
33 # and AcceptEnv ssh config vars to allow the environment variable
34 # BASH_LOGIN_SHELL to propagate across ssh. This also requires that we
35 # wrap ssh in interactive shells, because, once we export the var, it
36 # will go into scripts, and we want it to be nondefault there.
37 #
38 # -c is set whenever a command is passed to ssh
39 # -i is set whenever a command is not passed
40 if [[ $SSH_CONNECTION ]] \
41 && [[ $- == *c* ]] \
42 && [[ $- != *i* ]] \
43 && { [[ ! $SSH_TTY ]] || [[ $BASH_LOGIN_SHELL == false ]] ; } ; then
44 return 0
45 else
46
47 # the distinction between login and non-login shells is lame,
48 # get rid of it. note ssh shells normally its login if a command is passed
49 if ! shopt -q login_shell; then
50 if [[ -r /etc/profile ]]; then
51 source /etc/profile
52 fi
53 # note, this is not exactly the same as a login shell, because that
54 # reads ~/.bash_profile or alternative, which usually just sources
55 # this file, and we don't want to do that and cause an infinite
56 # loop.
57 fi
58 _x=$(readlink -f ${BASH_SOURCE[0]})
59 _x=${_x%/*}/brc
60 if [[ -r $_x ]]; then
61 # shellcheck source=./brc
62 source $_x
63 fi
64 # brc2 is for things i dont necessarily want on every system
65 _x=${_x%/*}/brc2
66 if [[ -r $_x ]]; then
67 # shellcheck source=./brc2
68 source $_x
69 fi
70 fi
71 # ensure no bad programs appending to this file will have an affect
72 return 0