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

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

9
PROGVERSION="20220323"
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
# Has a system the specified command?
hascommand()
{
32
	which "$1" 2>/dev/null >/dev/null
33 34
}

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 51 52 53 54
override_distrib()
{
	[ -n "$1" ] || return
	VENDOR_ID=''
	DISTRIB_ID="$(echo "$1" | sed -e 's|/.*||')"
	DISTRIB_RELEASE="$(echo "$1" | sed -e 's|.*/||')"
}

55 56
# Translate DISTRIB_ID to vendor name (like %_vendor does or package release name uses), uses VENDOR_ID by default
pkgvendor()
57 58
{
	[ "$DISTRIB_ID" = "ALTLinux" ] && echo "alt" && return
59
	[ "$DISTRIB_ID" = "ALTServer" ] && echo "alt" && return
60
	[ "$DISTRIB_ID" = "AstraLinux" ] && echo "astra" && return
61
	[ "$DISTRIB_ID" = "LinuxXP" ] && echo "lxp" && return
62
	[ "$DISTRIB_ID" = "TinyCoreLinux" ] && echo "tcl" && return
63
	[ "$DISTRIB_ID" = "VoidLinux" ] && echo "void" && return
64
	[ "$DISTRIB_ID" = "OpenSUSE" ] && echo "suse" && return
65 66
	[ "$DISTRIB_ID" = "openSUSETumbleweed" ] && echo "suse" && return
	[ "$DISTRIB_ID" = "openSUSELeap" ] && echo "suse" && return
67 68 69 70 71
	if [ -n "$VENDOR_ID" ] ; then
		[ "$VENDOR_ID" = "altlinux" ] && echo "alt" && return
		echo "$VENDOR_ID"
		return
	fi
72
	tolower "$DISTRIB_ID"
73 74
}

75 76 77 78 79 80 81 82 83
# 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
84
	ALTLinux|ALTServer)
85 86
		#which ds-install 2>/dev/null >/dev/null && CMD=deepsolver-rpm
		#which pkcon 2>/dev/null >/dev/null && CMD=packagekit-rpm
87 88 89 90
		CMD="apt-rpm"
		;;
	ALTServer)
		CMD="apt-rpm"
91 92 93 94 95 96 97
		;;
	PCLinux)
		CMD="apt-rpm"
		;;
	Ubuntu|Debian|Mint|AstraLinux|Elbrus)
		CMD="apt-dpkg"
		#which aptitude 2>/dev/null >/dev/null && CMD=aptitude-dpkg
98
		hascommand snappy && CMD=snappy
99 100 101 102 103 104 105 106 107 108 109 110 111 112
		;;
	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"
		;;
113
	Fedora|CentOS|OracleLinux|RockyLinux|AlmaLinux|RHEL|Scientific|GosLinux|Amzn|RedOS)
114
		CMD="dnf-rpm"
115
		hascommand dnf || CMD=yum-rpm
116
		[ "$DISTRIB_ID/$DISTRIB_RELEASE" = "CentOS/7" ] && CMD=yum-rpm
117 118 119 120
		;;
	Slackware)
		CMD="slackpkg"
		;;
121
	SUSE|SLED|SLES|openSUSETumbleweed|openSUSELeap)
122 123 124 125 126 127 128
		CMD="zypper-rpm"
		;;
	ForesightLinux|rPathLinux)
		CMD="conary"
		;;
	Windows)
		CMD="appget"
129 130
		hascommand $CMD || CMD="chocolatey"
		hascommand $CMD || CMD="winget"
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
		;;
	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"
		;;
	*)
157 158 159 160
		# try detect firstly
		if hascommand "rpm" ; then
			hascommand "urpmi" && echo "urpmi-rpm" && return
			hascommand "zypper" && echo "zypper-rpm" && return
161
			hascommand "apt-get" && echo "apt-rpm" && return
