#!/bin/sh
#
# Changelog
#
#	- Tue Feb 01 2011 - CATTO Guillaume
#	* Fix crms00290194: get the right upgrade partition version
#
#	- Mon Apr 02 2012 - SULYAN Michel
#	* Fix crms00369558: regression due to crms00290194
#
#	- Mon Apr 19 2012 - SULYAN Michel
#	* Fix crms00371518: loss of upgrade partition

esc=$'\x1b' attr="$esc[32m" default="$esc[0m" PS4="+$$: "

SYSCONFIG=/etc/sysconfig
SOFTVERSIONS=/etc/versions
BASEVERSIONS=/etc/baselines

# +++ crms00290194
PRINTK_CONFIG=/proc/sys/kernel/printk
# --- crms00290194

LinuxVers=`uname -r`
LinuxHost=`uname -n`
LinuxMach=`uname -m`

FULL=0

#######################
: "Function definition"
#######################

Usage()
{
	echo "ID: get terlinal identification"
	echo "Usage:"
	echo " 		id [full|sw|hw|linux|comp|rpm]"
}

ShortId()
{
        echo "soft       $VersionSoft"
        echo "type       $PhoneModel"
        echo "range      SIP"
        echo "hard       $PCMS"
        echo "serial     $SerialNumber"
        echo "builddate  $VersionDate"
}

LinuxId()
{
        echo "LinuxVers  $LinuxVers"
        echo "LinuxHost  $LinuxHost"
        echo "LinuxMach  $LinuxMach"
}

SwId()
{
        if [ "`grep 'ibl=' /proc/cmdline`" != "" ] ; then
                IBL_VER=`sed "s/.*ibl=\([0-9]*\.[0-9]*\).*/\1/g" /proc/cmdline `
        else
                IBL_VER=`rpm -q --qf "%{VERSION}" ibl`
        fi
        if [ "`grep 'sbl=' /proc/cmdline`" != "" ] ; then
                SBL_VER=`sed "s/.*sbl=\([0-9]*\.[0-9]*\).*/\1/g" /proc/cmdline `
        else
                SBL_VER=`rpm -q --qf "%{VERSION}" u-boot`
        fi

        # +++ crms00290194 +++
        UpgradeId
        # --- crms00290194 ---

        if [ $FULL == 0 ]
        then
        echo "soft       $VersionSoft"
        fi
        echo "boot       $IBL_VER"
        echo "u-boot     $SBL_VER"
        echo "upgrade    $VersionUpgd"
        echo "fab        $SWVer"
}

HwId()
{
        echo "board      $HWVer"
        echo "cputype    $CPUType"
        echo "ramsize    $DDRSize"
        echo "flashsize  $FlashSize"
        echo "touchpanel $TouchPanelVer"
        echo "sensekey   $SenseKeyVer"
        echo "backlight  $BackLightVer"
}

CompId()
{
        if [ -f $BASEVERSIONS ] ; then
           awk '{FS="="; printf("%-20s  %s\n", $1,$2); }' $BASEVERSIONS
        else
           echo "Cannot find $BASEVERSIONS"
        fi
}

RpmId()
{
        #rpm -qa --qf "%{NAME} %{VERSION} %{RELEASE}\n"
        rpm -qa
}

FullId()
{
        FULL=1
        ShortId
        HwId
        SwId
        LinuxId
        CompId
}

SavePrintkConfig() {
	unset OLD_PRINTK_CONFIG
	OLD_PRINTK_CONFIG=$(cat $PRINTK_CONFIG)
}

SetPrintkConfig() {
	: Setting printk configuration to value \"$1\"
	echo "$1" > $PRINTK_CONFIG
}

