epm-sh-functions 5.26 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2 3 4 5
#!/bin/sh
#
# Copyright (C) 2012  Etersoft
# Copyright (C) 2012  Vitaly Lipatov <lav@etersoft.ru>
#
6 7 8
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
Vitaly Lipatov's avatar
Vitaly Lipatov committed
9 10 11 12 13
# (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
14
# GNU Affero General Public License for more details.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
15
#
16 17
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
18 19
#

20 21 22 23 24 25 26 27 28
# copied from /etc/init.d/outformat (ALT Linux)
isatty()
{
	# Set a sane TERM required for tput
	[ -n "$TERM" ] || TERM=dumb
	export TERM
	test -t 1
}

29 30 31
check_tty()
{
	isatty || return
32 33 34 35
	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
36 37 38
	export USETTY=1
}

39 40 41 42
: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}

set_boldcolor()
{
43
	[ "$USETTY" = "1" ] || return
44 45 46 47 48 49 50 51
	{
		echo bold
		echo setaf $1
	} |tput -S
}

restore_color()
{
52
	[ "$USETTY" = "1" ] || return
53 54 55 56 57 58
	{
		echo op; # set Original color Pair.
		echo sgr0; # turn off all special graphics mode (bold in our case).
	} |tput -S
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
59 60 61
echover()
{
    [ -n "$verbose" ] || return
62
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
63 64 65 66 67 68 69 70 71 72 73
}

# Used DISTRNAME
set_target_pkg_env()
{
	[ -n "$DISTRNAME" ] || fatal "Run set_target_pkg_env without DISTRNAME"
	PKGFORMAT=$($DISTRVENDOR -p "$DISTRNAME")
	PKGVENDOR=$($DISTRVENDOR -s "$DISTRNAME")
	RPMVENDOR=$($DISTRVENDOR -n "$DISTRNAME")
}

74 75 76 77 78 79
# for systems without realpath command
realpath()
{
        readlink -f "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
80
# Print command line and run command line
81
showcmd()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
82
{
83 84 85 86 87 88
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
		[ "$UID" = 0 ] && PROMTSIG="#"
		echo " $PROMTSIG $@"
		restore_color
89
	fi >&2
90 91 92 93 94
}

# Print command line and run command line
docmd()
{
95
	showcmd "$@$EXTRA_SHOWDOCMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
96 97 98
	"$@"
}

99
# Run every arg with docmd
100 101 102 103 104 105 106 107 108 109 110
docmd_foreach()
{
	local cmd
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
		docmd $cmd $pkg
	done
}

111
# Print command line and run command line with SUDO
112
sudocmd()
113
{
114 115
	showcmd "$SUDO $@"
	$SUDO "$@"
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129
# Run every arg with sudocmd
sudocmd_foreach()
{
	local cmd
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
		sudocmd $cmd $pkg
	done
}

130 131 132 133 134 135 136 137 138 139 140 141
get_firstarg()
{
	echo -n "$1"
}

get_lastarg()
{
	local lastarg
	eval lastarg=\${$#}
	echo -n "$lastarg"
}

142

Vitaly Lipatov's avatar
Vitaly Lipatov committed
143 144 145 146 147 148 149 150 151 152 153 154 155
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

strip_spaces()
{
        echo "$*" | filter_strip_spaces
}

156 157 158 159 160 161
# param true false
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
store_output()
{
    # use make_temp_file from etersoft-build-utils
    RC_STDOUT=$(mktemp)
    #RC_STDERR=$(mktemp)
    "$@" 2>&1 | tee $RC_STDOUT
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
    return $PIPESTATUS
}

clean_store_output()
{
    rm -f $RC_STDOUT
}

177

178 179 180 181
epm()
{
	$PROGDIR/epm $@
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
182 183 184 185 186

# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
187
		echo "Error: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
188 189
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
190 191 192
	fi
	exit 1
}
193

Vitaly Lipatov's avatar
Vitaly Lipatov committed
194 195 196 197 198 199 200 201 202 203
# Print warning message
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
		echo "Warning: $@" >&2
#	else
#		echog "Error in $0: $@" >&2
	fi
}

204 205
set_sudo()
{
206 207 208 209
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return

210 211
	# set SUDO not for root user
	[ -n "$UID" ] || UID=`id -u`
212 213 214 215 216 217 218 219

	# do not need sudo
	[ $UID = "0" ] && return

	# use sudo if possible
	which sudo >/dev/null 2>/dev/null && SUDO="sudo" && return

	SUDO="fatal 'Can't find sudo. Please install sudo or run epm under root.'"
220 221
}

222 223 224
# print options description from HELPCMD/HELPOPT lines in the code
get_help()
{
225
    grep -v -- "^#" $0 | grep -- "# $1" | while read n ; do
226 227 228 229 230 231
        opt=$(echo $n | sed -e "s|) # $1:.*||g")
        desc=$(echo $n | sed -e "s|.*) # $1:||g")
        printf "    %-20s %s\n" $opt "$desc"
    done
}

232

233 234 235 236
# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
237 238 239

	# Fill for use: PMTYPE, DISTRNAME, DISTRVERSION, PKGFORMAT, PKGVENDOR, RPMVENDOR
	DISTRVENDOR=$PROGDIR/distr_info
240
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
241 242 243
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
	set_target_pkg_env

244 245 246 247 248 249
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

250
case $DISTRNAME in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
251
	ALTLinux|PCLinux)
252 253 254 255
		CMD="apt-rpm"
		#which deepsolver 2>/dev/null >/dev/null && CMD=deepsolver-rpm
		;;
	PCLinux)
256 257
		CMD="apt-rpm"
		;;
258
	Ubuntu|Debian|Mint)
259 260
		CMD="apt-dpkg"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
261
	Mandriva|ROSA)
262 263
		CMD="urpm-rpm"
		;;
264 265
	FreeBSD|NetBSD|OpenBSD|Solaris)
		CMD="pkgsrc"
266
		;;
267 268 269
	Gentoo)
		CMD="emerge"
		;;
270 271 272 273 274 275
	ArchLinux)
		CMD="pacman"
		;;
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific)
		CMD="yum-rpm"
		;;
276
	Slackware)
277
		CMD="slackpkg"
278
		;;
279 280 281
	SUSE|SLED|SLES)
		CMD="zypper-rpm"
		;;
282 283 284
	ForesightLinux|rPathLinux)
		CMD="conary"
		;;
285
	Windows)
286
		CMD="chocolatey"
287
		;;
288 289 290 291 292 293
	MacOS)
		CMD="homebrew"
		;;
	OpenWRT)
		CMD="ipkg"
		;;
294
	*)
295
		fatal "Have no suitable DISTRNAME $DISTRNAME"
296 297 298 299
		;;
esac
PMTYPE=$CMD
}
300