162 163 164 165 166 167
			hascommand "dnf" && echo "dnf-rpm" && return
		fi
		if hascommand "dpkg" ; then
			hascommand "apt" && echo "apt-dpkg" && return
			hascommand "apt-get" && echo "apt-dpkg" && return
		fi
168
		echo "We don't support yet DISTRIB_ID $DISTRIB_ID" >&2
169 170 171 172 173
		;;
esac
echo "$CMD"
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
# Print pkgtype (need DISTRIB_ID var)
pkgtype()
{
# TODO: try use generic names
    case $(pkgvendor) in
		freebsd) echo "tbz" ;;
		sunos) echo "pkg.gz" ;;
		slackware|mopslinux) echo "tgz" ;;
		archlinux|manjaro) echo "pkg.tar.xz" ;;
		gentoo) echo "tbz2" ;;
		windows) echo "exe" ;;
		android) echo "apk" ;;
		alpine) echo "apk" ;;
		tinycorelinux) echo "tcz" ;;
		voidlinux) echo "xbps" ;;
		openwrt) echo "ipk" ;;
		cygwin) echo "tar.xz" ;;
		*)
			case $(pkgmanager) in
				*-dpkg)
194
					echo "deb" ;;
195 196 197 198 199 200 201 202 203
				*-rpm)
					echo "rpm" ;;
				*)
					echo "rpm" ;;
			esac
	esac
}


204 205
get_var()
{
206 207
	# 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/"
208 209 210 211 212 213 214 215
}

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

216 217
normalize_name()
{
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	case "$1" in
		"RED OS")
			echo "RedOS"
			;;
		"CentOS Linux")
			echo "CentOS"
			;;
		"Fedora Linux")
			echo "Fedora"
			;;
		*)
			#echo "${1// /}"
			echo "$1" | sed -e "s/ //g"
			;;
	esac
233 234
}

235
# Default values
236
PRETTY_NAME=""
237
DISTRIB_ID=""
238
DISTRIB_RELEASE=""
239
DISTRIB_CODENAME=""
240 241 242

# Default with LSB
if distro lsb-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
243 244 245
	DISTRIB_ID=$(cat $DISTROFILE | get_var DISTRIB_ID)
	DISTRIB_RELEASE=$(cat $DISTROFILE | get_var DISTRIB_RELEASE)
	DISTRIB_CODENAME=$(cat $DISTROFILE | get_var DISTRIB_CODENAME)
246
	PRETTY_NAME=$(cat $DISTROFILE | get_var DISTRIB_DESCRIPTION)
247 248
fi

249 250 251 252 253 254 255 256 257 258 259 260 261 262
# 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

263 264 265
# ALT Linux based
if distro altlinux-release ; then
	DISTRIB_ID="ALTLinux"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
266
	# FIXME: fast hack for fallback: 10 -> p10 for /etc/os-release
267
	DISTRIB_RELEASE="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
268
	if has Sisyphus ; then DISTRIB_RELEASE="Sisyphus"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
269
	elif has "ALT p10.* p10 " ; then DISTRIB_RELEASE="p10"
270 271
	elif has "ALTServer 10." ; then DISTRIB_RELEASE="p10"
	elif has "ALTServer 9." ; then DISTRIB_RELEASE="p9"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
272 273
	elif has "ALT c10.* c10 " ; then DISTRIB_RELEASE="c10"
	elif has "ALT p9.* p9 " ; then DISTRIB_RELEASE="p9"
274
	elif has "ALT 9 SP " ; then DISTRIB_RELEASE="c9"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
275 276
	elif has "ALT c9f1" ; then DISTRIB_RELEASE="c9f1"
	elif has "ALT 8 SP " ; then DISTRIB_RELEASE="c8"
277
	elif has "ALT c8.2 " ; then DISTRIB_RELEASE="c8.2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
278 279
	elif has "ALT c8.1 " ; then DISTRIB_RELEASE="c8.1"
	elif has "ALT c8 " ; then DISTRIB_RELEASE="c8"
