distr_info 19.2 KB
Newer Older
1
#!/bin/sh
2 3 4
# 2007-2021 (c) Vitaly Lipatov <lav@etersoft.ru>
# 2007-2021 (c) Etersoft
# 2007-2021 Public domain
5 6 7 8

# You can set ROOTDIR to root system dir
#ROOTDIR=

Vitaly Lipatov's avatar
Vitaly Lipatov committed
9
PROGVERSION="20210806"
10

11 12
# TODO: check /etc/system-release

13 14 15 16 17 18 19 20 21 22 23 24 25
# Check for DISTRO specific file in /etc
distro()
{
	#[ -n "$ROOTDIR" ] || return
	# fill global DISTROFILE
	DISTROFILE="$ROOTDIR/etc/$1"
	[ -f "$DISTROFILE" ]
}

# Has a distro file the specified word?
has()
{
	[ -n "$DISTROFILE" ] || exit 1
26
	grep "$*" "$DISTROFILE" >/dev/null 2>&1
27 28
}

29 30 31 32 33 34
# Has a system the specified command?
hascommand()
{
	which $1 2>/dev/null >/dev/null
}

35 36 37 38 39
firstupper()
{
	echo "$*" | sed 's/.*/\u&/'
}

40 41 42 43 44 45 46
tolower()
{
	# tr is broken in busybox (checked with OpenWrt)
	#echo "$*" | tr "[:upper:]" "[:lower:]"
	echo "$*" | awk '{print tolower($0)}'
}

47 48 49 50
# Translate DISTRIB_ID to vendor name (like %_vendor does)
rpmvendor()
{
	[ "$DISTRIB_ID" = "ALTLinux" ] && echo "alt" && return
51
	[ "$DISTRIB_ID" = "AstraLinux" ] && echo "astra" && return
52
	[ "$DISTRIB_ID" = "LinuxXP" ] && echo "lxp" && return
53
	[ "$DISTRIB_ID" = "TinyCoreLinux" ] && echo "tcl" && return
54
	[ "$DISTRIB_ID" = "VoidLinux" ] && echo "void" && return
55
	[ "$DISTRIB_ID" = "OpenSUSE" ] && echo "suse" && return
56
	[ -n "$VENDOR_ID" ] && echo "$VENDOR_ID" && return
57
	tolower "$DISTRIB_ID"
58 59 60 61 62 63 64 65 66 67 68 69
}

# Translate DISTRIB_ID name to package manner (like in the package release name)
pkgvendor()
{
	[ "$DISTRIB_ID" = "Mandriva" ] && echo "mdv" && return
	rpmvendor
}

# Print pkgtype (need DISTRIB_ID var)
pkgtype()
{
70
# TODO: try use generic names
Vitaly Lipatov's avatar
Vitaly Lipatov committed
71
    case $(pkgvendor) in
72 73 74
		freebsd) echo "tbz" ;;
		sunos) echo "pkg.gz" ;;
		slackware|mopslinux) echo "tgz" ;;
75
		archlinux|manjaro) echo "pkg.tar.xz" ;;
76 77
		gentoo) echo "tbz2" ;;
		windows) echo "exe" ;;
78
		android) echo "apk" ;;
79
		alpine) echo "apk" ;;
80
		tinycorelinux) echo "tcz" ;;
81
		voidlinux) echo "xbps" ;;
82
		openwrt) echo "ipk" ;;
83
		cygwin) echo "tar.xz" ;;
84
		debian|ubuntu|mint|runtu|mcst|astra|kali) echo "deb" ;;
85
		alt|asplinux|suse|mandriva|rosa|mandrake|pclinux|sled|sles)
86
			echo "rpm" ;;
87
		fedora|redhat|redos|scientific|centos|rhel|goslinux|amzn)
88 89 90 91 92
			echo "rpm" ;;
		*)  echo "rpm" ;;
	esac
}

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 170 171 172 173 174 175 176
# TODO: in more appropriate way
#which pkcon 2>/dev/null >/dev/null && info "You can run $ PMTYPE=packagekit epm to use packagekit backend"

