epm-sh-functions 4.16 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/bin/sh
#
# Copyright (C) 2012  Etersoft
# Copyright (C) 2012  Vitaly Lipatov <lav@etersoft.ru>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (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
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

21 22 23 24 25 26 27 28 29
# 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
}

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

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

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

restore_color()
{
53
	[ "$USETTY" = "1" ] || return
54 55 56 57 58 59
	{
		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
60 61 62
echover()
{
    [ -n "$verbose" ] || return
63
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
64 65 66 67 68 69 70 71 72 73 74
}

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

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

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

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

100 101 102 103 104 105 106 107 108 109 110 111
# Print command line and run command line
docmd_foreach()
{
	local cmd
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
		docmd $cmd $pkg
	done
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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
137
		echo "Error: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
138 139
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
140 141 142
	fi
	exit 1
}
143

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

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

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

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

172

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

	# 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

184
case $DISTRNAME in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
185
	ALTLinux|PCLinux)
186 187
		CMD="apt-rpm"
		;;
188
	Ubuntu|Debian|Mint)
189 190
		CMD="apt-dpkg"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
191
	Mandriva|ROSA)
192 193
		CMD="urpm-rpm"
		;;
194 195
	FreeBSD)
		CMD="pkg_add"
196
		;;
197 198 199
	Gentoo)
		CMD="emerge"
		;;
200 201 202 203 204 205
	ArchLinux)
		CMD="pacman"
		;;
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific)
		CMD="yum-rpm"
		;;
206
	Slackware)
207
		CMD="slackpkg"
208
		;;
209 210 211
	SUSE|SLED|SLES)
		CMD="zypper-rpm"
		;;
212
	Windows)
213
		CMD="chocolatey"
214
		;;
215
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
216
		fatal "Do not known DISTRNAME $DISTRNAME"
217 218 219 220
		;;
esac
PMTYPE=$CMD
}
221