#!/bin/bash
#  A simple version utility for Ubuntu.
#  When used first time it will install itself to --> /usr/local/bin
#  Copyright 2010 Arto Jääskeläinen, temp001@pp.inet.fi  
#
#  This program 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.
#
#  Ver 1.0  Feb 09, 2010 / Arto Jaaskelainen.  
#  Ver 1.01 Feb 15, 2010 / Arto Jaaskelainen. Minor comment update above.
#  Ver 1.02 March 1, 2010 /A.J. Alsa version, update control of "ver".
#  Ver 1.1  March 16, 2010 / A.J. Detect LSB, info for non LSB dists.
#  Ver 1.11 March 16, 2010 / A.J. Installation outside of current dir fixed.
#  
THIS_VER=1.11    # <--Remember to update current version number here
#
# Do not add ANY characters in front of the line above --- version control variable.
#

GET_VERSION()
#Entry none, return INSTALLED_VER 
{
if [ -e /usr/local/bin/ver ]; then  # Some version of ver utility exists ?
INSTALLED_VER=`cut -b 1-14 /usr/local/bin/ver | grep THIS_VER= | sed 's/[a-Z _#<='-']*//g' `
if [ -z $INSTALLED_VER ]; then   #No version number found
INSTALLED_VER=1.00
fi 
else
INSTALLED_VER=0    #No ver file found
fi
}

CONV_IN() #Returns string looking like integer
#Entry ST, return RET
{
DOTPOS=`expr index $ST '.'`       #Position of dot
ST1=`expr match $ST '\([0-9]*\)'` #All before dot
ST2=`echo ${ST:$DOTPOS}`          #All after dot
LEN_ST2=${#ST2}                   #Length after dot
if [ $LEN_ST2 != 2 ]; then        #We force always two digits
ST2=`expr $ST2 \* 10`             #FYI, no parentehsis allowed here
if [ $ST2 = 0 ]; then             #Specific case of zero
ST2=00                            #Two digits for zero, too
fi
fi
RET=$ST1$ST2                      #Concatenated string
}

INSTALL()
{
if [ $THIS_VER_IN -gt $INSTALLED_VER_IN ] ; then  		# Need to update 
	sudo cp $0 /usr/local/bin/ver
	GET_VERSION
	ST=$INSTALLED_VER
	CONV_IN
	INSTALLED_VER_IN=$RET
	if [ $THIS_VER_IN = $INSTALLED_VER_IN ] ; then
	echo -e 
	echo 'Installed/updated '"ver"' utility succesfully.'
	else
	echo 'Could not install/update '"ver"' utility.'
	echo 'this ver:' $THIS_VER  'installed ver:' $INSTALLED_VER
	fi
fi
}

UNINSTALL()
{
sudo rm /usr/local/bin/ver 2>/dev/null
if [ ! -e /usr/local/bin/ver ]; then
echo Uninstall succesful.
else
echo Could not uninstall "ver" utility.
fi
}

HELP()
{
echo
echo 'ver' v$THIS_VER  '--- A simple version utility for Linux.'
echo 
echo 'When used first time it will install itself to /usr/local/bin.'
echo 
echo '    ver [-h | --help, -u | --uninstall]'
echo
echo 'ver   alone will show version information.'
echo
}

GET_BITS()
#Entry: none return: BIT_AMOUNT
{
BITS=`uname -m` 
case $BITS in
i686 )
  BIT_AMOUNT='32 bits';;
x86_64 )
  BIT_AMOUNT='64 bits';;
* )
  BIT_AMOUNT='Unknown';;
esac
}

#Version/update control of ver utility ----------------------

if [ $# = 0 ]; then   #No command line parameters

GET_VERSION
ST=$INSTALLED_VER
CONV_IN
INSTALLED_VER_IN=$RET

ST=$THIS_VER
CONV_IN
THIS_VER_IN=$RET

INSTALL
#-------------------------------------------------------------

if [ -e /etc/lsb-release ]; then  # Detect LSB compliant O/S
echo  
lsb_release -a 
echo -n Kernel:'         '
uname -r
GET_BITS
echo 'Bit width:      '$BIT_AMOUNT
else
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`

if [ "${OS}" = "SunOS" ] ; then
	OS=Solaris
	ARCH=`uname -p`	
	OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
	OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
	KERNEL=`uname -r`
	if [ -f /etc/redhat-release ] ; then
		DIST='RedHat'
		PSEUDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
		REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
	elif [ -f /etc/SuSE-release ] ; then
		DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
		REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
	elif [ -f /etc/mandrake-release ] ; then
		DIST='Mandrake'
		PSEUDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
		REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
	elif [ -f /etc/debian_version ] ; then
		DIST="Debian `cat /etc/debian_version`"
		REV=""

	fi
	if [ -f /etc/UnitedLinux-release ] ; then
		DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
	fi
	
	OSSTR="${OS} ${DIST} ${REV}(${PSEUDONAME} ${KERNEL} ${MACH})"

fi
echo
echo 'Non LSB O/S:    '${OSSTR}

fi

echo -n 'Sound:          '
cat /proc/asound/version
echo

#--------------------------------------------------------------
else
case $1 in
-h | --help )
  HELP;;
-u | --uninstall )
  UNINSTALL;;
* )
  HELP
  ;;
esac

fi