280
	elif has "ALT .*8.[0-9]" ; then DISTRIB_RELEASE="p8"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
281
	elif has "Simply Linux 10." ; then DISTRIB_RELEASE="p10"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
282
	elif has "Simply Linux 9." ; then DISTRIB_RELEASE="p9"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
283 284 285
	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"
286
	elif has "ALT Linux p8"  ; then DISTRIB_RELEASE="p8"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
287
	elif has "ALT Linux 8." ; then DISTRIB_RELEASE="p8"
288
	elif has "ALT Linux p7"  ; then DISTRIB_RELEASE="p7"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
289 290 291
	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"
292 293 294 295
	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"
296
	elif has "ALT Linux 4.1" ; then DISTRIB_RELEASE="4.1"
297
	elif has "ALT Linux 4.0" ; then DISTRIB_RELEASE="4.0"
298
	elif has "starter kit"   ; then DISTRIB_RELEASE="Sisyphus"
299
	elif has Citron   ; then DISTRIB_RELEASE="2.4"
300
	fi
301
	PRETTY_NAME="$(cat /etc/altlinux-release)"
302 303 304

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

309 310 311
elif [ "$DISTRIB_ID" = "ALTServer" ] ; then
	DISTRIB_RELEASE=$(echo $DISTRIB_RELEASE | sed -e "s/\..*//g")

312 313 314 315 316 317 318 319 320 321 322
elif [ "$DISTRIB_ID" = "ALTSPWorkstation" ] ; then
	DISTRIB_ID="ALTLinux"
	case "$DISTRIB_RELEASE" in
		8.0|8.1)
			;;
		8.*)
			DISTRIB_RELEASE="c9"
			;;
	esac
	DISTRIB_RELEASE=$(echo $DISTRIB_RELEASE | sed -e "s/\..*//g")

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

327
elif distro os-release && hascommand apk ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
328
	# shellcheck disable=SC1090
329
	. $ROOTDIR/etc/os-release
330
	DISTRIB_ID="$(firstupper "$ID")"
331 332
	DISTRIB_RELEASE="$VERSION_ID"

333
elif distro os-release && hascommand tce-ab ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
334
	# shellcheck disable=SC1090
335 336 337 338
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="TinyCoreLinux"
	DISTRIB_RELEASE="$VERSION_ID"

339
elif distro os-release && hascommand xbps-query ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
340
	# shellcheck disable=SC1090
341 342 343 344
	. $ROOTDIR/etc/os-release
	DISTRIB_ID="VoidLinux"
	DISTRIB_RELEASE="Live"

345
# TODO: use standart /etc/os-release or lsb
346 347
elif distro arch-release ; then
	DISTRIB_ID="ArchLinux"
348
	DISTRIB_RELEASE="rolling"
349

350
# Elbrus
351 352
elif distro mcst_version ; then
	DISTRIB_ID="MCST"
353
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g") #"
354

355 356 357 358 359
# OpenWrt
elif distro openwrt_release ; then
	. $DISTROFILE
	DISTRIB_RELEASE=$(cat $ROOTDIR/etc/openwrt_version)

360 361 362 363 364 365
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

366 367 368 369 370 371 372 373
# 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"
374
	DISTRIB_RELEASE=$(cat $DISTROFILE | sed -e "s/\..*//g")
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394


# 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
395 396 397
		if has ROSA ; then
			DISTRIB_ID="ROSA"
		fi
398 399 400 401 402 403 404 405 406 407
	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

408 409
# TODO: drop in favour of /etc/os-release
elif distro redhat-release && [ -z "$PRETTY_NAME" ] ; then
410 411 412 413 414 415 416
	# 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"
417 418
	elif has GosLinux ; then
		DISTRIB_ID="GosLinux"
419 420 421 422 423 424 425 426
	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"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
427
	elif has "release 8" ; then DISTRIB_RELEASE="8"
428 429 430 431 432 433 434 435 436 437 438 439 440
	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
