-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.sh
169 lines (130 loc) · 3 KB
/
functions.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
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
#!/bin/sh
BIARCH=''
[ "$(epm print info -a)" = "x86_64" ] && BIARCH="i586-"
# copied from /etc/init.d/outformat (ALT Linux)
# FIXME on Android: FIX ME! implement ttyname_r() bionic/libc/bionic/stubs.c:366
inputisatty()
{
# check stdin
#tty -s 2>/dev/null
test -t 0
}
isatty()
{
# check stdout
test -t 1
}
isatty2()
{
# check stderr
test -t 2
}
check_tty()
{
isatty2 || return
# Set a sane TERM required for tput
[ -n "$TERM" ] || TERM=dumb
export TERM
# egrep from busybox may not --color
# egrep from MacOS print help to stderr
if grep -E --help 2>&1 | grep -q -- "--color" ; then
export EGREPCOLOR="--color"
fi
which tput >/dev/null 2>/dev/null || return
# FreeBSD does not support tput -S
echo | tput -S >/dev/null 2>/dev/null || return
[ -z "$USETTY" ] || return
export USETTY=1
}
: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}
set_boldcolor()
{
[ "$USETTY" = "1" ] || return
{
echo bold
echo setaf $1
} |tput -S
}
restore_color()
{
[ "$USETTY" = "1" ] || return
{
echo op; # set Original color Pair.
echo sgr0; # turn off all special graphics mode (bold in our case).
} |tput -S
}
echover()
{
[ -z "$verbose" ] && return
echo "$*" >&2
}
# echo string without EOL
echon()
{
# default /bin/sh on MacOS does not recognize -n
/bin/echo -n "$*"
}
fatal()
{
echo "FATAL: $*" >&2
exit 1
}
info()
{
echo "$*"
}
#epm assure eepm || fatal
# epm remove/install
epmthird()
{
epm --scripts
}
is_root()
{
local EFFUID="$(id -u)"
[ "$EFFUID" = "0" ]
}
# if we have not sudo, returns 1 and set SUDO variable to fatal
SUDO_TESTED=''
SUDO_CMD='sudo'
set_sudo()
{
local nofail="$1"
# cache the result
[ -n "$SUDO_TESTED" ] && return "$SUDO_TESTED"
SUDO_TESTED="0"
SUDO=""
# skip SUDO if disabled
[ -n "$EPMNOSUDO" ] && return
if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
# skip sudo using on Windows
return
fi
# if we are root, do not need sudo
is_root && return
# start error section
SUDO_TESTED="1"
if ! which $SUDO_CMD >/dev/null 2>/dev/null ; then
[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't find sudo. Please install and tune sudo ('# epm install sudo') or run epm under root.'"
return "$SUDO_TESTED"
fi
# if input is a console
if inputisatty && isatty && isatty2 ; then
if ! $SUDO_CMD -l >/dev/null ; then
[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only without password sudo is supported in non interactive using). Please run epm under root.'"
return "$SUDO_TESTED"
fi
else
# use sudo if one is tuned and tuned without password
if ! $SUDO_CMD -l -n >/dev/null 2>/dev/null ; then
[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only without password sudo is supported). Please run epm under root.'"
return "$SUDO_TESTED"
fi
fi
SUDO_TESTED="0"
SUDO="$SUDO_CMD --"
# check for < 1.7 version which do not support -- (and --help possible too)
$SUDO_CMD -h 2>/dev/null | grep -q " --" || SUDO="$SUDO_CMD"
}
check_tty
set_sudo