#!/bin/bash

#/**
#         deployImage
#@brief   Actualiza la cache del cliente con una imagen, y la restaura en la partición.
#@param 1 
#@param ejemplo:  
#@return  
#@exception OG_ERR_FORMAT     formato incorrecto.
#@note   
#@todo: 
#@version 0.9.1 - integracion EAC
#@author  Antonio J. Doblas Viso. Universidad de Malaga.
#@date    2008/03/17
#@version 0.9.2 - integracion OpenGnsys
#@author  Antonio J. Doblas Viso. Universidad de Malaga.
#@date    2010/07/27
#@version 1.0.2 - Separacion de log
#@author  Antonio J. Doblas Viso. Universidad de Malaga.
#@date    2010/08/04
#*/ ##

TIME1=$SECONDS

#Carga del configurador del engine
[ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg


PROG="$(basename $0)"
if [ $# -lt 4 ]; then
    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $PROG REPO imagen ndisco nparticion [ UNICAST|MULTICAST|TORRENT ] [opciones protocolo]"
    exit $?
fi

#controlar param1 REPO
echo "[1] $MSG_SCRIPTS_TASK_START  updateCache REPO $2.img $5 $6" | tee -a $OGLOGSESSION $OGLOGFILE
updateCache REPO $2.img $5 $6 | tee -a $OGLOGCOMMAND
RETVAL=$?

TIME2=$[SECONDS-TIME1]
echo "   [ ]  $MSG_SCRIPTS_TIME_PARTIAL updateCache $[TIME2/60]m $[TIME2%60]s" | tee -a $OGLOGSESSION $OGLOGFILE



if [ "$RETVAL" != "0" ]
then
	echo "[100] $MSG_SCRIPTS_TASK_ERR = $RETVAL  ; updateCache REPO $2.img $5 $6 " | tee -a $OGLOGSESSION $OGLOGFILE
	# RC=15 No hay cache
	# RC=16 no hay espacio sufiente
	exit $RETVAL	
fi




TIME2=$SECONDS
echo "[50] $MSG_SCRIPTS_TASK_START  ogRestore CACHE /$2 $3 $4" | tee -a $OGLOGSESSION $OGLOGFILE
ogRestoreImage CACHE /$2 $3 $4 &>> $OGLOGCOMMAND
RETVAL=$?
if [ "$RETVAL" == "0" ] 
then
	RESUMERESTOREIMAGE=$(cat $OGLOGCOMMAND | grep "Total Time:")
	echo "   [ ]      $RESUMERESTOREIMAGE " | tee -a $OGLOGSESSION $OGLOGFILE
	echo "   [ ]      $MSG_SCRIPTS_TIME_PARTIAL : $[TIME2/60]m $[TIME2%60]s" | tee -a $OGLOGSESSION $OGLOGFILE
	TIME2=$[SECONDS-TIME2]
	echo "[90] $MSG_SCRIPTS_OS_CONFIGURE " | tee -a $OGLOGSESSION $OGLOGFILE
	configureOs $3 $4 | tee -a $OGLOGCOMMAND
fi
 
TIME=$[SECONDS-TIME1]
echo "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s" | tee -a $OGLOGSESSION $OGLOGFILE



exit $RETVAL