# Print package manager (need DISTRIB_ID var)
pkgmanager()
{
local CMD
# FIXME: some problems with multibased distros (Server Edition on CentOS and Desktop Edition on Ubuntu)
case $DISTRIB_ID in
	ALTLinux)
		CMD="apt-rpm"
		#which ds-install 2>/dev/null >/dev/null && CMD=deepsolver-rpm
		#which pkcon 2>/dev/null >/dev/null && CMD=packagekit-rpm
		;;
	PCLinux)
		CMD="apt-rpm"
		;;
	Ubuntu|Debian|Mint|AstraLinux|Elbrus)
		CMD="apt-dpkg"
		#which aptitude 2>/dev/null >/dev/null && CMD=aptitude-dpkg
		which snappy 2>/dev/null >/dev/null && CMD=snappy
		;;
	Mandriva|ROSA)
		CMD="urpm-rpm"
		;;
	FreeBSD|NetBSD|OpenBSD|Solaris)
		CMD="pkgsrc"
		which pkg 2>/dev/null >/dev/null && CMD=pkgng
		;;
	Gentoo)
		CMD="emerge"
		;;
	ArchLinux)
		CMD="pacman"
		;;
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific|GosLinux|Amzn|RedOS)
		CMD="dnf-rpm"
		which dnf 2>/dev/null >/dev/null || CMD=yum-rpm
		;;
	Slackware)
		CMD="slackpkg"
		;;
	SUSE|SLED|SLES)
		CMD="zypper-rpm"
		;;
	ForesightLinux|rPathLinux)
		CMD="conary"
		;;
	Windows)
		CMD="appget"
		which $CMD 2>/dev/null >/dev/null || CMD="chocolatey"
		which $CMD 2>/dev/null >/dev/null || CMD="winget"
		;;
	MacOS)
		CMD="homebrew"
		;;
	OpenWrt)
		CMD="opkg"
		;;
	GNU/Linux/Guix)
		CMD="guix"
		;;
	Android)
		CMD="android"
		;;
	Cygwin)
		CMD="aptcyg"
		;;
	alpine)
		CMD="apk"
		;;
	TinyCoreLinux)
		CMD="tce"
		;;
	VoidLinux)
		CMD="xbps"
		;;
	*)
		fatal "Have no suitable DISTRIB_ID $DISTRIB_ID"
		;;
esac
echo "$CMD"
}

177 178
get_var()
{
179 180
	# get first variable and print it out, drop quotes if exists
	grep -i "^$1 *=" | head -n 1 | sed -e "s/^[^=]*[ \t]*=[ \t]*//" | sed -e "s/^[\'\"]\(.*\)[\'\"]/\1/"
181 182 183 184 185 186 187 188
}

# 2010.1 -> 2010
get_major_version()
{
	echo "$1" | sed -e "s/\..*//g"
}

189 190 191 192
normalize_name()
{
	[ "$1" = "RED OS" ] && echo "RedOS" && return
	[ "$1" = "CentOS Linux" ] && echo "CentOS" && return
193 194
	#echo "${1// /}"
	echo "$1" | sed -e "s/ //g"
195 196
}

197
# Default values
198
PRETTY_NAME=""
199
DISTRIB_ID=""
200
DISTRIB_RELEASE=""
201
DISTRIB_CODENAME=""
202 203 204

# Default with LSB
if distro lsb-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
205 206 207
	DISTRIB_ID=$(cat $DISTROFILE | get_var DISTRIB_ID)
	DISTRIB_RELEASE=$(cat $DISTROFILE | get_var DISTRIB_RELEASE)
	DISTRIB_CODENAME=$(cat $DISTROFILE | get_var DISTRIB_CODENAME)
208
	PRETTY_NAME=$(cat $DISTROFILE | get_var DISTRIB_DESCRIPTION)
209 210
fi

211 212 213 214 215 216 217 218 219 220 221 222 223 224
# Next default by /etc/os-release
# https://www.freedesktop.org/software/systemd/man/os-release.html
if distro os-release ; then
	# shellcheck disable=SC1090
	. $DISTROFILE
	DISTRIB_ID="$(normalize_name "$NAME")"
#	DISTRIB_ID="$(firstupper "$ID")"
	DISTRIB_RELEASE="$VERSION_ID"
	[ -n "$DISTRIB_RELEASE" ] || DISTRIB_RELEASE="CUR"
	# set by os-release:
	#PRETTY_NAME
	VENDOR_ID="$ID"
fi