# +++ crms00371518 +++
GetUpgradeId()
{
	VersionUpgd=

	# create temporary mount point to avoid issue with upgrade software
	_upgdPart=`mktemp -d -p /var/tmp mntupgd.XXXXXX`
	if [ $? -eq 1 ]; then
		return 1
	fi

	# Save old printk configuration to restore it later
	# then disable mount kernel messages
	SavePrintkConfig
	SetPrintkConfig "0"

	# mount upgrade partition read-only to read versions file
	mount -t ubifs -o ro ubi0:UPGRADE $_upgdPart 2> /dev/null
	if [ $? -eq 0 ]; then
		_version=$(grep VersionSoft $_upgdPart/etc/versions 2> /dev/null)
		# umount upgrade partition
		umount $_upgdPart 2> /dev/null
	fi

	# restore old printk configuration
	[ a"$OLD_PRINTK_CONFIG" != "a" ] && SetPrintkConfig "$OLD_PRINTK_CONFIG"

	# remove temp mount point
	rmdir $_upgdPart 2> /dev/null

	# check read version
	if [ -z $_version ]; then
		return 1
	fi
	# Split it
	VersionUpgd=${_version##*=}

	return 0
}

UpgradeId()
{
	# get upgrade version from partition first 
	GetUpgradeId
	if [ -z $VersionUpgd ]; then
		# get upgrade version from rpm database
		# +++ crms00369558 +++
        	VersionUpgd=`rpm -q --qf "%{RELEASE}" upgrade_image`
		# --- crms00369558 ---
	fi
}

# --- crms00371518 ---

##############################
: "End of function definition"
##############################

: "${attr}Read system configuration${default}"
. $SYSCONFIG
. $DEBUGBINPATH/.functions

CONFIG_FAB="/config-fab"

: "${attr}Read Software & Hardware Files${default}"
[[ -f $SOFTVERSIONS ]] && { . $SOFTVERSIONS; } || { echo "Cannot find $SOFTVERSIONS"; }

: "${attr}Read Fab Config Files${default}"
PhoneModel=`CLISettings get CURRENT FAB_PHONE_MODEL 2>/dev/null`
PCMS=`CLISettings get CURRENT FAB_PCMS 2>/dev/null`
SerialNumber=`CLISettings get CURRENT FAB_SERIAL_NUMBER 2>/dev/null`
HWVer=`CLISettings get CURRENT FAB_HW_VERSION 2>/dev/null`
SWVer=`CLISettings get CURRENT FAB_SW_VERSION 2>/dev/null`
CPUType=`CLISettings get CURRENT FAB_HW_CPUTYPE 2>/dev/null`
DDRSize=`CLISettings get CURRENT FAB_HW_DDRSIZE 2>/dev/null`
FlashSize=`CLISettings get CURRENT FAB_HW_FLASHSIZE 2>/dev/null`
TouchPanelVer=`CLISettings get CURRENT FAB_HW_TOUCHPANEL 2>/dev/null`
SenseKeyVer=`CLISettings get CURRENT FAB_HW_SENSEKEY 2>/dev/null`
BackLightVer=`CLISettings get CURRENT FAB_HW_BACKLIGHT 2>/dev/null`

#[[ -f $NVFAB_ROOT/Release ]] && { . $NVFAB_ROOT/Release; } || { echo "Cannot find $NVFAB_ROOT/Release"; }
#[[ -f $NVFAB_ROOT/HardwareVersion ]] && { . $NVFAB_ROOT/HardwareVersion; } || { echo "Cannot find $NVFAB_ROOT/HardwareVersion"; }
#[[ -f $NVFAB_ROOT/SoftwareVersion ]] && { . $NVFAB_ROOT/SoftwareVersion; } || { echo "Cannot find $NVFAB_ROOT/SoftwareVersion"; }
#[[ -f $CONFIG_FAB/version.ini ]] && { . $CONFIG_FAB/version.ini 2>/dev/null; } || { echo "Cannot find $CONFIG_FAB/version.ini"; }
#[[ -f $CONFIG_FAB/fabdata.ini ]] && { . $CONFIG_FAB/fabdata.ini 2>/dev/null; } || { echo "Cannot find $CONFIG_FAB/fabdata.ini"; }

[ $# -ne 0 ] || {  ShortId ; EXITOK; }


if [ "$1" == "full" ]
then
   FullId
   EXITOK
else
if [ "$1" == "sw" ]
then
   SwId
   EXITOK
else
if [ "$1" == "hw" ]
then
   HwId
   EXITOK
else
if [ "$1" == "linux" ]
then
   LinuxId
   EXITOK
else
if [ "$1" == "comp" ]
then
   CompId
   EXITOK
else
if [ "$1" == "rpm" ]
then
   RpmId
   EXITOK
else
   Usage
   EXITKO
fi
fi
fi
fi
fi
fi