441
elif [ "$(uname)" = "FreeBSD" ] ; then
442 443
	DISTRIB_ID="FreeBSD"
	UNAME=$(uname -r)
444
	DISTRIB_RELEASE=$(echo "$UNAME" | grep RELEASE | sed -e "s|\([0-9]\.[0-9]\)-RELEASE|\1|g") #"
445 446

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
451
# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
452
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
453 454 455
	DISTRIB_ID="MacOS"
	DISTRIB_RELEASE=$(uname -r)

456
# fixme: move to up
457
elif [ "$(uname)" = "Linux" ] && hascommand guix ; then
458 459 460
	DISTRIB_ID="GNU/Linux/Guix"
	DISTRIB_RELEASE=$(uname -r)

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
466
elif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] ; then
467 468
        DISTRIB_ID="Cygwin"
        DISTRIB_RELEASE="all"
469
fi
470

471 472
[ -n "$DISTRIB_ID" ] || DISTRIB_ID="Generic"

473 474
if [ -z "$PRETTY_NAME" ] ; then
	PRETTY_NAME="$DISTRIB_ID $DISTRIB_RELEASE"
475 476
fi

477

478 479 480 481 482
get_uname()
{
    tolower $(uname $1) | tr -d " \t\r\n"
}

483 484 485 486
get_base_os_name()
{
local DIST_OS
# Resolve the os
487
DIST_OS="$(get_uname -s)"
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
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"
}

508

509 510 511 512
get_arch()
{
local DIST_ARCH
# Resolve the architecture
513
DIST_ARCH="$(get_uname -m)"
514
case "$DIST_ARCH" in
515
    'ia32' | 'i386' | 'i486' | 'i586' | 'i686')
516 517
        DIST_ARCH="x86"
        ;;
518 519 520
    'amd64' | 'x86_64')
        DIST_ARCH="x86_64"
        ;;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    '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"
        ;;
539 540 541 542 543 544 545
    '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
546 547 548 549 550 551 552 553 554
            DIST_ARCH="armel"
        else
            DIST_ARCH="armhf"
        fi
        ;;
esac
echo "$DIST_ARCH"
}

555 556 557 558
get_debian_arch()
{
    local arch="$(get_arch)"
    case $arch in
559
    'x86')
560 561 562 563 564 565 566
        arch='i386' ;;
    'x86_64')
        arch='amd64' ;;
    esac
    echo "$arch"
}

567 568 569 570 571
get_distro_arch()
{
    local arch="$(get_arch)"
    case "$(pkgtype)" in
        rpm)
572 573 574 575
            case $arch in
            'x86')
                arch='i586' ;;
            esac
576 577
            ;;
        deb)
578 579
            get_debian_arch
            return
580 581 582 583 584
            ;;
    esac
    echo "$arch"
}

585 586 587
get_bit_size()
{
local DIST_BIT
588 589 590 591 592 593 594 595 596

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
597 598 599
    'amd64' | 'ia64' | 'x86_64' | 'ppc64')
        DIST_BIT="64"
        ;;
600 601 602
    'aarch64')
        DIST_BIT="64"
        ;;
603 604 605
    'e2k')
        DIST_BIT="64"
        ;;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
#    '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"
}

622
# TODO: check before calc
623 624 625
get_memory_size()
{
    local detected=""
626 627 628 629 630 631 632 633 634 635 636
    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))
            ;;
637 638 639 640 641
        solaris)
            detected=$(prtconf | grep Memory | sed -e "s|Memory size: \([0-9][0-9]*\) Megabyte.*|\1|")
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
642 643
    esac

644
    [ -n "$detected" ] || detected=0
645 646 647
    echo $detected
}

648 649 650 651 652
print_name_version()
{
    [ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
}

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
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
}

678 679 680 681 682
get_core_mhz()
{
    cat /proc/cpuinfo | grep "cpu MHz" | head -n1 | cut -d':' -f2 | cut -d' ' -f2 | cut -d'.' -f1
}

683

