#!/bin/bash -p
#
# cvms v2.1 2013-08-04 , Finn Magnusson, finn.magnusson@ericsson.com
# Type "cvms" on its own for help

###########################################################################################
# Pathes to files:
###########################################################################################
moshelldir=`dirname "$0"`
if [[ $moshelldir != /* ]] ; then moshelldir=`pwd`/$moshelldir ; fi
swversion_file=$moshelldir/cmdfiles/swversions
vobsinstallation=0
unamea=$(uname -a)
gawkext=""
gawklib=""
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* ]] ; then gawklib="lin64"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".lin64" ; fi
elif [[ $unamea = [Ll][iI][nN][uU][xX]* ]]      ; then gawklib="linux"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".linux" ; fi
elif [[ $unamea = SunOS*sparc* ]]               ; then gawklib="solaris" ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".solaris" ; fi
elif [[ $unamea = SunOS* ]]                     ; then gawklib="sol86"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".sol86" ; fi
else gawklib="cygwin" ; gawkext=".exe"
fi
gawkprog="gawk${gawkext}" 
gawk="$moshelldir/$gawkprog"
#special case where moshell has been installed on a linux 32 bit machine but should be run on linux 64 sharing the same file system
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* && $vobsinstallation != 1 && `file "$moshelldir/gawk"` = *32-bit* ]] ; then gawklib="linux" ; fi
filefunc="$moshelldir/commonjars/lib/${gawklib}/filefuncs"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}$moshelldir/commonjars/lib/${gawklib}
export LANG=C
export LC_ALL=C

mobatch=$moshelldir/mobatch
tempdir="/tmp" 
tempcmdfile=$tempdir/cvms$$_$(date +%H%M%S) 
logdir="$moshelldir/logfiles/logs_cvms"
defLogDir=$moshelldir/commonjars/defLogDir.sh
temp_logdir=$($defLogDir cvms)
if [[ -w $temp_logdir && -d $temp_logdir ]] ; then logdir=$temp_logdir ; fi 
###########################################################################################
#include_time: set to 0 if time shouldn't appear in the cv name, ie: yymmdd_swlevel_cvname
#-----------------------------------------------------------------------------------------
include_time=0
###########################################################################################

function print_usage()
{
cat <<EOF
Usage:  cvms <sitefile> <cv name> <operator> [<comment>]

	Purpose: makes a cv and sets it as startable, for each rbs in the sitefile
		 the cv name will look like: yymmdd-hhmm_swlevel_cvname 
		 If SW level is not required to appear in the cvname, then it is 
		 possible to use mobatch (using command: cvms) instead of the cvms utility.
		 
	Example: cvms victoria-sites.txt baseline_1.5 finn 'align RF parameters'
		===> cv will look like this: 030404-2016_P2.0.13_baseline1.5		 
	Note:   swlevel and date are added automatically by cvms
		if comment contains spaces, it should be put in between quotes)

	Arguments:
	 - The first argument is the sitefile, containing IP/DNS addresses and/or 
	site names whose IP address are defined in the IP database.
	See example of sitefile and ipdatabase in: moshell/examples/mobatch_files
	 - The second argument is the cv name. Should be less than 20 chars.
	It will be automatically preceded by the date, time and SW level. 
	These dont need to be specified.
	 - The third argument is the operator.
	 - The fourth argument, the comment, is optional.
	If comment contains spaces, it should be put in between quotes.
		
	********************************************************************	 
	The swversions file path must be specified inside the cvms file.
	The swversions file contains the cross-reference between R-level of the UP and its P2 id
	See example in moshell directory: moshell/examples/cvms_file/swversions
	********************************************************************	 

EOF
}

trap 'rm $tempcmdfile 2>/dev/null' 0 1 2 3 15 

case $# in
4)  comment=$4
    operator=$3
    cvname=$2
    sitefile=$1
    ;;
3)  comment="no_comment"
    operator=$3
    cvname=$2
    sitefile=$1
    ;;
*) print_usage ; exit 1
  ;;   
esac

if ! test -r $swversion_file ; then echo "Unable to read swversion file: $swversion_file" ; exit 1 ; fi
logdir=$logdir/$(date +%y%m%d_%H%M)_$(basename $sitefile)

cat <<EOF > $tempcmdfile
lt ^(upgradepackage|configurationversion|ethernetlink)$
\$date = \`date +%y%m%d\`
get conf currentupg > \$currentUp
get \$currentUp administrativedata > \$currentSw
\$currentSwRev = \`$gawk -v currentsw="\$currentSwRev" 'BEGIN{ swrev=gensub(/\r|\n/,"","g",currentsw) ; print gensub(/^.*Revision = | >>> 3.product.*$/,"","g",swrev) }'\`
\$currentSwRevTranslated = \`$gawk '\$1=="\$currentSwRev" { print \$2}' $swversion_file\`
if \$currentSwRevTranslated !=
\$currentSwRev = \$currentSwRevTranslated
fi
cvms \$date_\$currentSwRev_$cvname $operator $comment
EOF

#echo $tempcmdfile
#cat $tempcmdfile

$mobatch $sitefile $tempcmdfile $logdir

echo -e "\n\nChecking CVMS results...."
cd $logdir
for file in * ; do
nodename=`echo $file | tr -d ".log"`
cvname=`grep "configurationVersionName (string):" $file`
cvname=`echo $cvname |  $gawk '{print gensub(/^.*: /,"",1,$0)}'`
if [[ `grep -c '>>> Return value = null' $file` = 2 ]] ; then echo $nodename : CV OK - CvName= $cvname
else echo $nodename : CV FAIL
fi
done





#get 0 userlabel > \$node
#get 0 site      > \$site
#get ethernetlink  ipaddress > \$ipaddress
#if \$node != 
#\$tempfile = /tmp/cvms_ + \$node
#else if \$site != 
#\$tempfile = /tmp/cvms_ + \$site
#fi
#l echo "\$currentSw" > \$tempfile
#\$currentSwRev = \`awk '/2.productRevision/{ print \$NF}' \$tempfile\`
#l rm \$tempfile
#The tempfile must be unique for each node
#\$tempfile = /tmp/cvms_ + \$ipaddress
#\$tempfile = \$tempfile + _$$
#administrativeData                   Struct{5}
# >>> 1.productNumber = MGw_R2_CNP_R2.0.7.0
# >>> 2.productRevision = R1A01
# >>> 3.productName = UP_C_MGw_R2_CNP_R2.0.7.0
# >>> 4.productInfo = Upgrade for C_MGw_R2_CNP_R2.0.7.0
# >>> 5.productionDate = 20040607


