#!/bin/sh

# $Id$
# $URL$
#
# Criado por Ingo Schmidt em 03/2006
# Modificado por Marcelo Dalmas 09/2009.
# Modificado por Ana Lucia de Oliveira Tavares em 11/2010
# Modificado por Andre Marra em 09/2012.

# Copyrigth (C) 2006-2012 Reason Tecnologia S.A. Todos os direitos reservados.

cd pqfw

source ./installer.params

NEED_REBOOT=1

# Impede a instalacao do firmware em equipamentos com P800/900.
OSVERSION=`/sbin/osversion | cut -d' ' -f1`

if [ $OSVERSION != "rpv311-os" ]; then
    echo "Installation aborted:"
    echo "Firmware version does not match to this equipment [${LABEL}-${VERSION}${VARIANT}${REVISION}.${SUBREVISION}]."
    logger -tinstall -i -plocal0.info "Installation aborted:"	
    logger -tinstall -i -plocal0.info "Firmware version does not match to this equipment [${LABEL}-${VERSION}${VARIANT}${REVISION}.${SUBREVISION}]."
    exit 1
fi

if [ -e /etc/pqfw/RCS/cfg.xml,v ]; then 

    current_model=`cut -b1-3 /etc/pqfw/version`
    
    if [ ${current_model} == "RPV" ]; then
	current_label=`cut -b1-6 /etc/pqfw/version`
	current_version=`cut -b8-9 /etc/pqfw/version`
	current_revision=`cut -b11-12 /etc/pqfw/version`
	current_subrevision=`cut -b14-15 /etc/pqfw/version`
    else
	current_label=`cut -b1-7 /etc/pqfw/version`
	current_version=`cut -b9-10 /etc/pqfw/version`
	current_revision=`cut -b12-13 /etc/pqfw/version`
	current_subrevision=`cut -b15-16 /etc/pqfw/version`
    fi

    #Impede a troca de equipamento no upgrade do firmware.
    if [ ${LABEL} != ${current_label} ]; then
	echo "Installation aborted:"
	echo "Firmware version does not match to this equipment."
	logger -tinstall -i -plocal0.info "Installation aborted:"	
	logger -tinstall -i -plocal0.info "Firmware version does not match to this equipment [${LABEL}-${VERSION}${VARIANT}${REVISION}.${SUBREVISION}]."
	exit 1
    fi

    #Impede a realizacao de um downgrade.
    if [ ${current_version} -gt ${VERSION} ] || [ ${VERSION} -eq ${current_version} -a ${current_revision} -gt ${REVISION} ] ||
	[ ${current_version} -eq ${VERSION} -a ${current_revision} -eq ${REVISION} -a ${current_subrevision} -gt ${SUBREVISION} ] ||
	[ "${SUBREVISION}" == "" ]; then
	echo "Installation aborted:"
	echo "Firmware version is older than the one currently installed."
	logger -tinstall -i -plocal0.info "Installation aborted:"
	logger -tinstall -i -plocal0.info "Firmware version is older than the one currently installed."
	exit 1
    fi

    MSGS="Starting firmware upgrade [${LABEL}-${VERSION}${VARIANT}${REVISION}.${SUBREVISION}]"
    MSGF="Finishing firmware upgrade"
    NEED_REBOOT=0
else
    MSGS="Installing [${LABEL}-${VERSION}${VARIANT}${REVISION}.${SUBREVISION}]"
    MSGF="Finishing install"
fi

# Encerra processos.

sh installer/stop.sh

# Inicializa o syslog-ng para a instalacao.

/usr/sbin/syslog-ng -f `pwd`/etc/syslog-ng/syslog-ng.conf.install

# Atualizar sistema operacional
sh patch/rpv311-os.patch.bash
if [ $? -ne 0 ]; then
    echo "Installation aborted:"
    echo "Operating system failed during upgrade."
    logger -tinstall -i -plocal0.info "Installation aborted:"
    logger -tinstall -i -plocal0.info "Operating system failed during upgrade."
    exit 1
fi

logger -tinstall -i -plocal0.info ${MSGS}

# Inicia a instalacao.

sh installer/base.sh ./installer.params
sh installer/system.sh ./installer.params
sh installer/instrument.sh ./installer.params
sh installer/wif.sh ./installer.params

# Copia arquivos e diretorios das particoes tmpfs

rm -f /var/log/messages.[1-9] /var/foo/var/log/* > /dev/null 2>&1
rm -f /var/foo/var/run/* > /dev/null 2>&1
rm -f /data/log/eqpt.log.[4-9] /var/foo/data/log/* > /dev/null 2>&1

/usr/bin/rsync -av /var/log/ /var/foo/var/log > /dev/null 2>&1
/usr/bin/rsync -av /var/run/ /var/foo/var/run > /dev/null 2>&1
/usr/bin/rsync -av --filter='exclude eqpt.log.*' /data/log/ /var/foo/data/log > /dev/null 2>&1
/bin/cp /data/log/eqpt.log.[1-3] /var/foo/data/log/ > /dev/null 2>&1

# Remove diretorios lost+found dos diretorios de registros

rm -rf /data/*/lost+found > /dev/null 2>&1

logger -tinstall -i -plocal0.info ${MSGF}

cd ..

# eof $Id$
