epm-sh-functions 6.46 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
# copied from /etc/init.d/outformat (ALT Linux)
21

22 23 24 25 26 27
inputisatty()
{
	# check stdin
	tty -s
}

28 29
isatty()
{
30 31 32 33 34 35
	# check stdout
	test -t 1
}

isatty2()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
36 37
	# check stderr
	test -t 2
38 39
}

40 41
check_tty()
{
42 43 44 45 46 47
	isatty2 || return

	# Set a sane TERM required for tput
	[ -n "$TERM" ] || TERM=dumb
	export TERM

48 49 50 51
	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
52 53 54
	export USETTY=1
}

55 56 57 58
: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}

set_boldcolor()
{
59
	[ "$USETTY" = "1" ] || return
60 61 62 63 64 65 66 67
	{
		echo bold
		echo setaf $1
	} |tput -S
}

restore_color()
{
68
	[ "$USETTY" = "1" ] || return
69 70 71 72 73 74
	{
		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
75 76 77
echover()
{
    [ -n "$verbose" ] || return
78
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
79 80 81 82 83 84 85 86 87 88 89
}

# 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")
}

90 91 92 93 94 95
# for systems without realpath command
realpath()
{
        readlink -f "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
96
# Print command line and run command line
97
showcmd()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
98
{
99 100 101
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
102
		[ "$EFFUID" = 0 ] && PROMTSIG="#"
103 104
		echo " $PROMTSIG $@"
		restore_color
105
	fi >&2
106 107 108 109 110
}

# Print command line and run command line
docmd()
{
111
	showcmd "$@$EXTRA_SHOWDOCMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
112 113 114
	"$@"
}

115
# Run every arg with docmd
116 117 118 119 120 121 122 123 124 125 126
docmd_foreach()
{
	local cmd
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
		docmd $cmd $pkg
	done
}

127
# Print command line and run command line with SUDO
128
sudocmd()
129
{
130 131
	showcmd "$SUDO $@"
	$SUDO "$@"
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145
# Run every arg with sudocmd
sudocmd_foreach()
{
	local cmd
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
		sudocmd $cmd $pkg
	done
}

146 147 148 149 150 151 152 153 154 155 156 157
get_firstarg()
{
	echo -n "$1"
}

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

158

Vitaly Lipatov's avatar
Vitaly Lipatov committed
159 160 161 162 163 164 165 166 167 168 169 170 171
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

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

172 173 174 175 176 177
# param true false
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

178 179 180 181
store_output()
{
    # use make_temp_file from etersoft-build-utils
    RC_STDOUT=$(mktemp)
182 183
    local CMDSTATUS=$RC_STDOUT.pipestatus
    echo 1 >$CMDSTATUS
184
    #RC_STDERR=$(mktemp)
185 186 187
    ( "$@" 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
    return $(cat $CMDSTATUS)
    # bashism
188
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
189
    #return $PIPESTATUS
190 191 192 193
}

clean_store_output()
{
194
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
195 196
}

197

198 199 200 201
epm()
{
	$PROGDIR/epm $@
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
202 203 204 205 206

# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
207
		echo "Error: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
208 209
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
210 211 212
	fi
	exit 1
}
213

Vitaly Lipatov's avatar
Vitaly Lipatov committed
214 215 216 217 218 219 220 221 222 223
# Print warning message
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
		echo "Warning: $@" >&2
#	else
#		echog "Error in $0: $@" >&2
	fi
}

224 225
set_sudo()
{
226 227 228 229
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return

230
	EFFUID=`id -u`
231 232

	# do not need sudo
233
	[ $EFFUID = "0" ] && return
234 235 236 237 238

	# 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.'"
239 240
}

241 242 243 244 245 246 247
set_eatmydata()
{
	# skip if disabled
	[ -n "$EPMNOEATMYDATA" ] && return
	# use if possible
	which eatmydata >/dev/null 2>/dev/null || return
	SUDO="$SUDO eatmydata"
248
	isatty && echo "Uwaga! eatmydata is installed, we will use it for disable all sync operations." >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
249
	return 0
250 251
}

252 253 254 255 256
assure_exists()
{
	PATH=$PATH:/sbin:/usr/sbin which $1 2>/dev/null >/dev/null && return
	echo "Install appropriate package for $1 command..."
	case $1 in
257
		equery|revdep-rebuild)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
			epm install gentoolkit
			;;
		apt-repo)
			epm install apt-repo
			;;
		apt-file)
			epm install apt-file
			;;
		update-kernel|remove-old-kernels)
			epm install update-kernel
			;;
		*)
			fatal "Internal error: Unknown binary $1 to check if exists"
			;;
	esac
}

275 276 277 278
# improve
get_package_type()
{
	local i
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	case $1 in
		*.deb)
			echo "deb"
			return
			;;
		*.rpm)
			echo "rpm"
			return
			;;
		*)
			#fatal "Don't know type of $1"
			# return package name for info
			echo "$1"
			return 1
			;;
	esac
295 296 297
}


298 299 300
# print options description from HELPCMD/HELPOPT lines in the code
get_help()
{
301
    grep -v -- "^#" $0 | grep -- "# $1" | while read n ; do
302 303 304 305 306 307
        opt=$(echo $n | sed -e "s|) # $1:.*||g")
        desc=$(echo $n | sed -e "s|.*) # $1:||g")
        printf "    %-20s %s\n" $opt "$desc"
    done
}

308

309 310 311 312
# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
313 314 315

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

320 321 322 323 324 325
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

326
case $DISTRNAME in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
327
	ALTLinux|PCLinux)
328 329 330 331
		CMD="apt-rpm"
		#which deepsolver 2>/dev/null >/dev/null && CMD=deepsolver-rpm
		;;
	PCLinux)
332 333
		CMD="apt-rpm"
		;;
334
	Ubuntu|Debian|Mint)
335 336
		CMD="apt-dpkg"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
337
	Mandriva|ROSA)
338 339
		CMD="urpm-rpm"
		;;
340 341
	FreeBSD|NetBSD|OpenBSD|Solaris)
		CMD="pkgsrc"
342
		;;
343 344 345
	Gentoo)
		CMD="emerge"
		;;
346 347 348 349 350 351
	ArchLinux)
		CMD="pacman"
		;;
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific)
		CMD="yum-rpm"
		;;
352
	Slackware)
353
		CMD="slackpkg"
354
		;;
355 356 357
	SUSE|SLED|SLES)
		CMD="zypper-rpm"
		;;
358 359 360
	ForesightLinux|rPathLinux)
		CMD="conary"
		;;
361
	Windows)
362
		CMD="chocolatey"
363
		;;
364 365 366 367 368 369
	MacOS)
		CMD="homebrew"
		;;
	OpenWRT)
		CMD="ipkg"
		;;
370 371 372
	GNU/Linux/Guix)
		CMD="guix"
		;;
373
	*)
374
		fatal "Have no suitable DISTRNAME $DISTRNAME"
375 376 377 378
		;;
esac
PMTYPE=$CMD
}
379