225 226 227
# ALT Linux based
if distro altlinux-release ; then
	DISTRIB_ID="ALTLinux"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
228 229
	# FIXME: fast hack for fallback: 10 -> p10 for /etc/os-release
	DISTRIB_RELEASE="p$DISTRIB_RELEASE"
230
	if has Sisyphus ; then DISTRIB_RELEASE="Sisyphus"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
231 232 233
	elif has "ALT p10.* p10 " ; then DISTRIB_RELEASE="p10"
	elif has "ALT c10.* c10 " ; then DISTRIB_RELEASE="c10"
	elif has "ALT p9.* p9 " ; then DISTRIB_RELEASE="p9"
234
	elif has "ALT 9 SP " ; then DISTRIB_RELEASE="c9"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
235 236
	elif has "ALT c9f1" ; then DISTRIB_RELEASE="c9f1"
	elif has "ALT 8 SP " ; then DISTRIB_RELEASE="c8"
237
	elif has "ALT c8.2 " ; then DISTRIB_RELEASE="c8.2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
238 239
	elif has "ALT c8.1 " ; then DISTRIB_RELEASE="c8.1"
	elif has "ALT c8 " ; then DISTRIB_RELEASE="c8"
240
	elif has "ALT .*8.[0-9]" ; then DISTRIB_RELEASE="p8"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
241
	elif has "Simply Linux 10." ; then DISTRIB_RELEASE="p10"
242
	elif has "Simply Linux 9." ; then DISTRIB_RELEASE="p9"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
243 244 245
	elif has "Simply Linux 8." ; then DISTRIB_RELEASE="p8"
	elif has "Simply Linux 7." ; then DISTRIB_RELEASE="p7"
	elif has "Simply Linux 6." ; then DISTRIB_RELEASE="p6"
246
	elif has "ALT Linux p8"  ; then DISTRIB_RELEASE="p8"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
247
	elif has "ALT Linux 8." ; then DISTRIB_RELEASE="p8"
248
	elif has "ALT Linux p7"  ; then DISTRIB_RELEASE="p7"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
249 250 251
	elif has "ALT Linux 7." ; then DISTRIB_RELEASE="p7"
	elif has "ALT Linux t7." ; then DISTRIB_RELEASE="t7"
	elif has "ALT Linux 6." ; then DISTRIB_RELEASE="p6"
252 253 254 255
	elif has "ALT Linux p6"  ; then DISTRIB_RELEASE="p6"
	elif has "ALT Linux p5"  ; then DISTRIB_RELEASE="p5"
	elif has "ALT Linux 5.1" ; then DISTRIB_RELEASE="5.1"
	elif has "ALT Linux 5.0" ; then DISTRIB_RELEASE="5.0"
256
	elif has "ALT Linux 4.1" ; then DISTRIB_RELEASE="4.1"
257
	elif has "ALT Linux 4.0" ; then DISTRIB_RELEASE="4.0"
258
	elif has "starter kit"   ; then DISTRIB_RELEASE="Sisyphus"
259
	elif has Citron   ; then DISTRIB_RELEASE="2.4"
260
	fi
261
	PRETTY_NAME="$(cat /etc/altlinux-release)"
262 263 264

elif distro gentoo-release ; then
	DISTRIB_ID="Gentoo"
265
	MAKEPROFILE=$(readlink $ROOTDIR/etc/portage/make.profile 2>/dev/null) || MAKEPROFILE=$(readlink $ROOTDIR/etc/make.profile)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
266
	DISTRIB_RELEASE=$(basename $MAKEPROFILE)
267
	echo $DISTRIB_RELEASE | grep -q "[0-9]" || DISTRIB_RELEASE=$(basename "$(dirname $MAKEPROFILE)") #"
268 269 270

elif distro slackware-version ; then
	DISTRIB_ID="Slackware"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
271
	DISTRIB_RELEASE="$(grep -Eo '[0-9]+\.[0-9]+' $DISTROFILE)"
272

273
elif distro os-release && hascommand apk ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
274
	# shellcheck disable=SC1090
275
	. $ROOTDIR/etc/os-release
276
	DISTRIB_ID="$(firstupper "$ID")"
277 278
	DISTRIB_RELEASE="$VERSION_ID"

279
elif distro os-release && hascommand tce-ab ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
280
	# shellcheck disable=SC1090
