#!/bin/sh -
OPATH="$PATH"
PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd; export PATH

# opsys - guess machine's operating system
# Steve Kinzler, steve@kinzler.com, Sep 93
# https://kinzler.com/me/home.html#unixuni

# heavily borrowed from Jan-Simon Pendry's "os-type" script

case "$1" in
'')	source=;;
-s)	source=t;;

*)	cat << EOF 1>&2
usage: $0 [ -s ]
	-s	report source of operating system guess
EOF
	exit 1;;
esac

src=uname
oss=`($src -s) 2> /dev/null`
os=

test -d /data/data/com.termux && oss=

case "$oss" in
?*)	osr=`$src -r`

	case "`$src -n`" in
	"$oss")	case "$osr" in
		[0-9]*)	oss=unix;;
		*)	oss=;;
		esac;;
	esac

	case "$oss$osr" in
	[Aa][Ii][Xx]*)			osr=`$src -v`;;
	[Bb][Ss][Dd]/386*)		os=bsd386;;
	[Dd][Aa][Rr][Ww][Ii][Nn]*)	os=darwin;;
	[Ff][Rr][Ee][Ee][Bb][Ss][Dd]*)	os=freebsd;;
	[Hh][Pp]-[Uu][Xx]*)		os=hpux;;
	[Ii][Rr][Ii][Xx]*)		osr=`echo "$osr" | sed 's/\..*//'`;;
	[Oo][Ss][Ff]1*)			osr=`echo "$osr" | sed 's/\..*//'`;;
	[Ss][Uu][Nn][Oo][Ss]*)		osr=`echo "$osr" | sed 's/\..*//'`;;
	[Uu][Ll][Tt][Rr][Ii][Xx]*)	osr=`echo "$osr" | sed 's/\..*//'`;;
	[Li][Ii][Nn][Uu][Xx]*)		os=linux
		test -f /etc/redhat-release && os=redhat
		test -f /etc/SuSE-release   && os=suse
		ID=; DISTRIB_ID=
		test -r /etc/os-release     && . /etc/os-release
		test -r /etc/lsb-release    && . /etc/lsb-release
		case "${ID:-$DISTRIB_ID}" in
		?*)	os=; osr=
			oss=`echo "${ID:-$DISTRIB_ID}" | tr A-Z a-z`
			case "$oss" in
			rhel)	oss=redhat;;
			sles)	oss=suse;;
			esac;;
		esac;;
	esac

	case "$os" in
	'')	case "$oss" in
		*[0-9])	case "$osr" in
			[0-9]*)	os="$oss-$osr";;
			*)	os="$oss$osr";;
			esac;;
		*)	os="$oss$osr";;
		esac

		case "$os" in
		*[\ ./A-Z]*)	abc=abcdefghijklmnopqrstuvwxyz
				ABC=ABCDEFGHIJKLMNOPQRSTUVWXYZ
				os=`echo "$os" |
				    sed "s/[ \.]/_/g; s,/,-,g; y/$ABC/$abc/"`;;
		esac;;
	esac;;

*)	src=/etc/motd

	case "`sed 3q $src 2> /dev/null | tr '[A-Z]' '[a-z]'`" in
	*"4.4 bsd unix"*)		os=bsd44;;
	*"4.3 bsd reno unix"*)		os=bsd44;;
	*"4.3 bsd unix"*)		os=bsd44
					if test -f /etc/minidisk; then
						os=acis43
					elif test ! -f /sbin/nfsiod; then
						os=xinu43
					fi;;

	*"umax 4.3"*)			os=umax43;;
	*"ultrix v4."*)			os=ultrix4;;
	*"ultrix v3."*)			os=ultrix3_0;;
	*"ultrix worksystem v2."*)	os=ultrix3_0;;
	*"ultrix-32 v3."*)		os=ultrix3_0;;
	*"ultrix v2."*)			os=ultrix2_2;;
	*"multiflow 4.3 bsd"*)		os=tracebsd;;
	*"sunos 5."*)			os=sunos5;;
	*"sunos release 4."*)		os=sunos4;;
	*"sun unix 4.2 release 3."*)	os=sunos3;;
	*"risc ix release 1."*)		os=riscix;;
	*"dec osf/1 v3."*)		os=osf1v3;;
	*"dec osf/1 v2."*)		os=osf1v2;;
	*"dec osf/1 v1."*)		os=osf1v1;;
	*"riscos 4"*)			os=mipsos4;;
	*"mach 1000 release 2.5"*)	os=mach2_5;;
	*"hp-ux"*)			os=hpux;;
	*"hlh ots version 1."*)		os=hlh42;;
	*"hcx/ux"*)			os=hcx;;
	*"fpx 4."*)			os=fpx4;;
	*"dynix/ptx"*)			os=dynixptx;;
	*"dynix(r"*)			os=dynix;;
	*" dcosx "*)			os=dcosx;;
	*"cx/ux"*)			os=cxux;;
	*"alliant concentrix"*)		os=concen;;

	*)	src=Filesystem

		if   test -d /data/data/com.termux; then os=termux
		elif test -f /etc/comply;	    then os=utek
		elif test -d /usr/lib/methods -o \
			  -d /etc/methods;	    then os=aix3
		elif test -f /etc/nd;		    then os=sunos3
		elif test -f /etc/elcsd;	    then os=ultrix4
		elif test -f /hp-ux;		    then os=hpux
		elif test -f /etc/ttylocal;	    then os=xinu43
		elif test -f /etc/minidisk;	    then os=acis43
		elif test -f /etc/toolboxdaemon;    then os=aux
		elif test -f /sbin/nfsiod;	    then os=bsd44
		elif test -d /vrm;		    then os=aix2
		elif test -f /dynix;		    then os=dynix
		elif test -f /bin/pyr && /bin/pyr;  then os=pyrosx
		elif test -d /NextApps;		    then os=nextos
		elif test -f /etc/gl/ucode;	    then os=irix3
		elif test -f /usr/gfx/ucode -o \
			  -d /usr/gfx/ucode;	    then os=irix4
		elif test -f /stellix;		    then os=stellix
		elif test -f /unicos;		    then os=unicos

		else
			src=archit
			arch=`(PATH="$OPATH"; export PATH; $src) 2> /dev/null`

			case "$arch" in
			ibm032)	os=acis43;;
			aviion)	os=dgux;;
			*)	os=UNKNOWN; src=none;;
			esac
		fi;;
	esac;;
esac

case "$source" in
?*)	os="$os ($src)";;
esac

exec echo "$os"
