epm-sh-functions 4.3 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 95
}

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

99 100 101 102 103 104 105 106 107 108 109 110
# Print command line and run command line
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
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

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


# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
136
		echo "Error: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
137 138
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
139 140 141
	fi
	exit 1
}
142

143 144
set_sudo()
{
145 146 147 148
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return

149 150
	# set SUDO not for root user
	[ -n "$UID" ] || UID=`id -u`
151 152 153 154 155 156 157 158

	# 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.'"
159 160
}

161 162 163
# print options description from HELPCMD/HELPOPT lines in the code
get_help()
{
164
    grep "# $1" $0 | while read n ; do
165 166 167 168 169 170
        opt=$(echo $n | sed -e "s|) # $1:.*||g")
        desc=$(echo $n | sed -e "s|.*) # $1:||g")
        printf "    %-20s %s\n" $opt "$desc"
    done
}

171

172 173 174 175
# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
176 177 178 179 180 181 182

	# Fill for use: PMTYPE, DISTRNAME, DISTRVERSION, PKGFORMAT, PKGVENDOR, RPMVENDOR
	DISTRVENDOR=$PROGDIR/distr_info
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d)
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
	set_target_pkg_env

183 184 185 186 187 188
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

189
case $DISTRNAME in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
190
	ALTLinux|PCLinux)
191 192 193 194
		CMD="apt-rpm"
		#which deepsolver 2>/dev/null >/dev/null && CMD=deepsolver-rpm
		;;
	PCLinux)
195 196
		CMD="apt-rpm"
		;;
197
	Ubuntu|Debian|Mint)
198 199
		CMD="apt-dpkg"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
200
	Mandriva|ROSA)
201 202
		CMD="urpm-rpm"
		;;
203 204
	FreeBSD)
		CMD="pkg_add"
205
		;;
206 207 208
	Gentoo)
		CMD="emerge"
		;;
209 210 211 212 213 214
	ArchLinux)
		CMD="pacman"
		;;
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific)
		CMD="yum-rpm"
		;;
215
	Slackware)
216
		CMD="slackpkg"
217
		;;
218 219 220
	SUSE|SLED|SLES)
		CMD="zypper-rpm"
		;;
221
	Windows)
222
		CMD="chocolatey"
223
		;;
224
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
225
		fatal "Do not known DISTRNAME $DISTRNAME"
226 227 228 229
		;;
esac
PMTYPE=$CMD
}
230