281 282 283 284
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="TinyCoreLinux"
	DISTRIB_RELEASE="$VERSION_ID"

285
elif distro os-release && hascommand xbps-query ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
286
	# shellcheck disable=SC1090
287 288 289 290
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="VoidLinux"
	DISTRIB_RELEASE="Live"

291
# TODO: use standart /etc/os-release or lsb
292 293
elif distro arch-release ; then
	DISTRIB_ID="ArchLinux"
294
	DISTRIB_RELEASE="rolling"
295

296
# Elbrus
297 298
elif distro mcst_version ; then
	DISTRIB_ID="MCST"
299
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g") #"
300

301 302 303 304 305
# OpenWrt
elif distro openwrt_release ; then
	. $DISTROFILE
	DISTRIB_RELEASE=$(cat $ROOTDIR/etc/openwrt_version)

306 307 308 309 310 311
elif distro astra_version ; then
	#DISTRIB_ID=`cat $DISTROFILE | get_var DISTRIB_ID`
	DISTRIB_ID="AstraLinux"
	#DISTRIB_RELEASE=$(cat "$DISTROFILE" | head -n1 | sed -e "s|.* \([a-z]*\).*|\1|g")
	DISTRIB_RELEASE=$DISTRIB_CODENAME

312 313 314 315 316 317 318 319
# for Ubuntu use standard LSB info
elif [ "$DISTRIB_ID" = "Ubuntu" ] && [ -n "$DISTRIB_RELEASE" ]; then
	# use LSB version
	true

# Debian based
elif distro debian_version ; then
	DISTRIB_ID="Debian"
320
	DISTRIB_RELEASE=$(cat $DISTROFILE | sed -e "s/\..*//g")
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340


# Mandriva based
elif distro pclinuxos-release ; then
	DISTRIB_ID="PCLinux"
	if   has "2007" ; then DISTRIB_RELEASE="2007"
	elif has "2008" ; then DISTRIB_RELEASE="2008"
	elif has "2010" ; then DISTRIB_RELEASE="2010"
	fi

elif distro mandriva-release || distro mandrake-release ; then
	DISTRIB_ID="Mandriva"
	if   has 2005 ; then DISTRIB_RELEASE="2005"
	elif has 2006 ; then DISTRIB_RELEASE="2006"
	elif has 2007 ; then DISTRIB_RELEASE="2007"
	elif has 2008 ; then DISTRIB_RELEASE="2008"
	elif has 2009.0 ; then DISTRIB_RELEASE="2009.0"
	elif has 2009.1 ; then DISTRIB_RELEASE="2009.1"
	else
		# use /etc/lsb-release info by default
341 342 343
		if has ROSA ; then
			DISTRIB_ID="ROSA"
		fi
344 345 346 347 348 349 350 351 352 353
	fi

# Fedora based

elif distro MCBC-release ; then
	DISTRIB_ID="MCBC"
	if   has 3.0 ; then DISTRIB_RELEASE="3.0"
	elif has 3.1 ; then DISTRIB_RELEASE="3.1"
	fi

354 355
# TODO: drop in favour of /etc/os-release
elif distro redhat-release && [ -z "$PRETTY_NAME" ] ; then
356 357 358 359 360 361 362
	# FIXME if need
	# actually in the original RHEL: Red Hat Enterprise Linux .. release N
	DISTRIB_ID="RHEL"
	if has CentOS ; then
		DISTRIB_ID="CentOS"
	elif has Scientific ; then
		DISTRIB_ID="Scientific"
363 364
	elif has GosLinux ; then
		DISTRIB_ID="GosLinux"
365 366 367 368 369 370 371 372
	fi
	if has Beryllium ; then
		DISTRIB_ID="Scientific"
		DISTRIB_RELEASE="4.1"
	elif has "release 4" ; then DISTRIB_RELEASE="4"
	elif has "release 5" ; then DISTRIB_RELEASE="5"
	elif has "release 6" ; then DISTRIB_RELEASE="6"
	elif has "release 7" ; then DISTRIB_RELEASE="7"
373
	elif has "release 8" ; then DISTRIB_RELEASE="8"
374 375 376 377 378 379 380 381 382 383 384 385 386
	fi