684 685 686 687 688 689 690 691 692 693 694
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
695 696 697 698 699

    # TODO: use virt-what under root

    # inspired by virt_what
    if [ -d "/proc/vz" -a ! -d "/proc/bc" ]; then
700 701
        echo "openvz" && return
    fi
702 703 704 705 706

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

707 708 709 710 711 712 713 714
    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
715 716
    # TODO
    #[ -d /usr/share/upstart ] && echo "upstart" && return
717 718 719 720
    [ -d /etc/init.d ] && echo "sysvinit" && return
    echo "(unknown)"
}

721 722 723 724
print_pretty_name()
{
    echo "$PRETTY_NAME"
}
725 726 727 728

print_total_info()
{
cat <<EOF
729
distro_info v$PROGVERSION : Copyright © 2007-2022 Etersoft
730 731 732 733 734 735 736 737 738 739 740 741
Total system information:
    Pretty distro name (--pretty): $(print_pretty_name)
     Distro name and version (-e): $(print_name_version)
     Package manager/type (-g/-p): $(pkgmanager) / $(pkgtype)
     Running service manager (-y): $(get_service_manager)
              Virtualization (-i): $(get_virt)
            CPU Cores/MHz (-c/-z): $(get_core_count) / $(get_core_mhz) MHz
            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)
Base distro (vendor) name (-s|-n): $(pkgvendor)
742 743

(run with -h to get help)
744 745 746
EOF
}

747

748
case $1 in
749
	-h|--help)
750
		echo "distro_info v$PROGVERSION - distro information retriever"
751 752
		echo "Usage: distro_info [options] [args]"
		echo "Options:"
753
		echo " -a - print hardware architecture (--distro-arch for distro depended name)"
754
		echo " -b - print size of arch bit (32/64)"
755
		echo " -c - print number of CPU cores"
756
		echo " -z - print current CPU MHz"
757 758 759 760 761 762 763
		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 " -o - print base OS name"
		echo " -p [SystemName] - print type of the packaging system"
764
		echo " -g [SystemName] - print name of the packaging system"
765
		echo " -s|-n [SystemName] - print base name of the distro (vendor name) (ubuntu for all Ubuntu family, alt for all ALT family) (as _vendor macros in rpm)"
766 767
		echo " -y - print running service manager"
		echo " --pretty - print pretty distro name"
768 769 770 771 772
		echo " -v - print version of distro"
		echo " -V - print the utility version"
		echo "Run without args to print all information."
		exit 0
		;;
773
	-p)
774
		override_distrib "$2"
775 776 777
		pkgtype
		exit 0
		;;
778
	-g)
779
		override_distrib "$2"
780 781 782
		pkgmanager
		exit 0
		;;
783 784 785
	--pretty)
		print_pretty_name
		;;
786
	--distro-arch)
787
		override_distrib "$2"
788 789 790
		get_distro_arch
		exit 0
		;;
791
	--debian-arch)
792
		override_distrib "$2"
793 794 795
		get_debian_arch
		exit 0
		;;
796 797 798
	-d)
		echo $DISTRIB_ID
		;;
799 800 801 802 803 804
	-a)
		get_arch
		;;
	-b)
		get_bit_size
		;;
805 806 807
	-c)
		get_core_count
		;;
808 809 810
	-z)
		get_core_mhz
		;;
811 812 813
	-i)
		get_virt
		;;
814 815 816 817 818 819
	-m)
		get_memory_size
		;;
	-o)
		get_base_os_name
		;;
820 821 822
	-v)
		echo $DISTRIB_RELEASE
		;;
823
	-s|-n)
824
		override_distrib "$2"
825 826 827
		pkgvendor
		exit 0
		;;
828 829 830
	-y)
		get_service_manager
		;;
831
	-V)
832
		echo "$PROGVERSION"
833 834
		exit 0
		;;
835 836 837
	-e)
		print_name_version
		;;
838
	*)
839
		print_total_info
840 841 842
		;;
esac