-
Notifications
You must be signed in to change notification settings - Fork 2
/
try-me.sh
executable file
·101 lines (84 loc) · 3.03 KB
/
try-me.sh
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
#!/bin/bash
# Copyright (C) 2018 Whisperity
#
# SPDX-License-Identifier: GPL-3.0
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Create a temporary HOME directory and run a shell there to test envprobe
# without having to mess up the user's real home.
# -----------------------------------------------------------------------------
# (via http://stackoverflow.com/a/14203146)
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
LOAD_ENVPROBE=1
KEEP_TEMPORARY_HOME=0
SHELL_TO_RUN="bash"
while getopts "hkn?s:" opt; do
case "$opt" in
h|\?)
echo "Usage: try-me.sh [OPTION]..."
echo
echo "Start a new shell which has Envprobe enabled, in a *temporary*"
echo "home directory, to allow trying out Envprobe without anything"
echo "affecting the current user's configuration."
echo
echo -e "\t-k\t\tdo not clean up the temporary HOME after execution"
echo -e "\t-n\t\tdry-run: start a shell, but do NOT load 'Envprobe'"
echo -e "\t-s SHELL\tuse the given SHELL (defaults to '${SHELL_TO_RUN}')"
echo -e "\t-h\t\tdisplay this help and exit"
exit 0
;;
k) KEEP_TEMPORARY_HOME=1
;;
n) LOAD_ENVPROBE=0
;;
s) SHELL_TO_RUN="${OPTARG}"
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
# ----------------------------------------------------------------------------
# (via http://stackoverflow.com/a/630387)
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
if [ -z "$MY_PATH" ] ; then
# error; for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # fail
fi
# ----------------------------------------------------------------------------
TEMPHOME=$(mktemp --directory -p "$MY_PATH" ".testhome-XXXXXX")
pushd ${TEMPHOME}
touch \
.${SHELL_TO_RUN}rc \
.is_temporary_home \
.sudo_as_admin_successful
mkdir tmp
if [ $LOAD_ENVPROBE -eq 1 ]
then
echo 'eval "$('"$MY_PATH"'/envprobe config hook '"${SHELL_TO_RUN}"' $$)"' \
>> ./.${SHELL_TO_RUN}rc
fi
popd
env -u XDG_CONFIG_HOME -u XDG_DATA_HOME -u XDG_RUNTIME_DIR \
-u ENVPROBE_SHELL_PID -u ENVPROBE_SHELL_TYPE -u ENVPROBE_CONFIG \
HOME="${TEMPHOME}" TMPDIR="${TEMPHOME}/tmp" \
${SHELL_TO_RUN}
# Cleanup.
if [ $KEEP_TEMPORARY_HOME -ne 1 ]
then
rm -rf "${TEMPHOME}"
fi