# SUSE based
elif distro SuSe-release || distro SuSE-release ; then
	DISTRIB_ID="SUSE"
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "VERSION" | sed -e "s|^VERSION = ||g")
	if   has "SUSE Linux Enterprise Desktop" ; then
		DISTRIB_ID="SLED"
	elif has "SUSE Linux Enterprise Server" ; then
		DISTRIB_ID="SLES"
	fi

# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
387
elif [ "$(uname)" = "FreeBSD" ] ; then
388 389
	DISTRIB_ID="FreeBSD"
	UNAME=$(uname -r)
390
	DISTRIB_RELEASE=$(echo "$UNAME" | grep RELEASE | sed -e "s|\([0-9]\.[0-9]\)-RELEASE|\1|g") #"
391 392

# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
393
elif [ "$(uname)" = "SunOS" ] ; then
394 395 396
	DISTRIB_ID="SunOS"
	DISTRIB_RELEASE=$(uname -r)

Vitaly Lipatov's avatar
Vitaly Lipatov committed
397
# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
398
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
399 400 401
	DISTRIB_ID="MacOS"
	DISTRIB_RELEASE=$(uname -r)

402
# fixme: move to up
403
elif [ "$(uname)" = "Linux" ] && hascommand guix ; then
404 405 406
	DISTRIB_ID="GNU/Linux/Guix"
	DISTRIB_RELEASE=$(uname -r)

407
# fixme: move to up
Vitaly Lipatov's avatar
Vitaly Lipatov committed
408
elif [ "$(uname)" = "Linux" ] && [ -x $ROOTDIR/system/bin/getprop ] ; then
409 410 411
	DISTRIB_ID="Android"
	DISTRIB_RELEASE=$(getprop | awk -F": " '/build.version.release/ { print $2 }' | tr -d '[]')

Vitaly Lipatov's avatar
Vitaly Lipatov committed
412
elif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] ; then
413 414
        DISTRIB_ID="Cygwin"
        DISTRIB_RELEASE="all"
415
fi
416

417 418
[ -n "$DISTRIB_ID" ] || DISTRIB_ID="Generic"

419 420
if [ -z "$PRETTY_NAME" ] ; then
	PRETTY_NAME="$DISTRIB_ID $DISTRIB_RELEASE"
421 422
fi

423

424 425 426 427 428
get_uname()
{
    tolower $(uname $1) | tr -d " \t\r\n"
}

429 430 431 432
get_base_os_name()
{
local DIST_OS
# Resolve the os
433
DIST_OS="$(get_uname -s)"
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
case "$DIST_OS" in
    'sunos')
        DIST_OS="solaris"
        ;;
    'hp-ux' | 'hp-ux64')
        DIST_OS="hpux"
        ;;
    'darwin' | 'oarwin')
        DIST_OS="macosx"
        ;;
    'unix_sv')
        DIST_OS="unixware"
        ;;
    'freebsd' | 'openbsd' | 'netbsd')
        DIST_OS="freebsd"
        ;;
esac
echo "$DIST_OS"
}

454

455 456 457 458
get_arch()
{
local DIST_ARCH
# Resolve the architecture
459
DIST_ARCH="$(get_uname -m)"
460
case "$DIST_ARCH" in
461
    'ia32' | 'i386' | 'i486' | 'i586' | 'i686')
462 463
        DIST_ARCH="x86"
        ;;
464 465 466
    'amd64' | 'x86_64')
        DIST_ARCH="x86_64"
        ;;
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    'ia64' | 'ia-64')
        DIST_ARCH="ia64"
        ;;
    'ip27' | 'mips')
        DIST_ARCH="mips"
        ;;
    'powermacintosh' | 'power' | 'powerpc' | 'power_pc' | 'ppc64')
        DIST_ARCH="ppc"
        ;;
    'pa_risc' | 'pa-risc')
        DIST_ARCH="parisc"
        ;;
    'sun4u' | 'sparcv9')
        DIST_ARCH="sparc"
        ;;
    '9000/800')
        DIST_ARCH="parisc"
        ;;
485 486 487 488 489 490 491
    'arm64' | 'aarch64')
        DIST_ARCH='aarch64'
        ;;
    armv7*)
        # TODO: use uname only
        # uses binutils package
        if which readelf >/dev/null 2>/dev/null && [ -z "$(readelf -A /proc/self/exe | grep Tag_ABI_VFP_args)" ] ; then
