-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsu-helper
executable file
·92 lines (79 loc) · 1.91 KB
/
dsu-helper
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
#!/bin/bash
#
# Setup environment after sudo/su.
#
# Set usage output
USAGE="[-h |--help] [-s <scripts-dir> | --scripts-dir=<scripts-dir>] [-t <tag> | --tag=<tag>] [-w <workspace-dir> | --workspace-dir=<workspace-dir>] <homedir>"
LONGUSAGE="\t-h, --help\n\t\tPrint this help message
\t-s <scripts-dir>, --scripts-dir=<scripts-dir>\n\t\tScripts directory (default: HOME/.scripts)
\t-t <tag>, --tag=<tag>\n\t\tSession tag (default: none)
\t-w <workspace-dir>, --workspace-dir=<workspace-dir>\n\t\tWorkspace directory (default: none)
\t<homedir>\n\t\tPath to home directory"
die() {
echo "Failed: $@"
exit 1
}
usage() {
local myusage;
if [ -n "${USAGE}" ]; then
myusage=${USAGE}
else
myusage="No usage given"
fi
if [ -n "$1" ]; then
echo "$@"
fi
echo ""
echo "Usage:"
echo "`basename $0` ${myusage}"
if [ -n "${LONGUSAGE}" ]; then
echo -e "${LONGUSAGE}"
fi
exit 1
}
# Script name
ME=$(basename $0)
# Parse arguments
ARGS=`getopt -o hs:t:w: --long help,scripts-dir:,tag:,workspace-dir: -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-s|--scripts-dir) SCRIPTS=$2 ; shift 2 ;;
-t|--tag) DSU_TAG=$2 ; shift 2 ;;
-w|--workspace-dir) DSU_WSDIR=$2 ; shift 2 ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
# Remaining arguments are in $1, $2, etc. as normal
HOME=${1}
if [ ! -d "${HOME}" ]; then
usage "Must give homedir"
fi
if [ -z "${SCRIPTS}" ]; then
SCRIPTS="${HOME}/.scripts"
fi
if [ ! -d "${SCRIPTS}" ]; then
usage "Scripts dir ${SCRIPTS} doesn'd exist"
fi
. "${SCRIPTS}/bashrc.d/env/flavor"
case "${FLAVOR}" in
fedora) __bashargs="-l" ;;
*) __bashargs="" ;;
esac
export HOME
export SCRIPTS
if [ -n "${DSU_WSDIR}" ]; then
if [ ! -d "${DSU_WSDIR}" ]; then
usage "Workspace dir ${DSU_WSDIR} doesn'd exist"
fi
export DSU_WSDIR
fi
if [ -n "${DSU_TAG}" ]; then
export DSU_TAG
fi
exec bash ${__bashargs}