492 493 494 495 496 497 498 499 500
            DIST_ARCH="armel"
        else
            DIST_ARCH="armhf"
        fi
        ;;
esac
echo "$DIST_ARCH"
}

501 502 503 504
get_debian_arch()
{
    local arch="$(get_arch)"
    case $arch in
505
    'x86')
506 507 508 509 510 511 512
        arch='i386' ;;
    'x86_64')
        arch='amd64' ;;
    esac
    echo "$arch"
}

513 514 515 516 517
get_distro_arch()
{
    local arch="$(get_arch)"
    case "$(pkgtype)" in
        rpm)
518 519 520 521
            case $arch in
            'x86')
                arch='i586' ;;
            esac
522 523
            ;;
        deb)
524 525
            get_debian_arch
            return
526 527 528 529 530
            ;;
    esac
    echo "$arch"
}

531 532 533
get_bit_size()
{
local DIST_BIT
534 535 536 537 538 539 540 541 542

DIST_BIT="$(getconf LONG_BIT 2>/dev/null)"
if [ -n "$DIST_BIT" ] ; then
    echo "$DIST_BIT"
    exit
fi

# Try detect arch size by arch name
case "$(get_uname -m)" in
543 544 545
    'amd64' | 'ia64' | 'x86_64' | 'ppc64')
        DIST_BIT="64"
        ;;
546 547 548
    'aarch64')
        DIST_BIT="64"
        ;;
549 550 551
    'e2k')
        DIST_BIT="64"
        ;;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
#    'pa_risc' | 'pa-risc') # Are some of these 64bit? Least not all...
#       BIT="64"
#        ;;
    'sun4u' | 'sparcv9') # Are all sparcs 64?
        DIST_BIT="64"
        ;;
#    '9000/800')
#       DIST_BIT="64"
#        ;;
    *) # In any other case default to 32
        DIST_BIT="32"
        ;;
esac
echo "$DIST_BIT"
}

568
# TODO: check before calc
569 570 571
get_memory_size()
{
    local detected=""
572 573 574 575 576 577 578 579 580 581 582
    local DIST_OS="$(get_base_os_name)"
    case "$DIST_OS" in
        macosx)
            detected=$((`sysctl hw.memsize | sed s/"hw.memsize: "//`/1024/1024))
            ;;
        freebsd)
            detected=$((`sysctl hw.physmem | sed s/"hw.physmem: "//`/1024/1024))
            ;;
        linux)
            [ -r /proc/meminfo ] && detected=$((`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`/1024))
            ;;
583 584 585 586 587
        solaris)
            detected=$(prtconf | grep Memory | sed -e "s|Memory size: \([0-9][0-9]*\) Megabyte.*|\1|")
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
588 589
    esac

590
    [ -n "$detected" ] || detected=0
591 592 593
    echo $detected
}

594 595 596 597 598
print_name_version()
{
    [ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
get_core_count()
{
    local detected=""
    local DIST_OS="$(get_base_os_name)"
    case "$DIST_OS" in
        macos|freebsd)
            detected=$(sysctl hw.ncpu | awk '{print $2}')
            ;;
        linux)
            detected=$(grep -c "^processor" /proc/cpuinfo)
            ;;
        solaris)
            detected=$(prtconf | grep -c 'cpu[^s]')
            ;;
        aix)
            detected=$(lsdev -Cc processor -S A | wc -l)
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
    esac

    [ -n "$detected" ] || detected=0
    echo $detected
}

624 625 626 627 628
get_core_mhz()
{
    cat /proc/cpuinfo | grep "cpu MHz" | head -n1 | cut -d':' -f2 | cut -d' ' -f2 | cut -d'.' -f1
}

629

630 631 632 633 634 635 636 637 638 639 640
get_virt()
{
    local VIRT
    local SDCMD
    SDCMD=$(which systemd-detect-virt 2>/dev/null)
    if [ -n "$SDCMD" ] ; then
        VIRT="$($SDCMD)"
        [ "$VIRT" = "none" ] && echo "(host system)" && return
        [ -z "$VIRT" ] && echo "(unknown)" && return
        echo "$VIRT" && return
    fi
641 642 643 644 645

    # TODO: use virt-what under root

    # inspired by virt_what
    if [ -d "/proc/vz" -a ! -d "/proc/bc" ]; then
646 647
        echo "openvz" && return
    fi
648 649 650 651 652

    if [ -r "/sys/bus/xen" ] ; then
        echo "xen" && return
    fi

653 654 655 656 657 658 659 660
    echo "(unknown)"
    # TODO: check for openvz
}

# https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem
get_service_manager()
{
    [ -d /run/systemd/system ] && echo "systemd" && return
661 662
    # TODO
    #[ -d /usr/share/upstart ] && echo "upstart" && return
663 664 665 666
    [ -d /etc/init.d ] && echo "sysvinit" && return
    echo "(unknown)"
}

667 668 669 670
print_pretty_name()
{
    echo "$PRETTY_NAME"
}
671 672 673 674

print_total_info()
{
cat <<EOF
675
distro_info v$PROGVERSION : Copyright © 2007-2021 Etersoft
676
==== Total system information:
677
Pretty distro name (--pretty): $(print_pretty_name)
678
 Distro name and version (-e): $(print_name_version)
679
 Package manager/type (-g/-p): $(pkgmanager) / $(pkgtype)
680 681
 Running service manager (-y): $(get_service_manager)
          Virtualization (-i): $(get_virt)
682
        CPU Cores/MHz (-c/-z): $(get_core_count) / $(get_core_mhz) MHz
683 684 685 686 687 688
        CPU Architecture (-a): $(get_arch)
 CPU norm register size  (-b): $(get_bit_size)
 System memory size (MB) (-m): $(get_memory_size)
            Base OS name (-o): $(get_base_os_name)
Build system distro name (-s): $(pkgvendor)
Build system vendor name (-n): $(rpmvendor)
689 690

(run with -h to get help)
691 692 693
EOF
}

694

695
case $1 in
696
	-h|--help)
697
		echo "distro_info v$PROGVERSION - distro information retriever"
698 699
		echo "Usage: distro_info [options] [args]"
		echo "Options:"
700
		echo " -a - print hardware architecture (--distro-arch for distro depended name)"
701
		echo " -b - print size of arch bit (32/64)"
702
		echo " -c - print number of CPU cores"
703
		echo " -z - print current CPU MHz"
704 705 706 707 708 709 710 711
		echo " -d - print distro name"
		echo " -e - print full name of distro with version"
		echo " -i - print virtualization type"
		echo " -h - this help"
		echo " -m - print system memory size (in MB)"
		echo " -n [SystemName] - print vendor name (as _vendor macros in rpm)"
		echo " -o - print base OS name"
		echo " -p [SystemName] - print type of the packaging system"
712
		echo " -g [SystemName] - print name of the packaging system"
713
		echo " -s [SystemName] - print name of distro for build system (like in the package release name)"
714 715
		echo " -y - print running service manager"
		echo " --pretty - print pretty distro name"
716 717 718 719 720
		echo " -v - print version of distro"
		echo " -V - print the utility version"
		echo "Run without args to print all information."
		exit 0
		;;
721 722 723 724 725 726
	-p)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgtype
		exit 0
		;;
727 728 729 730 731 732
	-g)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgmanager
		exit 0
		;;
733 734 735
	--pretty)
		print_pretty_name
		;;
736 737 738 739 740 741
	--distro-arch)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		get_distro_arch
		exit 0
		;;
742 743 744 745 746 747
	--debian-arch)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		get_debian_arch
		exit 0
		;;
748 749 750
	-d)
		echo $DISTRIB_ID
		;;
751 752 753 754 755 756
	-a)
		get_arch
		;;
	-b)
		get_bit_size
		;;
757 758 759
	-c)
		get_core_count
		;;
760 761 762
	-z)
		get_core_mhz
		;;
763 764 765
	-i)
		get_virt
		;;
766 767 768 769 770 771
	-m)
		get_memory_size
		;;
	-o)
		get_base_os_name
		;;
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
	-v)
		echo $DISTRIB_RELEASE
		;;
	-s)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgvendor
		exit 0
		;;
	-n)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		rpmvendor
		exit 0
		;;
787 788 789
	-y)
		get_service_manager
		;;
790
	-V)
791
		echo "$PROGVERSION"
792 793
		exit 0
		;;
794 795 796
	-e)
		print_name_version
		;;
797
	*)
798
		print_total_info
799 800 801
		;;
esac